Google Analytics Universal Code
Analytics.js: Remove isogram and load early.
The Google Analytics tracking code is on every page of every site of mine. The tracker code is setup using a very common design patern of injecting async javascript on your page.
(function() { var ga = document.createElement(‘script’); […] document.getElementsByTagName(‘script’)[0]; s.parentNode.insertBefore(ga, s);
})();
During the last decade we’ve learned to put any async javascript low in the body to speed up rendering of the page by the browser. Anybody who did any custom event tracking with analytics learned quickly they needed to create a _gaq object first. Something to do with a call to Analytics ’ _gaq before it was made available in your tracking script, leading to Javascript errors.
New Analytics properties are now asked to use an updated tracker code: Google Universal Analytics (beta). One major technical change is that it now relies on a function call, instead of an object.push. The consequence is also that the tracker always needs to be initiated before any calls can be made to it.
The problem with this is that the tracker code needs to high up in the source (prefered in the <head>) to make this function available. Although it’s async, most developers are finaly comfortable with keeping the <head> as clean as possible from any javascript due to render blocking. Now we’re back with being forced to put stuff in the <head>.
If you don’t but it high up; you’re making a call using an function that isn’t defined yet (by default window.ga). Causing more errors.
I’ve looked at the script, and there is no way to quickly init the ga() function high up, while loading the actual tracker lower down the source*.
Another thing that made my spidy senses tingle is that their new tracker code spells a word: isogram. And I hate it. Because I could, I’ve rewritten their code to a more accessible and common design patern:
tracking.js
Email me when Yvo Schaap publishes or recommends stories