Josh Earl
Josh Earl

Reputation: 18361

Google Analytics events fail to show up

I'm trying to get Google Analytics working on my blog, but the events are failing to show up in my account.

Part of the issue is that I'm using the new async code, but most of the documentation relates to the older synchronous version. I've read a bunch of SO and blog posts but can't quite see what I'm doing wrong.

One mentions that you have to "activate" event tracking in your site's profile, but I can't find where I might do that.

I originally put my Google Analytics code in an external file called ga.events.js, which is still linked from my site. I attached the events from a jQuery loaded event:

$(function () {
  $('.rss').click(function() {
    trackEvent("engagement", "rss subscribe", "rss subscription link clicks");
  });

  function trackEvent(category, action, label) {
    _gaq.push(['_trackEvent', category, action, label]);
  }
});

But I found a post that said you can't link in an external file for Google Analytics, so I also tried the old fashioned onclick approach:

<a href="http://forms.aweber.com/form/08/728505808.htm" onclick="_gaq.push(['_trackEvent', 'Engagement', 'Click', 'Mailing list subscribe']);" target="_blank">email list</a>

I put the _target="blank" attribute in case the request wasn't completing before the user navigated away from the page.

Executing the code in the Chrome console on my site returns a 0, when I was expecting a boolean value:

_gaq.push(['_trackEvent', 'Engagement', 'click', 'RSS subscription link'])

I've waited 24 hours after each of these tests to see if the event tracking wasn't real time.

What else should I try? Any obvious mistakes?

Upvotes: 2

Views: 3097

Answers (1)

Josh Earl
Josh Earl

Reputation: 18361

Figured it out. I had onclick instead of onClick with a capital C. Wait, JavaScript is a case sensitive language? Who knew?

I also had the syntax of the GA call incorrect.

Here's what my working code looks like:

<a href="http://forms.aweber.com/form/08/728505808.htm" onClick="_gaq.push(['_trackEvent', 'Engagement', 'Post footer click', 'Mailing list subscription',, false]);" target="_blank">email list</a>

And here's a version that works using jQuery to attach the click handlers:

$(function () {
  $('.rss').click(function() {
    trackEvent('Engagement', 'Sidebar link click', 'RSS feed subscription');
  });

  function trackEvent(category, action, label) {
    _gaq.push(['_trackEvent', category, action, label,, false]);
  }
});

Upvotes: 3

Related Questions