Uncaught ReferenceError: _gaq is not defined (Goog

2019-03-11 16:12发布

问题:

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.

回答1:

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.



回答2:

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>


回答3:

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


回答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'
});


回答5:

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>