I’m dissapointed

Surrogate injection does not mimic tracker behaviour (IMHO it should)

I’m doing a convenience wrapper around google analytics for tracking downloads and _blank hits. We use ga.js for now as the client does not want to upgrade yet. The problem is that to properly track a hit I push a function to _gaq so that navigating happens after the event is logged to GA.

Surrogates are a great idea but I think that the implementation should mock the target fully. What happens now is that external and asset links will not work for my client as the function pushed to _gaq never gets executed, consider:

if ( !isNewTab && window._gat && window._gat._getTracker ) { 
e.preventDefault();
// Pushing a function to _gaq ensures that it gets opened after tracking it
_gaq.push(function () {
window.location.href = url;
});
};

This approach is taken from StackOverflow so probably I’m not the only one that would be affected. Setting a timer to navigate seems really inefficient and ugly to me. I would rather check if I’m dealing with a surrogate or not or expect the surrogate to actually execute the code I’m sending.

After some digging I found out that the surrogate implementation is:

function () {
try {
if (arguments[0][0]== '_link') window.location.href = arguments[0][1]
} catch(er) {}
}

So if I do _gaq.push(['_link', url]) it will work (given that Ghostery blocks ga.js).
If I do the same with ga.js loaded properly it just logs a hit.

Still, this is inconsistent.
1 person likes
this idea
+1
Reply
  • Hi Piotr, thanks for posting. We think that developers should avoid creating and tying functionality to third party loaded scripts, especially for unrelated functions. Surrogates are there to prevent major site breakage for a given user, not to substitute or catch functionality tied into the tracker.
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. sad, anxious, confused, frustrated indifferent, undecided, unconcerned happy, confident, thankful, excited kidding, amused, unsure, silly

  • That's why I check if _gat is accessible - to safeguard against blocking. Setting a timer for window.location.href = url would be even more unreliable as it would corrupt hits on slow connections.

    Usually I wouldn't matter for me but in this case my client expects code that will work. I see that surrogates make sense and fix major breakage but then it's not too hard to distinguish the surrogate from the real thing given that you do some poking at it. Go a step further and in time if a site values breakage over a lack of tracking it will make sure it breaks.

    Given that, at least some basic API should be recreated in my opinion. Especially when you implement _gaq.push(['_link', url]) (which is not the standard way AFAIK of dealing with redirecting links).

    I had to make a special case just for the surrogate to get my stuff working and that's not nice when you consider developers oblivious of Ghostery. Here it is if you're interested.
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. sad, anxious, confused, frustrated indifferent, undecided, unconcerned happy, confident, thankful, excited kidding, amused, unsure, silly