Having issues with event tracking in Google Analytics? Read on…

July 29, 2009 at 10:40 pm
filed under Web, Web Analytics, Web Design
Tagged , , , , , ,

Note: feel free to ping me or leave a comment if you are having major issues!

Yesterday the event tracking on a site was not working, and someone thought it might be that I had missed a call to the ga.js file or some extra Javascript. My first thought was “This is why I hate Javascript.” That thought recurred throughout my investigation process, but I decided to save some people time and effort by posting a few things you could check before giving up and crying. My particular example was an onClick event, but the same things could apply to other situations.

  1. Check the JavaScript call. So you must understand that Javascript (which I shall now write as JS) can be the best and worst scripting language. Very powerful and can do cool things like animation (and your analytics tracking), but also a pain in that you must be very careful with syntax and punctuation. One easy way to see if your JS is creating errors is by installing the Web Developer’s Toolbar in Firefox, it will show you when there are JS errors and a myriad of other useful things.

    Example: onclick=”pageTracker._trackEvent(“Foo”,”Bar”,”JS”);” This will be wrong because you have double quotation marks instead of apostrophes within the parentheses. So the page will read it as “pageTracker.trackEvent(” which will not do anything. Fix it like this: onclick=”pageTracker._trackEvent(‘Foo’,'Bar’,'JS’);”.

  2. Is the variable actually pageTracker? In most cases, if you’re not changing the Google Analytics code that is on every page, it should be pageTracker. If the variable is named anything else, like movieTracker or pageCounter, then you need to fix the call in the onClick and change it to: onclick=”movieTracker._trackEvent(…);” This is the part of the code you want to pay attention to:

    try {
    var pageTracker = _gat._getTracker(“UA-xxxxxx-x”);
    pageTracker._trackPageview();
    } catch(err) {}

  3. Where your code at? This ended up being my problem. People recommend that the GA code should be inserted at the footer of the page to not slow down the loading time of a page. The catch is, the JS file/definition of the variable pageTracker may need to be at the top of the page for _trackEvent to do its thing. If you try moving the code with the ga.js file to the head of the page, it should help. Or it helped me anyway.

As much as I hate debugging JS, I think it has really helped me to have knowledge of how it works, so if this is all kinds of confusing, try working through the W3C tutorials.

Misc.

The Grok delivers!! I was tweeting with @TheGrok and I asked if there was any way he could post about web analytics for non-profits. Brendan Regan wrote an awesomely helpful post about it. I will probably comment more about it later, but still, very cool.

And for those who use Twitter, #wa is being deprecated in favor of #measure. See Web Analytics Demystified for an explanation.

Share:
  • email
  • Twitter
  • Digg
  • del.icio.us
  • Facebook
  • LinkedIn
  • Reddit
  • StumbleUpon
  • Yahoo! Buzz

Related posts:

4 comments

RSS / trackback

respond

 

  1. Having issues with event tracking in Google Analytics? Read on …- SFWEBDESIGN.com

    on July 30, 2009 at 1:04 am

    [...] the rest here:  Having issues with event tracking in Google Analytics? Read on … Tags: event, file-or-some, not-working, some-extra, the-event, the-ga-js, thought-it-might, [...]

  2. Rob

    on August 2, 2009 at 6:25 pm

    Hey Sarah,

    Yeah, JS can be a pain at times. I must use your tips and give event tracking a go though.

    Another very handy Firefox-based debugger is Firebug https://addons.mozilla.org/en-US/firefox/addon/1843 Does the same stuff as Web Developer’s Toolbar, but its CSS Inspector is much better and clearer.

  3. Phil

    on May 12, 2010 at 4:56 pm

    Hi Sarah,

    Thanks for the helpful article!

    I was having a similar problem with my event tracking. What eventually solved it for me, was putting pageTracker._initData(); just before the pageTracker._trackPageview(); method call.

    So like:
    pageTracker._initData();
    pageTracker._trackPageview();

    Apparently this does something?! Anyways, thought this could be useful for others who come across this.

    Cheers,
    Phil

  4. ps

    on June 19, 2010 at 4:15 am

    i came here looking for answers to my event tracking woes. i actually modified nothing, and maybe that was my problem? i notice in the line

    var pageTracker = _gat._getTracker(“UA-xxxxxx-x”);

    there are quotemarks, except when i changed them to single quote marks

    var pageTracker = _gat._getTracker(‘UA-xxxxxx-x’);

    then, my code seemed to work! umm… do you know why that might be?