I understand that by simply adding a script to the end of the body tag of a html document one makes it processable by Google analytics. My question is, is this likely to hav
Although downloading and running the actual ga.js is fast, what I've noticed all across Europe, on different connections/computers/OSes/browsers, is a MAJOR lag (anywhere from 0 to 30 (thirty) seconds) between the last byte of HTTP request and first byte of HTTP response.
This is understandable, given the immense popularity of GA, but this is happenning before window.onload fires. So, if your page relies on JS and your users hit this lag, they are not going to analyze which component is responsible - they'll assume your site is horribly slow.
A workaround for this is to register a window.onload function which will add the GA script. Example (using "window.onload=function()" for simplicity):
window.onload = function() {
var gaJsHost = (("https:" == document.location.protocol)
? "https://ssl."
: "http://www.");
var s = document.createElement('script');
s.src = gaJsHost + "google-analytics.com/ga.js";
s.type='text/javascript';
var bodies = document.getElementsByTagName('body');
if (bodies.length > 0) {
bodies[0].appendChild(s);
} else { // this should never happen, but sometimes does (curse you IE6!)
document.appendChild(s);
}
// this says 100ms, but won't happen until ga.js is loaded
window.setTimeout(function(){
if (window['_gat']) {
var pageTracker = _gat._getTracker("UA-xxxxxx-x");
pageTracker._trackPageview();
}
},100);
}