After reading a blog post, I got the idea to add safety code to ensure that the Google Analytics objects are fully loaded before calling it's functions.
Typicle Google Analytics code goes something like:
var pageTracker = _gat._getTracker("X-UAXXXXX");
pageTracker._trackPageview();
and
pageTracker._addItem( bla bla );
pageTracker._trackTrans();
I have thought of two options to double ensure that the _gat-object is loaded before use:
1) Use JQuery.ready to call the _get-functions. Something like:
$(document).ready(function() {
var pageTracker = _gat._getTracker("X-UAXXXXX");
pageTracker._trackPageview();
}
or
2) Use JavaScript timeout
function checkGat() {
if( gat_is_ready ) {
var pageTracker = _gat._getTracker("X-UAXXXXX");
pageTracker._trackPageview();
} else {
setTimeout('checkGat()', 1000);
}
}
checkGat()
What is the better solution? Why? and any additional comments?