TL;DR In short, every library has their own way of tracking users. You should handle every library differently and redirect when they all finished. Below, I've explained the different ways of tracking I could think of on the top of head. Especially, pay attention to the beacons part, I haven't seen it between the answers here, though it will be used more and more in the future I think.
This actually depends on the way other parties track the users. In general, I think there are three kind of ways to track a user. JavaScript, images and (maybe) iframes. Some methods allow you to check if the tracking is done. I'll try to explain the different methods. In short, per 3rd party you'll have to check if they finished tracking the user and redirect when all have finished (using window.location
).
1. Javascript
In JavaScript, tracking can be implemented in different ways. I can think of 4 different methods on the top of my head. I'll try to explain how you can track if everything is loaded and processed correctly.
1.1. Beacons
Beacons are not very well known. It is a new technology, which allows the browser to send small packets of data to the server before the page unloads. Those are the easiest to handle. At the moment the page has unloaded (so after the redirect is initiated) they can still send their final data to the server. In short, don't worry to much about this way.
(But be aware, this is not yet supported in every browser. If you want to read more about it, please see https://developer.mozilla.org/en-US/docs/Web/API/Navigator/sendBeacon . I'm not sure, but I thought Google Analytics was using this).
1.2. AJAX Requests
The AJAX requests are the most difficult. I see others have suggested $.ajaxComplete
from the jQuery library. It's worth a try, but I'm not sure if you can catch AJAX calls which are made from scripts that are loaded from a different domain. From the same domains should work, I've used this myself to keep track of the AJAX calls on a page. You could test this however. Take two domains. On the first domain, use jQuery to catch the AJAX calls. On the second domain, host a file which makes an AJAX call (without using jQuery). This way, you should be able to find out if this works.
Otherwise, you should check if the libraries have a callback method for their tracking methods. Once this callback is called, you can assume the tracking part has finished.
1.3. Append image
With JavaScript, it is also possible to append an image tag to the HTML. In short, see the section below about tracking images. But, you will need to write some small piece of JavaScript to detect the tracking pixel which is appended. I think most libraries will add something to the image so you can identify them with JS.
1.4. Append iframe
Of course, appending an iframe is also an option. I don't think it will be used that often in combination with JavaScript, but the idea should be the same as with the tracking pixels.
2. Images
Images should not be that difficult. If you place the images in the body yourself, add a certain class or ID and use jQuery's load
event. Again, I'm not sure this will always launch, but there are ways which can make you sure it does. For example, see this questions on SO: Check if an image is loaded (no errors) in JavaScript
For the tracking pixels append with JavaScript, if the always use a certain class, you write some JavaScript right after the <body>
tag. For example:
<body>
<script type="text/javascript">
$(document).on('load', '.trackingPixelClass', function () {
// increase counter and check if every tracking work is done
});
</script>
3. IFrames
IFrames should be the same as tracking if images are done. This should work, but I'm not a 100% certain what happens if the iframe loads content from another domain. Browsers might think this is a security risk.