Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Analytics Event Tracking

I am trying to implement the google event tracking api into a html5 player, but for some reason it doesn't want to work.. Here is what i am 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 guess it has to be right. From the Developer Console i don't see any HTTP request happening when the code runs, 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. No errors here..

i also tried using:

_trackEvent('krusty-player', eventName);

This returns error "_trackEvent is not defined"

Any idea what is going on?

like image 639
tk66 Avatar asked Aug 29 '12 13:08

tk66


People also ask

What is Event Tracking in Google Analytics?

Event tracking leverages a custom code snippet that you add to the elements that you want to track on your website. Whenever users interact with that element, the code tells Google Analytics to record the event.

How do I track events in Google Analytics 4?

When you add the Google Analytics 4 tag to your website, it will automatically track a number of events when someone views a page. For example, Google Analytics will automatically track an event when someone spends at least 10 seconds on your website.


2 Answers

Are you testing on localhost or an intranet? See Google Analytics GIF request not sent for details.
TLDR: the tracking GIF request doesn't get made for localhost servers by default.

Regarding _trackEvent is not defined: _trackEvent() isn't a stand alone function and needs to be called via _gaq.push

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

or via a pageTracker object (old style, non-async analytics)

pageTracker._trackEvent(category, action)
like image 98
mike Avatar answered Oct 03 '22 00:10

mike


Bind the _gaq.push action on an event.

Like :

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

And rename the caps names with plain / dynamic generated text and it should work.

like image 31
Cosmin Avatar answered Oct 03 '22 00:10

Cosmin