<h1>IIS Analytics Encoder</h1>
<h3>by Russ Michaels</h3>
<p>Use this script to encode your Google analytics code so that it can be used in an IIS URL rewrite rule and applied to your entire site.</p>
<form>
paste your tracking code here<br/>
<textarea id="code" cols="50" rows="5"></textarea><br/>
<button type="button" onclick="encode()">Encode</button>
<br/>
encoded version<br/>
<textarea id="ncode" cols="50" rows="5"></textarea><br/>
</form>
<h2>Rewrite Rule</h2>
<p>
Here is the rewrite rule to add to your web.config. Just paste the rule into your web.config <system.webServer> section and your analytics code will be applied to every html page on your site.<br/>
view page source and you should see the analytics code inserted before body tag.</p>
<pre>
<rewrite>
<outboundRules>
<rule name="Google analytics" preCondition="IsHtml">
<match filterByTags="None" pattern="&lt;/body>" ignoreCase="true" />
<action type="Rewrite" value="<div id="insertCode">#analytics code will go here#</div> &lt;/body>" />
</rule>
<preConditions>
<preCondition name="IsHtml" logicalGrouping="MatchAny">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
</preCondition>
</preConditions>
</outboundRules>
</rewrite>
</pre>
CSS
body {font-family: arial,verdana,helvetica;}
pre {font: courier new; color:blue;}