JavaScript / jQuery: How do I ensure that the cross-domain tracking event is successful before the user leaves the page?

I am implementing click tracking from different pages on our corporate intranet to add some much-needed popular links to popular sources ("the most popular links in your department in the last 24 hours", etc.)

I use jQuery.live () to bind to the mousedown event for all link elements on the page, filter the event, and then use a pseudo-ajax request with various data to the internal server before returning true to trigger the link action:

$("#contentarea a").live("mousedown", function(ev) { // // detect event, find closest link, process it here // $.ajax({ url: 'my-url', cache: false, dataType: 'jsonp', jsonp: 'cb', data: myDataString, success: function() { // silence is golden -- server does send success JSONP but // regardless of success or failure, we allow the user to continue } }); return true; // allow event to continue, user leaves the page. } 

As you can guess from the above, I have several limitations:

  • The internal tracking server is on a different subdomain from the calling page. I can not get around this. This is why I use JSONP (and GET), unlike AJAX itself with POST. I cannot implement AJAX proxies because web servers do not have outgoing network access for scripts.
  • This is probably not relevant, but in the interest of full disclosure, the content and script are inside the "iframe" of the main content (and this will not change. Most likely, eventually move the event listener to the parent frame to track its links and all the child content, but step 1 makes it work correctly in the simplified case of “1 child window”). Parent and child are the same domain.
  • The back-end is IIS / ASP (again, the limitation is do not ask!), So I can’t immediately unlock the background process or otherwise stop the answer, but continue to process as I could on the best platform

Despite this, for the most part, the system works - I click the links on the page and they appear in the database quite easily.

However, it is not reliable - for a large number of links, especially links outside the site, for which their purpose is set to "_top", they are not displayed. If the link is opened in a new tab or window, it registers OK.

I excluded script errors - it seems like either:

(a) the request never makes it on time; or

(b) the request does this, but ASP detects that the client disconnects shortly afterwards and, because it is a GET request, does not process it.

I suspect (b), since the server timeout is very fast, and many links are registered OK. If I turned on the warning popup after the event is triggered, or set the return value to false, the click is registered in order.

Any advice on how I can solve this (in the sense that I cannot change my limitations)? I cannot make the GET request synchronous, as this is not AJAX.

Q : Will it work better if I make a POST request for ASP? If (b) is the culprit, will he behave differently for POST and GET? If so, I can use the iframe / form hidden form to submit the data. however, I suspect this will be slower and more awkward, and may still not be in time. I could not listen if the request is completed because it is cross-domain.

Q Can I just add a delay to the script after the GET request is sent? How to do it in a single-threaded way? I need to return true from my function to ensure that the default event will ultimately fire, so I cannot use setTimeout (). Will a tight loop wait for “success” to shoot and set some variable work? I worry that this will freeze too much and the response will be slowed down. I assume jQuery delay () plugin is also a loop?

Or something else I did not think about being the culprit?

I don't need bulletproof reliability. If all links are equally attractive in 95% of cases, this is normal. However, now some links are 100% comprehensible, while others are not available - which is not going to shorten it for what I want to achieve.

Thanks in advance.

+1
javascript jquery cross-domain asp-classic mouseclick-event
source share
3 answers

Solved!

Short answer: there is no reliable way to make this cross-domain with a GET request. I tried all sorts, including saving the event and trying to replay the event later, and all kinds of hacks to try to get this to work.

Then I tried hard loops, and they were also not reliable.

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

This works reliably - the browser seems to stop to complete its POST request before moving on, and ASP rewards the POST. Turns out it's not "awkward." Of course, due to the browser security model, I don’t see the result ... but in this case it does not matter.

Now I kick myself that at first I am not trying to use this option.

0
source share

I would try to return false from the link event handler, remember the URL and delete only when the JSONP request is successful. Hopefully he shouldn't add too much latency. Given that you are on the intranet, everything may be in order.

0
source share

I would try a different approach. You can bind to another event, for example:

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

All Articles