Infrequently Noted

Alex Russell on browsers, standards, and the process of progress.

Comments for Ending The ga.js Wait

This is quite useful. The analytics does takes a lot of time to load. Also hope there will be a version from Mootools :-)
by 面经 at
I just copy ga.js to my domain and merge it with my other scripts.
by kl at
We had discovered this issue of Google Analytics load time effecting our JavaScript from executing; GA was holding back on DOMReady event. We were using YUI, so I just wrote a convenience wrapper the YAHOO.util.Get.script function which made the loading of ga.js asynchronous.

Back in October 2008, I decided to re-write my little utility and make it work with YUI 3, YUI 2.7.0, and jQuery 1.3.

Since then my blog has received a majority of it's search keyword traffic from people searching for a solution to this, it's pretty evident that other developers are effected by a synchronous loading of Google Analytics and want to take control of it. It's great to see this being baked into Dojo and if people are using other libraries (jQuery or YUI) my utility should help them solve this issue.

Here is jQuery alternative:

var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");

jQuery.getScript(gaJsHost + "", function(){ _gat._getTracker("UA-8245545-1")._trackPageview(); });


Since people are claiming prior art, might as well include mine with the bunch (circa 2007):

Obviously, library specific code would be more elegant.

One wonders why the Google Analytics team doesn't fix their example code though. Web stats are a tricky beast.


Wait? We have a right hand?


by alex at
Is there a chance that you could "miss" a track though? If a user quickly moved off the page before the script had been fully downloaded?

The (small) benefit of the original script is that missing a track is unlikely because the page is forced to wait?

by Neil at
Awhile back, I tried to not use the document.write method to place ga.js but it didn't like to cooperate. Now I just use the dojo way, so all is good and well.
by David at

Sure, you could miss it, but I'm not sure I'd call it a "visit" if the user never even got to the point where all the resources loaded. I'd hope that the goal of all content is to provide value to the user first and to the publisher second, so by that rubric, missing a couple of clicks on analysis is no big loss if the user gets a better experience. I can see valid reasons for your personal calculus being different, though. I'm only tracking a personal blog, after all.


by alex at
That's great, but come on, Alex, you work at Google, now, make it happen, get them to fix their script. :)
If you put the tag with GA at the end of your HTML, the page will show up without waiting for the script to load, so all this seems fairly pointless.
by Pies at

The problem is that while that will, indeed, get most of the content to render before requesting ga.js, it holds up the visual indicators of "doneness" in the browser. Further, it causes any scripts that expect to be run onload to be significantly delayed. Depending on the site, the UI consequences of this lag can be mild or severe. I'd just prefer not to have to deal with it at all.


by alex at
I'd guess one reason people don't play with analytics' tracking code is because they're worried it might affect the tracking status.

i.e. Google's "Instructions for adding tracking" don't make it clear how important it is to stick to the example tracking code - if at all.

there are any method for prototype? thanks in advanced ;)
by Alvin at
Out of curiosity, why is the "before" example loading a gzipped ga.js, (9K), and the after loads a non-compressed one (23K)? If that's inherent to the way works, it should probably be fixed, or in some cases (slow connection) you'll loose more than you gain.
by Barak at
Google seems to have a problem with compressing the text/javascript mimetype. You can make google use gzip compression by changing the type to application/javascript in all of the script code. I also suggest wrapping your type= to double quotes in the document.write, since single quotes in HTML are a bad idea.

Someone should tell google to turn on gzip compression for the text/javascript mime type, or to switch the suggested code to application/javascript. It would probably save them terabytes per month in traffic and make peoples sites go faster :)

by Chris Stephens at

Why not do what "kl" suggested doing (near the top of the comments section)? Are there any drawbacks?