Uncaught ReferenceError: ga undefined

I want to know how many times people clicked on a certain button (it should be very simple with Google analytics). However, I had a "Uncaught ReferenceError: ga is not defined" error from the Google console and could not find how to fix it.

I added this to my 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 the onclick event to the button, creating 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 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 deleted it.

Does anyone know if a problem arises from the script I'm using? Or if it is from the onclick event code?

+8
javascript google-analytics
source share
3 answers

I created simple HTML with the same JS and click handler and ran it on the local apache server, it just works fine. Make no mistakes.

Make sure you don’t have AdBlocker or other anti-tracking software that could block 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'});"/> 
+15
source share

The same problem arose on my Wordpress website. I had an event tracking code in the field of my contact field 7, but after installing the Monster Insights plugin, I had to remove the code in the theme settings. Having forgotten the code in my contact forms, I also received this message.

So remove all tracking code if you start using this plugin. Hope someone finds this information helpful.

+3
source share

You already solved this question, but I just wanted to add:

Make sure your browser allows sites to track you. If you have disabled tracking, the Google Analytics JS file does not load and you may receive this error.

0
source share

All Articles