Google Analytics Event Tracking

I am trying to implement a google event tracking api in an html5 player, but for some reason it does not want to work. That's what I'm doing:

var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-myid-1']); _gaq.push(['_trackPageview']); (function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com//u/ga_debug.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })(); if (typeof _gaq != undefined){ _gaq.push(['_trackEvent', 'krusty-player', eventName, 'demo', 1]); } 

I copied the code from the google documentation page, so I think this should be correct. In the developer console, I do not see the HTTP request when the code works, but when I do:

 console.log(_gaq.push(['_trackEvent', 'krusty-player', eventName])); 

I get a counter that goes up +1 for every time this part is called. There are no errors.

I also tried using:

 _trackEvent('krusty-player', eventName); 

This returns the error "_trackEvent not defined"

Any idea what is going on?

+4
source share
2 answers

Are you testing on a local host or intranet? For more information, see the Google Analytics GIF request that was not submitted .
TL; DR: A default GIF tracking request is not created for local servers.

Regarding _trackEvent is not defined : _trackEvent () is not a standalone function and requires calling through _gaq.push

 _gaq.push(['_trackEvent', category, action]) 

or through the pageTracker object (old style, non-asynchronous analytics)

 pageTracker._trackEvent(category, action) 
+6
source

Bind the _gaq.push action to the event.

Like:

 <span onClick="_gaq.push(['_trackEvent', EVENT_NAME, EVENT_ACTION, EVENT_DESCRIPTION]);">Click me</span> 

And rename the names of the headers with open / dynamic generated text, and it should work.

+1
source

All Articles