Reputation: 803
I'm trying to get an event when I'm pressing a button but I can't see any clue of my activity on my Google Analytics page. I followed the Google Analytics tutorial in this page https://developers.google.com/analytics/devguides/collection/analyticsjs/events
Following that instructions I've created a button with the id "button":
<a id="button"><btn class="btn btn-default">Button</btn></a></p>
then I have included in the head of the document the main Google Analytics call to connect my page to the right database (I've changed UA-XXXXXXXX-X', 'myname.com with my current settings)
<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', 'myname.com'); //create a tracker
ga('send', 'pageview'); //send a pageview
</script>
and lastly I paste that code on the bottom of the page that should track the event:
<script>
var downloadLink = document.getElementById('button');
addListener(downloadLink, 'click', function() {
ga('send', 'test', 'button', 'click', 'nav-buttons');
console.log("clicked and sent (hopefully)");
});
/**
* Utility to wrap the different behaviors between W3C-compliant browsers
* and IE when adding event handlers.
*
* @param {Object} element Object on which to attach the event listener.
* @param {string} type A string representing the event type to listen for
* (e.g. load, click, etc.).
* @param {function()} callback The function that receives the notification.
*/
function addListener(element, type, callback) {
if (element.addEventListener) element.addEventListener(type, callback);
else if (element.attachEvent) element.attachEvent('on' + type, callback);
}
</script>
It looks right to me: the id is "button" and it should trigger the call to the event. But it's not working! I don't receive anything from Google's dashboard so I can't understand what's wrong with my jJavascript.
Upvotes: 0
Views: 436
Reputation: 2269
Change
ga('send', 'test', 'button', 'click', 'nav-buttons');
to
ga('send', 'event', 'button', 'click', 'nav-buttons');
Upvotes: 2