views:

1767

answers:

5

I'm trying to track clicks via Google Analytics that do not result in a new request. Specifically, clicks on tabs that are created via the jQuery UI tabs widget. I'm using the older version of the code ('urchin tracker') and trying to log the clicks like so:

$('.ui-tabs-nav li a').click(function() {
    val = "/tab/" + $(this).attr('href');

    // when uncommented, the following line reports, for example:
    //   /tab/#main
    // as expected.
    // console.log(val);

    res = urchinTracker(val);
});

The same method works, in another instance, whose only significant difference, as far as I can tell, is the lack of a hash (#) symbol in the string. Is that character not allowed in a string tracked by urchinTracker(), or could there be some other cause (other than no-one having clicked on the links!)?

+3  A: 

In short, Google Analytics can't track on-page links containing a '#' character. A click on index.html#foo is treated simply as a click on index.html, with everything after the '#' ignored.

You could consider escaping the '#' character:

$('.ui-tabs-nav li a').click(function() {
    val = new String("/tab/" + $(this).attr('href')).replace('#', '&');

    // when uncommented, the following line reports, for example:
    //   /tab/#main
    // as expected.
    // console.log(val);

    res = urchinTracker(val);
});

In your example, this would record a page view for /tab/?main, which should work fine with Google Analytics.

Rob Knight
Thanks for the suggestion. In actual fact, I'm testing a similar approach in which I simply call .substring(1) to drop the leading '#'. If this works (of course, it takes a while to confirm this!) I'll accept your answer.
Bobby Jack
Yup, that definitely seems to be it. Have upvoted your answer.
Bobby Jack
BTW, how do you know this? I couldn't find that info on Google's site anywhere. Obviously, it makes a bit of sense that it would work that way, but I would have thought the script could differentiate between an implicit tracking hit containing a fragment identifier, and an explicit call to the tracking function.
Bobby Jack
I'm not entirely sure how I know - where I work, we do a lot of work with Google Analytics and Website Optimizer, and I've never seen fragment identifiers tracked in any example code or reference guide. When answering this question I had a good look around the web to see if I could find a counter-example, but couldn't find one.It makes sense that fragment identifiers aren't tracked though; *all* calls to the tracker script are explicit calls, so there's no basis for making a distinction. And if you're making the call in your own script, you have the option of escaping the character.
Rob Knight
Sure - by "explicit", I meant whether an argument is passed to urchinTracker() or not. Surely that function can base its decision on whether an explicit string was provided or not. That way, clients wouldn't have to worry about any disallowed characters (I wonder if there are any others, for example).
Bobby Jack
+4  A: 

By default, Google Analytics disables tracking anchor tags. To enable it for the legacy tracking code, use the following form:

_uanchor = 1;
urchinTracker(val);

Setting a value to variable _uanchor is the equivalent of calling method _setAllowAnchor when using the latest GA code base. (You find a detailed comparison in the Google Analytics Tracking Code Migration Guide).

Török Gábor
A: 

Sounds like you want Google Analytics Event Tracking the examples use onclick="" rubbish but I bet you could probably bind these to jQuery click events as well.

Tom
A: 
  1. I tried using the _uanchor = 1; method. That does not work. The reason it does not work is that it is designed to send the google analtyics parameters as hash values, not send your hash value.

From the guide quoted above, setting _uanchor to one has the following effects: "When enabled, uses # instead of the default ? to separate the request stem from the query string"; this is in the 'Campaign Tracking' section.

  1. The number 2 answer works fine, but I made a small modification. In pseudo code:

    if (href contains ?) { href = href.replace('#', '&'); } else { href = href.replace('#', '?'); }

Not sure if it really matters to GA to have a url like foo.html&bar=baz but it just seemed cleaner to me.

mooreds
A: 

This may be a dumb question, but where should this code be added? With the Google Analytics code or with the JQuary Call?

Ben-Jamin