Monday, 6 September 2010

Auto-selecting http(s) Google Analytics urchin script source with asp.net

Depending on the configuration of your website, you may have some pages which are https secured, and some which are not. Instead of having to edit each page to include either the https or http url for the urchin source script, you can let asp.net do the clever stuff for you:

    <% if (Request.IsSecureConnection)
       { %>

    <script src="https://ssl.google-analytics.com/urchin.js" type="text/javascript">
    </script>

    <% } %>
    <% else
        { %>

    <script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
    </script>

    <% } %>

Use this in your masterpage, at the bottom, and you can forget about "insecure content" warning and the like.

Cheers.