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 , 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 + "' type='text/javascript'%3E%3C/script%3E"));
try {
var pageTracker = _gat._getTracker("UA-xxxxxxxx-x");
} catch(err) {}

This is their new code:

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

(function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '';
    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() {}

then call it at the same time by

(function() {}) ()

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=""></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:

  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),

  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

JavaScript in Depth