I am using jquery to log clicks on anchors to google analytics.
I have code like the following, that wires up the event:
$(document).delegate("a[rel='mylink']", "Click", function (e) {
//log to google analytics
console.log('tracking link');
_gaq.push(event);
e.preventDefault();
});
The problem is that the href seems to be followed too quickly for this event to fire.
Is there a way to delay the following of the href until the event has been fired/completed?
I can see in the console that it gets as far as ‘tracking link’ but then it moves on before the google analytics push event completes.
Any advice appreciated.
Google Analytics records data by making requests for a tracking pixel. As you’ve found, the request can be canceled when a new page is opened in the same window.
Note: a delay is only necessary if the link being opened in the same window. I use some variation of the following for a click function: