IIS rewrite encoder

Encode analytics code so it can be used in IIS URL rewrite

by Russ Michaels

HTML

<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 &lt;system.webServer&gt; 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>
        &lt;rewrite&gt;
            &lt;outboundRules&gt;
                &lt;rule name=&quot;Google analytics&quot; preCondition=&quot;IsHtml&quot;&gt;
                    &lt;match filterByTags=&quot;None&quot; pattern=&quot;&amp;lt;/body&gt;&quot; ignoreCase=&quot;true&quot; /&gt;
    &lt;action type=&quot;Rewrite&quot; value=&quot;<div id="insertCode">#analytics code will go here#</div> &amp;lt;/body&gt;&quot; /&gt;
                &lt;/rule&gt;
                &lt;preConditions&gt;
                    &lt;preCondition name=&quot;IsHtml&quot; logicalGrouping=&quot;MatchAny&quot;&gt;
                        &lt;add input=&quot;{RESPONSE_CONTENT_TYPE}&quot; pattern=&quot;^text/html&quot; /&gt;
                    &lt;/preCondition&gt;
                &lt;/preConditions&gt;
            &lt;/outboundRules&gt;
        &lt;/rewrite&gt;
</pre>

CSS

body {font-family: arial,verdana,helvetica;}
pre {font: courier new; color:blue;}

JavaScript

function encode() {
	var encoded = document.getElementById('code').value;
    encoded = encoded.replace(/"/g,"'");
    encoded = encoded.replace(/</g,"&lt;");
    encoded = encoded.replace(/{/g,"{UrlDecode:%7B}");
    encoded = encoded.replace(/(\r\n|\n|\r)/gm,"");
    document.getElementById('ncode').value = encoded;
    document.getElementById('insertCode').innerText = encoded;
}