Uncaught ReferenceError: _gaq is not defined (Google Analytics)
Asked Answered
A

5

33

The following message appears when viewing a site page in the chrome debug log.

Uncaught ReferenceError: _gaq is not defined

The page itself is supposed to track an object using the onload event handler and fire a _trackEvent for Google Analytics.

My best guess is that perhaps the ga.js file doesn't load in time and therefore the onload _trackEvent triggered is not caught. the async snippet is being used before the </body> close and the object is positioned in the middle <body>.

(some other posts have referenced jQuery position too but, this might be a red herring)

any assistance greatly appreciated.

Alacrity answered 1/5, 2012 at 17:49 Comment(0)
P
34

From https://developers.google.com/analytics/devguides/collection/gajs/ this code replaces your existing "traditional snippet" with the "latest, asynchronous version, you should remove the existing tracking snippet first."

<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-XXXXX-X']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>

To see your events come in (to know if this is working) look for "Events" under the "Real-Time" menu option on the left side of the "Reporting" page.

Prickly answered 10/3, 2014 at 18:14 Comment(1)
this is a much better answer and contains an encapsulated solution - i always am grateful when context is not left to the imagination.Prestigious
F
16

Regarding the position of the async snippet, the GA help page says -

Paste this snippet into your website template page so that it appears before the closing </head> tag.

My first thought was that JS should be loaded at the bottom of the page to improve page speed. However, the GA async tracking snippet should be loaded in the head, as it will not load the ga.js immediately, and it won't block page execution.(It does this by dynamically adding the script tag to the DOM, which puts it at the back of the queue.)

If, for some reason, you can't move the async snippet to the head, you can define _gaq yourself, like this-

<button onclick="var _gaq = _gaq || []; _gaq.push(['_trackEvent', 'button3', 'clicked'])"/><button>
Fireworks answered 28/2, 2013 at 23:41 Comment(1)
This was exactly my problem. I moved the tracking code to the footer and didn't realize it was loaded using async even.. Thanks for the tip!Ja
G
11

Had the same problem. You have to define the _gaq array. Just add this after your Google Analytics script in the header:

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXX-X']);
_gaq.push(['_trackPageview']);
Gustavogustavus answered 23/4, 2013 at 19:46 Comment(0)
A
4

You can use the last version of analytics.js instead of ga.js

ga.js is a legacy library. If you are starting a new implementation we recommend you use the latest version of this library, analytics.js. For exisiting implementations, learn how to migrate from ga.js to analytics.js.

Here is an example:

ga('send', {
  hitType: 'event',
  eventCategory: 'Video',
  eventAction: 'play',
  eventLabel: 'cats.mp4'
});
Abash answered 21/11, 2015 at 17:47 Comment(1)
This worked for me. I wouldn't change to other solutions. Since my version of ga doesn't use _gaqGangue
T
0

Change the Tracking Code to:

<script type="text/javascript">
      var gaq; 
      var _gaq = gaq || [];
      _gaq.push(['_setAccount', 'UA-XXXXX-X']);
      _gaq.push(['_setDomainName', 'yourdomain.com']);
      _gaq.push(['_setAllowLinker', true]);
       _gaq.push(['_trackPageview']);

      (function() {
        var ga = document.createElement('script');
        ga.type = 'text/javascript';
        ga.async = true;
        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www')
                      + '.google-analytics.com/ga.js';
        var s = document.getElementsByTagName('script')[0];
        s.parentNode.insertBefore(ga, s);
      })();

</script>
Taction answered 9/5, 2014 at 15:17 Comment(3)
I don't quite see how this will work. Won't _gaq always be set to the gaq variable you just initiated in the first line?Savil
This ensures a clean start to a new GA. If you want to use another existing _gaq, you may. var _gaq = _gag will reference it (if the existing one is an array all will be ok. if it is not an array you will get an error). in the code above, _gaq although referenced to gaq (new variable) will be an array. check it out.Taction
Since you know gaq is undefined, your first two lines can be combined into _gaq = []. If you are trying to get an existing array or set up a new array, then you would use the traditional _gaq = _gaq || []. If _gaq does already exist, you just killed it. Line two is effectively _gaq = undefined || new Array, in which case, it will always be set to equal an array.Savil

© 2022 - 2024 — McMap. All rights reserved.