Google Analytics Tracking Code History

By Xah Lee. Date: . Last updated: .

Few months ago (2010 July), Google released a new tracking code for Google Analytics, featuring asynchronous JavaScript. If you haven't updated yet, you should do it now. It increases your page load speed noticeably. When during busy times such as morning or noon, the speeds up can be 1 second or more.

Note that saving a fraction of a second matters. See: [Speed Matters By Jake Brutlag. At http://googleresearch.blogspot.com/2009/06/speed-matters.html , accessed on 2009-12-26 ].

This is their old code, in 2 blocks of 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"));
try {
var pageTracker = _gat._getTracker("UA-xxxxxxxx-x");
pageTracker._trackPageview();
} catch(err) {}

This is their new code:

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-xxxxxxxx-x']);
_gaq.push(['_trackPageview']);

(function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(ga, s);
})();

Some Analysis of the New Code

Let's study this new code a bit.

You can see that it first creates the variable _gaq if it is not already defined. Then it defines a anonymous function by:

function() {body}

then call it at the same time by

(function() {body}) ()

This is commonly technique to create a function expression and evaluate it at the same time. [see Functional Programing (old, pre-JS2015)]

The function basically constructs the following HTML code and insert it into the page.

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

(or https version at “https://ssl.”.)

Now let's look at the JavaScript code ga.js. It is a minimized and obfuscated JavaScript code. It has a total of 25k chars. It has 839 ;, and 383 {} pairs, and the function definition happens about 254 times. If you expand the code into lines, it's about over 1k lines, more likely close to 2k lines. When de-obfuscated but without rewriting functions, i'd guess it's 4k lines. See the files here:

2014 February, New Google Universal Analytics Tracker Code

Google Analytics again updated their tracker, called “Universal Analytics”.

Here is the new code:

(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-xxxxxxxx-x', 'example.ocm');
  ga('send', 'pageview');

here's the new code: google_analytics_tracker_2014-02-25.js

See also: JavaScript in Depth

BUY ΣJS JavaScript in Depth