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 + "' type='text/javascript'%3E%3C/script%3E"));
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-XXXXXX-X");

to this :

<script src="" type="text/javascript"></script>
<script type="text/javascript">
var pageTracker;
setTimeout('startGA();', 500);
function startGA()
pageTracker = _gat._getTracker("UA-XXXXXX-X");

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.

  1. Fix worked like a champ!

  2. 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 ( 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. 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;

    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.

  4. 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.

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

    Steve Lieu
  6. Finally, a solution that works in IE6! Thanks for this!

  7. 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.

    avant garde
  8. 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!

  9. 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.?


  10. Thanks a lot, your code fixed the issue in IE.

  11. Simply Excelent! You solved my problem

  12. 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

    • Hi Mikul

      It would appear that you forgot to include the original tracker code in your site.

      Dont forget this bit >>

      <script src="" type="text/javascript"></script>
  13. 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?

    • 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.

  14. This worked for me

    var pageTracker;
    function startGA(num_tries) {
    try {
    pageTracker = _gat._getTracker(“XX-XXXXXX-X”);
    } catch(err) {
    if (num_tries < 10) {
    setTimeout('startGA(' + (num_tries+1) + ');', 500);

  15. I’ve tried **everything** you guys have suggested. No luck.

  16. 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

  17. Worked Brians method 🙂

  18. Beautiful solution!!

  19. Well, it “works” in that in IE 7 and IE8 it certainly stops the JS error BUT what adverse affect would it have? For example, how might it interfere with Event Tracking? As well, the speculation that you might be losing traffic data #s since the JS file never loads, is that proven, or just a natural guess? Also, how in 2010 is this still an issue with GA code? As well, this is a 2008-2009 topic, is it still relevant?

    I see both sides of this question – older code seems to fix .JS error but does it really then also improve “possible” stats loss #’s “maybe” caused by newer GA code OR is the newest GA code provided just fine and the .JS error is not to be worried about and it does not affect traffic #’s..