I am a bit extreme when it comes to optimization (as I will prove in a bit). Both YSlow and Google PageSpeed are great pals in this, and I get concerned when they report inefficiencies. You see, I use Google Analytics in order to track the page views of this site. If you ever used it, you have certainly copied the tracking script from the GA site — the script that is generated for you automatically and you just need to paste in the pages that you want to track. I like this feature pretty much, as it saves time; it also saves Google the need to explain what code you need to write to get it done. However, the code that gets generated isn’t optimized, nor minified, so the above tools find it offensive. So I took the effort of optimizing it, and here is how.
This is the default snippet that gets generated from the GA site, without additional options.
- Array.push can accept multiple arguments. This is even mentioned in the online help, but the generated code does not contain it.
ga.src = '//google-analytics.com/ga.js';
- (Theoretically optional) The script persists its location in the document, which is nice for more advanced scenarios that require more tracking. However, placing the script anywhere else than before the closing body tag might degrade performance. If performance is more important than tracking,
And after a few manual minification tricks, here comes the…
After minification, the reduction is by 45% (187 bytes), since the current GA code generator does not minify its output. Voila!
The optimized script is active on this site, and was tested in IE6 (’nuff said).
I hope that you will find this useful.