Skip to content Skip to sidebar Skip to footer

Javascript/jquery: How To Make Sure Cross-domain Click Tracking Event Succeeds Before The User Leaves The Page?

I'm implementing click tracking from various pages in our corporate intranet in order to add some sorely needed crowd-sourced popular link features ('most popular links in your dep

Solution 1:

I would try to return false from the link event handler, remember the URL and navigate away only when JSONP request succeeds. Hopefully it shouldn't add too much latency. Considering you are on the inranet, it might be OK.

Solution 2:

I would try a different approach. You can bind to a different event like:

$(window).unload(function(event) {
  // tracking code here
});

Solution 3:

Solved!

The short answer is: there is no reliable way to do this cross-domain with a GET request. I tried all sorts, including storing the event and trying to replay the event later, and all manner of hacks to try to get that to work.

I then tried tight loops, and they weren't reliable either.

Finally, I just gave in and used a dynamically created form that POSTed the results, with the target set to a hidden iFrame.

That works reliably -- it seems the browser pauses to finish its POST request before moving on, and ASP honours the POST. Turns out it's not 'clunky' at all. Sure, due to the browser security model I can't see the result... but it doesn't matter in this case.

I am now kicking myself that I didn't try that option first.

Post a Comment for "Javascript/jquery: How To Make Sure Cross-domain Click Tracking Event Succeeds Before The User Leaves The Page?"