_gat is undefined: New Analytics Code

So Google has updated their analytics engine and added some new code for users to put on their sites.  That’s great, right?  It’s good to see a company releasing updates to a free product.  Not entirely.

It would seem that their new code doesn’t work 100% of the time and it leaves out about 75% of the hits and tracking for your site.

_gat is undefined

This is the error that shows up in Firebug, IE7’s error console and Safari.

This is happening because the script (ga.js) doesn’t have enough time to load before you try to feed it the tracking ID (UA-XXXX-XX) into it.

What’s the solution?

The solution is very simple.  Delay the code.

So change the code from this :

<script type="text/javascript">
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”));
</script>
<script type=”text/javascript”>
var pageTracker = _gat._getTracker(”UA-XXXXXX-X”);
pageTracker._trackPageview();
</script>

to this :

<script src="http://www.google-analytics.com/ga.js" type="text/javascript"></script>
<script type=”text/javascript”>
var pageTracker;
setTimeout(’startGA();’, 500);
function startGA()
{
pageTracker = _gat._getTracker(”UA-XXXXXX-X”);
pageTracker._initData();
pageTracker._trackPageview();
}
</script>

So how does this fix your problem?  We create a new function called startGA (Start Google Analytics) and as you can see we have set a timeout of 500ms or 1/2 second. This gives the script enough time to run before you try to feed it variables and use it.

You should also never use the document.write function. The W3C compatibility rules pretty much said it was the anti-christ of code.

If you have any questions about the proper implimentation of the google analytics code.  Please do not hesitate to ask us.

6 Responses to “_gat is undefined: New Analytics Code”

  1. Jeff York Says:

    Fix worked like a champ!

  2. Toronto Graphic Design Says:

    It’s all a matter of tweaking your settings. The number 500 is basically 500 miliseconds. I’ve upped that amount to 800ms and this may alleviate your problem Chris. It all depends on the size of the page to the speed of the users’ connection to the site, to how fast my host (bluehost.com) can push through the desired content.

    Jeff : Great to hear this solution fixed your problem. You still haven’t added it to your site’s homepage which is still receiving the error.

  3. ioannis cherouvim » Blog Archive » Best way to integrate Google Analytics Says:

    [...] This is the best solution to integrate Google Analytics into your site. It uses ideas and code from the following 2 sites: http://www.mattiasgeniar.be/webdevelopment/_gat-is-not-defined-google-analytics-error/ http://www.maifith.com/news/_gat-is-undefined-new-analytics-code [...]

  4. Mattias Geniar Says:

    The problem could also be caused by some AdBlockers (such as Firefox’s AdBlock Plus addon) that’s preventing the javascript-file from being downloaded, and thus executing code that hasn’t fully loaded. A solution to that issue can be found here; http://www.mattiasgeniar.be/webdevelopment/_gat-is-not-defined-google-analytics-error/

    But since AdBlock is a widely used Firefox Extension, this would mean that (depending on your amount of firefox visitors) a large amount of your visitors aren’t tracked in Google Analytics. Disturbing, to say the least.

  5. Toronto Graphic Design Says:

    Mattias, while what you say is possible, this would also prevent any javascript based analytic solution from working. I am confident that with a 500ms or 800ms lapse in time, the JS would load from google and there wouldn’t be any need for the (if (typeof(_gat) == ‘object’) ) fix you propose on your site.

  6. Steve Lieu Says:

    You guys are very knowledgable. Thanks for the quick fix. I really appreciate it.

Leave a Reply