I’m not sure what happens when a page containing the asynchronous version of Google Analytics hasn’t fully loaded but the user either closes the browser or navigates to another page.
If the analytics doesn’t get recorded like I assume, then what methods are available to ensure that it does?
If it does, how does it work?
This is probably impossible. You can kind of reverse-engineer google analytics stuff by tracking the resources they add to the page, like this:
But the problem is that only gets you halfway there. You can check to see if it’s done by using
window.onunloadas @nidhin mentioned. However, since javascript is single-threaded, only one process can be taking place at a time. So, I see no way for you to block the user from exiting the page without also blockingga_script‘s ability to run in the background. Hence, you can check to see if Google has finished doing their thing, but you can’t actually make sure Google can finish.You could, however, send some info to your own server (the page could leave, but the data would still get sent) and gather statistics on how many users actually do this, to get a feel for what your margin of error is. You could even attempt to do some of the tracking yourself, if you are really determined.