meij57
meij57

Reputation: 271

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?

Upvotes: 27

Views: 98438

Answers (6)

Jay Brunet
Jay Brunet

Reputation: 3750

If you're no longer using Google Analytics, it's possible you accidentally left a ga("send", "pageview"); in your code.

If you're not sure, in your theme directory try grep -r "ga(" *

If you actually want Google Analytics, I'm thinking you called the ga() function before it was defined, or for some reason Google Analytics is not loading at all.

Upvotes: 1

Dagmar
Dagmar

Reputation: 3281

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

Upvotes: 2

Mav2287
Mav2287

Reputation: 876

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

Upvotes: 4

jordy
jordy

Reputation: 33

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

Upvotes: 3

user7305940
user7305940

Reputation: 41

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.

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

Upvotes: 4

kiran
kiran

Reputation: 384

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'});"/>

Upvotes: 22

Related Questions