最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Google Analytics Event Tracking - Stack Overflow

programmeradmin3浏览0评论

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//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?

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.//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?

Share Improve this question asked Aug 29, 2012 at 13:00 tk66tk66 2847 silver badges21 bronze badges 7
  • It looks fine. Does the _trackPageview occurs correctly? – Tchoupi Commented Aug 29, 2012 at 13:09
  • @MathieuImbert no.. i don't see a request for this one as well.. – tk66 Commented Aug 29, 2012 at 13:17
  • What request are you looking for? It should be an image file called __utm.gif – Tchoupi Commented Aug 29, 2012 at 13:24
  • well i am clearing the console and no new request e in when i trigger the event. Either way i was searching for something like what is posted here: pranshuarya./jaal/Development/… – tk66 Commented Aug 29, 2012 at 13:26
  • It is very hard to debug with no access to the code. Is your website public? Can we see it? – Tchoupi Commented Aug 29, 2012 at 13:28
 |  Show 2 more ments

2 Answers 2

Reset to default 7

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)

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.

发布评论

评论列表(0)

  1. 暂无评论