Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncaught ReferenceError: _gaq is not defined (Google Analytics)

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.

like image 999
Alex Brown Avatar asked May 01 '12 17:05

Alex Brown


4 Answers

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.

like image 124
PJ Brunet Avatar answered Nov 19 '22 14:11

PJ Brunet


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>
like image 16
Spongeboy Avatar answered Nov 19 '22 15:11

Spongeboy


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']);
like image 11
kaleazy Avatar answered Nov 19 '22 14:11

kaleazy


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'
});
like image 4
Ouadie Avatar answered Nov 19 '22 14:11

Ouadie