'Measure how quickly users abandon slowly loading pages

I'm trying to measure how quickly users abandon slowly loading pages.

My plan was to register a listener for the "unload" event as soon as possible, and then send the value of performance.now() using navigator.sendBeacon()

My problem is that the "unload" event appears to not fire if "DOMContentLoaded" hasn't fired yet. I suppose that makes sense ... can something be unloaded if it hasn't been loaded yet? But I still need a workaround to measure how quickly users abandon the page if they abandon before "DomContentLoaded".

I've attempted to listen for the "abort" event, but it doesn't seem to fire. My guess is that it only fires if a download was interrupted, and isn't fired when DOM parsing is interrupted.

Any suggestions on how to successfully measure this?

PS: suggestions that don't work in all browsers are also welcome.

EDIT: I've read jQuery ajaxError() handler fires if user leaves page before page finishes loading, and tried to build a solution which registers an "abort" handler on an XMLHttpRequest which the server intentionally answers very slowly. It does produce an event, which fires in the case of navigation, but it doesn't fire if the tab is closed. There's no guarantee that it runs, unlike the "unload" event. A solution that always works would be much better.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source