How-To Solve: “_gat is not defined”, a google analytics workaround
Just a small JavaScript addition can solve a minor bug in Google’s Analytics Script. If anybody has a common AdBlocker installed and visits your Google Analytics enhanced page, she will notice 2 errors, including the famous: “_gat is not defined”. Looks like the google script does not validate if the actual tracking script is already loaded before it is executed.
As a quick work around add the following BOLD lines around your script:
<script type="text/javascript">
if (typeof(_gat) == 'object')
{
var pageTracker (YOUR TRACKING CODE)
...
}
</script>
ét voilá.
Thanks Sebastian, I was integrating GA with ExpressionEngine but this took care of it anyway.
All the best
You are welcome. Thanks for your comment.
Thank you for great help
I was wondering how could Google publish buggy code for Analytics…
Thank you very much for posting this!
I modified the code in this way, to allow for delayed downloads of the JS file.
var gaJsHost = ((“https:” == document.location.protocol) ? “https://ssl.” : “http://www.”);
document.write(unescape(“%3Cscript src=’” + gaJsHost + “google-analytics.com/ga.js’ type=’text/javascript’%3E%3C/script%3E”));
var hndl = window.setTimeout(“StartTracking()”, 100);
function StartTracking(){
if (typeof(_gat) == ‘object’)
{
window.clearTimeout(hndl);
var pageTracker =_gat._getTracker(” — YOUR GA CODE HERE –”);
pageTracker._initData();
pageTracker._trackPageview();
} else {
hndl = window.setTimeout(“StartTracking()”, 1000);
}
}
Veer’s code did it for me! Cheers fella!
thanks VEER, great addition.
@ Sebastian ‘n Veer :
Thanks a lot bro’, really helpfull ! ;]
Thanks Sebastian. It worked for me. Although I’m getting a “_gat is not defined” for your site.
http://i365.photobucket.com/albums/oo99/alanr1979/Untitled5.png
Maybe this is out of your control and is handled by the WordPress guys.
Thanks Alan. You are right – I personally don’t track anything here at wordpress (say: this blog is hosted at vpx.wordpress.com). But a good hint to the wordpress dev team
Thanks Veer
…….. Thank u very very very much………… your code worked superbly………….. Once again thanks………….
I had the same error message, and it was solved (strangely) by keeping the javascript code divided into 2 SCRIPT tags :
var gaJsHost = ((“https:” == document.location.protocol) ? “https://ssl.” : “http://www.”);
document.write(unescape(“%3Cscript src=’” + gaJsHost + “google-analytics.com/ga.js’ type=’text/javascript’%3E%3C/script%3E”));
var pageTracker = _gat._getTracker(“UA-2721574-3″);
pageTracker._initData();
pageTracker._trackPageview();
When doing my cut/paste, I thought it was silly to close the script then re-open, so I removed lines 4 & 5
putting them back solved the problem… I guess it changes sth for the execution time!
Veer, use of timeout does not make any sense. Browser won’t execute “_gat._getTracker” until the ga.js is fully loaded and processed anyway, that’s the rules. And if “ga.js” cannot be loaded due to this ad blocker or network error, then all you really need is typeof check and that’s it. Timeout does not do anything here.
thanks dennis. everyday this post sees around 50 hits, so there is an audience for all your hints’n'tricks.