Tag: analytics

  • Google Analytics XHTML compliance

    I had to add the Google Analytics code snippet to this site and had some issues with it validating as XHTML 1.1 and MIME type application/xhtml+xml which were solved at the end.

    The snippet is divided into two pieces of <script> tags and the first part is simply a feature to avoid getting warning messages from your browser if your page was server over SSL (https://). If your site serves pages in both HTTP and HTTPS then you can achieve the same through e.g. a server side scripting language such as PHP.

    The snippet needs to be changed to the following on HTTP pages:

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

    and of course on HTTPS pages use this piece:

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

    Now the second part of the code needs to be put between CDATA tags to properly isolate it. The code will now look like this except you need to introduce your own Google Analytics ID:

    <script type="text/javascript">
    //<![CDATA[
    var pageTracker = _gat._getTracker("UA-1234567-8");
    pageTracker._initData();
    pageTracker._trackPageview();
    //]]>
    </script>

    Now you can safely run the W3 Validator without any errors caused by Google Analytics.