vpx

How-To Solve: “_gat is not defined”, a google analytics workaround

Posted in web by sebastian on June 6, 2008

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

14 Responses

Subscribe to comments with RSS.

  1. Meir said, on June 23, 2008 at 7:28 am

    Thanks Sebastian, I was integrating GA with ExpressionEngine but this took care of it anyway.

    All the best

  2. sebastian said, on June 30, 2008 at 8:13 am

    You are welcome. Thanks for your comment.

  3. Martin said, on June 30, 2008 at 8:56 pm

    Thank you for great help :-)
    I was wondering how could Google publish buggy code for Analytics…

  4. Sue said, on August 7, 2008 at 12:51 am

    Thank you very much for posting this!

  5. Veer said, on August 8, 2008 at 6:31 am

    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);
    }
    }

  6. Raj Parmar said, on September 15, 2008 at 2:50 am

    Veer’s code did it for me! Cheers fella!

  7. sebastian said, on September 15, 2008 at 9:07 am

    thanks VEER, great addition.

  8. Susokary said, on October 9, 2008 at 9:51 am

    @ Sebastian ‘n Veer :
    Thanks a lot bro’, really helpfull ! ;]

  9. Alan Riaso said, on October 23, 2008 at 7:41 am

    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.

  10. sebastian said, on October 23, 2008 at 10:25 am

    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 ;)

  11. Subhapriyo said, on November 8, 2008 at 8:25 am

    Thanks Veer :) …….. Thank u very very very much………… your code worked superbly………….. Once again thanks………….

  12. mudar said, on December 9, 2008 at 9:11 pm

    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!

  13. Dennis Mox said, on January 10, 2009 at 7:17 pm

    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.

    • sebastian said, on January 10, 2009 at 10:37 pm

      thanks dennis. everyday this post sees around 50 hits, so there is an audience for all your hints’n'tricks.


Leave a Reply