Comments for Ending The ga.js Wait
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. http://925html.com/code/non-blocking-google-analytics-integration/
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.
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
jQuery.getScript(gaJsHost + "google-analytics.com/ga.js", function(){ _gat._getTracker("UA-8245545-1")._trackPageview(); });
Source: http://www.reddit.com/r/javascript/comments/8bvx0/ending_the_gajs_wait/c08t4jy?context=1
http://www.zachleat.com/web/2007/11/01/speed-up-google-analytics-with-dynamic-includes/
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?
Regards
The (small) benefit of the original script is that missing a track is unlikely because the page is forced to wait?
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.
Regards
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.
Regards
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.
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 :)
Why not do what "kl" suggested doing (near the top of the comments section)? Are there any drawbacks?