Google Analytics Asynchronous Tracking Code

Advertise Here

,

Few months ago, 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. @ Google Blog.

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() {…}

then call it at the same time by

(function() {…}) ()

This is commonly known as lambda function or function expression.

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 js code “ga.js”. It is a condensed and obfuscated js 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:

The code seems to use a lot advanced functional programing, with lots of function expressions.

Obfuscated Code

Obfuscated code was once a recreational activity, to demonstrate the bad design of languages, in particular C, and Perl, or to showcase the author's mastery of the language.

JavaScript has made code obfuscation a practical need, because the js system does not have encryption, and the code file size need to be as small as possible to increase load speed for massively trafficked sites, and also as a method to prevent other companies from stealing the code or ideas.

Probably because of this practical need, the art of code obfuscation has advanced quite far, into systematic methods that can be machine generated. See Code obfuscation.

There are a lot software for minimizing js, ASP, CSS, HTML code. Here are the major ones:

For a lot more, see: Minification (programming).

Note that for this purpose, these engines make the file size smaller, and impossible to read, but also possibly increase the performance by creating inline functions and eliminate dead code, but one thing it does not do is possibly slowing down code performance for the sake of obfuscation, as recreational obfuscation might do.

blog comments powered by Disqus