views:

53

answers:

1

I have a website which is using Google Analytics newer asynchronous tracking method (_gaq). The problem I've run into is that I want to institute some specific link tracking and am worried that I will be creating a race condition.

Basically, it's a news website so it has headlines which link to stories all over the place. A headline for a story might appear in 3 different places on a page, and appear on hundreds of other pages. Thus, in order to understand how our audience is interacting with the site we have to track how each specific headline block is used, and not just the destination. Because of those two stipulations tracking individual pages, nor tracking referred pages won't be enough, we have to track individual links.

So if I have a link.

<a href="http://www.blah.com" onclick="_gaq.push('_trackEvent','stuff')">Here</a>

Because _gaq.push() is an asynchronous call, isn't it possible that the page change will occur prior to Google's completion of the click tracking? If so is there a way to prevent that, or do I have a misunderstanding about the way that Google Analytics Async functions (http://code.google.com/apis/analytics/docs/tracking/asyncUsageGuide.html).

A: 

You're right. If the browser leaves the page before it sends the GA tracking beacon (gif hit) for the event, the event will not be recorded. This is not new to the async code however, because the process of sending the tracking beacon is asynchronous; the old code worked the same way in that respect. If tracking is really that important, you could do something like this:

function track(link) {
  if (!_gat) return true;
  _gaq.push(['_trackEvent', 'stuff']);
  setTimeout(function() {location.href=link.href'}, 200);
  return false;
}

...

<a href="example.com" onclick="return track(this);"></a>

This will stop the browser from going to the next page when the link is clicked if GA has been loaded already (it's probably best to not make the user wait that long). Then it sends the event and waits 200 milliseconds to send the user to the href of the link they clicked on. This increases the likelihood that the event will be recorded. You can increase the likelihood even more by making the timeout longer, but that also may be hurting user-experience in the process. It's a balance you'll have to experiment with.

Brian
Shouldn't the `if(!_gat)` be `if(!_gaq)`?
yc
Nope. The goal of that check is to find out if GA has loaded yet. If you're using the async syntax, checking for _gaq won't tell you anything; you create it in your snippet code. When using async, _gat is only created when ga.js executes, so checking for it will tell you if tracking beacons will be sent without delay.
Brian
Theoretically, I could set the timeout to a fairly low number like 100ms. While imperfect, every millisecond of delay would mean that more clicks would be registered and less lost in the race. Unless there is a way for the _gaq.push() to return a value, and thus wait for that value, there really isn't an ideal solution it seems.
Owen Allen
This doesn't really have anything to do with _gaq.push or async. The same situation holds for the old GA syntax because callbacks on the tracking beacons aren't supported by GA. I really wish they were for this exact situation.
Brian