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?
javascript google-analytics
meij57
source share