Updated Google Analytics tracking code (ga.js)

Google has updated the ga.js tracking code due to a number of problems Google Analytics users were experiencing. The initial release of the new Google Analytics Tracking Code (GATC) prevented websites from validating, did not publish correctly within Blogger templates and the new code did not work with ASP websites. The updated tracking code solves these problems.

Updated version of the ga.js tracking code:

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-XXXX-XX");
pageTracker._initData();
pageTracker._trackPageview();
</script>


This updated tracking code does not validate for XHTML Strict websites, however adding CDATA tags solves this problem.

XHTML compliant version of the updated ga.js tracking code:

<script type="text/javascript">
//<![CDATA[
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
//]]>
</script>
<script type="text/javascript">
//<!--[CDATA[
var pageTracker = _gat._getTracker("UA-XXXX-XX");
pageTracker._initData();
pageTracker._trackPageview();
//]]-->
</script>


Here is a copy of the tracking code before the update for those of you who are interested.

Previous version of the tracking code:

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write("\
\<\/script>" );
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-XXXX-XX");
pageTracker._initData();
pageTracker._trackPageview();
</script>


Thank you to Justin Cutroni who posted about the updated ga.js tracking code and Jason Green who provided the XHTML Strict solution.

Labels: , ,

Share

Posted on Saturday, December 29, 2007

0 Comments:

Post a Comment

Links to this post:

Create a Link

Mangold Sengers provides Google Analytics consulting, Google Analytics onsite training and Google Analytics online training. Contact us to discuss your needs.