Uncaught ReferenceError: ga is not defined

I want know how many times people clicked on a particular button (should be very simple with Google analytics).
However, I had an “Uncaught ReferenceError: ga is not defined” error from google console and can’t find how to fix it.

I added this in the head:

     <script>

      (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
      (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
      m=s.getElementsByTagName(o)
      [0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
      })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

      ga('create', 'UA-XXXXXXXX-X', 'auto') ; 

      ga('send', 'pageview');

    </script>

And add an onclick event on the button by generating the code with this tool http://gaconfig.com/google-analytics-event-tracking/contact-form/ :

onclick="ga('send', 'event', { eventCategory: 'Book button', eventAction: 'Click', eventLabel: 'enquiry home page'});"

Then I set up the goals in google analytics, but still have this error in the console.

So I tried to add:

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

Before declaring the google analytics function, but it generates a second error so I just dropped it.

Does anyone know if the problem comes from the script I’m using? Or if it is from the onclick event code?

I created a simple HTML with the same JS and click handler and ran it in my local apache server, it just works fine. Don’t see any reference errors.

Make sure you don’t have AdBlocker or other software preventing tracking which might be blocking the GoogleAnalytics
`

 <script>

    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
    (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
    m=s.getElementsByTagName(o)
    [0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

    ga('create', 'UA-XXXXXXXX-X', 'auto') ; 

    ga('send', 'pageview');

  </script>

<input type= "button" value ="Click Me" onclick="ga('send', 'event', { eventCategory: 'Book button', eventAction: 'Click', eventLabel: 'enquiry home page'});"/>

The same problem occured in my WordPress website.
I had event tracking code in my contact form 7 additional field, but after I installed the plugin Monster Insights I had to delete the code in the Theme Options. Forgetting the code in my contact forms, I got this message too.

Read More:   Convert long/lat to pixel x/y on a given picture

So delete all tracking code if you start using this plugin.
Hope someone finds this information usefull..

Open a new incognito window. If you are logged in then the analytics code won’t be ran. Also instead of “ga” you need to use “__gaTracker” for some reason Monster Insights doesn’t use “ga” they change it to “__gaTracker”.

You settled this question already but I just wanted to add:

Make sure your browser is allowing sites to track you. If you disallow tracking, the Google Analytics JS file will not load and you may get this error

This is a follow on from all the others who had an issue with the Monster Insights WordPress plugin which renames ga to __gaTracker.

I didn’t want to hardcode __gaTracker in my JS (to send an event to GA) – in case someone later removed the MonsterInsights plugin and my script just stopped working.

Here is my code:

<script type="text/javascript">
    var ga = typeof ga === "undefined" && typeof __gaTracker !== "undefined" ? __gaTracker : ga;
    ga('send', 'event', 'Order', 'Received', 'My cool product name');
</script>

The first line creates a var called ga which is either the original ga object or the __gaTracker object if ga does not exist. Note: if ga and __gaTracker both don’t exist you will end up with the same error ga is not defined


The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Similar Posts