Updated Google Analytics tracking code (ga.js)

Written by Benjamin Mangold

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: , ,

New Google Analytics Tracking Code (ga.js) released

Written by Benjamin Mangold

The new Google Analytics Tracking Code (ga.js) is now live. The new tracking code will give you access to new features as they are released, the first new feature will be Event Tracking.

Key features:
  • Smaller tracking code = faster loading speed
  • Easier to use
  • Automatic detection of HTTPS
  • Required for new features

You will have to replace the legacy tracking code (urchin.js) with the new tracking code on your website. Google has stated that the legacy tracking code will continue to function for at least 12 months from the release of the new tracking code.

The following steps show you how to find the new Google Analytics Tracking Code:

Step 1

Click 'Edit' next to the desired profile.

New Google Analytics Tracking Code ga.js

Step 2

Click 'Check Status' on the Profile Settings page.

New Google Analytics Tracking Code ga.js

Step 3

Replace the tracking code on your website with the new ga.js tracking code.

New Google Analytics Tracking Code ga.js

Labels: , ,