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!)?
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 - this link is outdated).
The method _setAllowAnchor
is described on the Google Reference Site.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With