Using the information here about inserting code via an outbound rule, and also encoding the Google Analytics javascript (because of the "{" character in the GA javascript), the outbound rule will correctly insert the GA code before the body code .
The process works fine for .htm pages on the IIS site with Coldfusion 9 installed, but a .cfm page (same code as htm, but with a cfm extension so ColdFusion processes it), I get a 502 error (returned by the error page processing). (Compression is turned off.)
The infrastructure is through an ARR proxy server (Windows 2008 R2 with ARR/UrlRewrite), which rewrites the request to another server (Windows 2008 R2, with ColdFusion 9.01 and URLRewrite); that second server has the page file and the outbound rule.
So a Failed Request Tracing shows the actual error as 500.52, which I believe indicates "A rewrite error occurred during RQ_SEND_RESPONSE notification handling. An outbound rule execution occurred."
The "General Reponse Header" in the FRT shows:
Headers="Cache-Control: no-cache Transfer-Encoding: chunked Content-Type: text/html; charset=UTF-8 Server: Microsoft-IIS/7.5 X-Powered-By: cf9prod01"
The GA code inside the outbound rule (created via editing the web.config) is this:
<outboundRules><rule name="googleanalytics tracking script" preCondition="IsHTML" enabled="true" patternSyntax="ExactMatch"><match filterByTags="None" pattern="</body>" /><action type="Rewrite" value="<script type="text/javascript"> var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-35053227-1']); _gaq.push(['_trackPageview']); (function() {UrlDecode:%7B} var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })();</script></body>" /></rule><preConditions><preCondition name="IsHTML"><add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" /></preCondition></preConditions></outboundRules>
So, the question (after hopefully providing enough information): why does the HTML page work with the outbound rule, but the CFM (ColdFusion) page causes the 500.52 error?