_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.
November 2nd, 2008 at 2:17 pm
Fix worked like a champ!
November 3rd, 2008 at 2:48 pm
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.
November 6th, 2008 at 4:18 pm
[...] 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 [...]
November 6th, 2008 at 6:46 pm
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.
November 10th, 2008 at 10:05 am
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.
December 9th, 2008 at 6:32 pm
You guys are very knowledgable. Thanks for the quick fix. I really appreciate it.
March 13th, 2009 at 8:57 am
Finally, a solution that works in IE6! Thanks for this!
April 22nd, 2009 at 1:21 pm
[...] developers here, we soon realized the problem was with Google Analytics. The page, it seems, is loading too fast! That seems to be the bulk of the issue, coupled with the fact that Internet Explorer 8 has been [...]
June 3rd, 2009 at 7:30 am
Verify that Active Scripting, ActiveX, and Java are not being blocked by Internet Explorer
Active scripting, ActiveX, and Java are all involved in shaping the way information displays on a Web page. If these are blocked on your computer, it may disrupt the display of the Web page. Reset the Internet Explorer security settings to make sure these are not blocked. To do this, follow these steps:
1. Start Internet Explorer.
2. On the Tools menu, click Internet Options.
3. In the Internet Options dialog box, click the Security tab.
4. Click Default Level.
5. Click OK.
Note ActiveX controls and Java programs are turned off at the High security level in Internet Explorer.
July 1st, 2009 at 12:50 am
Very cool, I have been messing with the code for about two days now and finnally saw this area during a searchfor _gat is undefined. After installing your new code with the delay it has totally fixed the problem! Thanks alot guys and good work!
September 14th, 2009 at 5:16 am
Hello,
This approach will work fine, but there may be loading issues depending up on the network speed or unnecessary delay if the ga.js already loaded.
Is there anyother way to do this?
How can we ensure that ga.js will be loaded in 500ms?.
If it is loading in 200ms or takes more than 500ms,Is there a dynamic way of doing this, I mean depending on the loading time.?
Thanks,
Raj
September 23rd, 2009 at 1:18 pm
Thanks a lot, your code fixed the issue in IE.
November 19th, 2009 at 5:41 am
Simply Excelent! You solved my problem
November 27th, 2009 at 10:45 am
hi there ,
i am getting error in IE 6 it’s say
pageTracker is null or not an object
can u please help me to solve this
December 3rd, 2009 at 6:18 pm
I am not an specialist in the computers. How I have to do to change the script? I have no clou. Can somebody help me?
December 4th, 2009 at 10:30 am
Hi Mikul
It would appear that you forgot to include the original tracker code in your site.
Dont forget this bit >>
December 4th, 2009 at 8:38 pm
Hi Soso, let me know what you need to change. If you just need to add in your own tracker code, replace the UA-XXXXXXX-XX number with the one that comes through from your Google Analytics account.
February 4th, 2010 at 1:31 pm
This worked for me
var pageTracker;
function startGA(num_tries) {
try {
pageTracker = _gat._getTracker(“XX-XXXXXX-X”);
pageTracker._trackPageview();
} catch(err) {
if (num_tries < 10) {
setTimeout('startGA(' + (num_tries+1) + ');', 500);
}
}
}
startGA(1);
February 14th, 2010 at 3:34 am
I’ve tried **everything** you guys have suggested. No luck.
March 4th, 2010 at 9:41 pm
Hey, I found this blog post while searching for help with JavaScript. I have recently switched browsers from Opera to IE. After the change I seem to have a problem with loading JavaScript. Every time I browse page that needs Javascript, my browser does not load and I get a “runtime error javascript.JSException: Unknown name”. I cannot seem to find out how to fix it. Any help is very appreciated! Thanks
April 5th, 2010 at 1:06 pm
Worked Brians method
June 13th, 2010 at 12:21 pm
Beautiful solution!!