New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ensure JS tracker unload event is triggered in edge cases to make sure tracking requests are sent #18810
Comments
tried |
@tsteur, @bx80, and I found the Do lots of research, there is not really a good way to detect a user left browser other than |
I'm not sure I fully understand it but I'd listen to both
Would that solve the issue? |
@tsteur ah I think I confused you, but the problem is |
Yeah sorry I might not be getting it yet. It would be a good thing if they switch to a different tab and then we trigger the unload event I think, or could there be an issue? The biggest thing I could think of is that if people switch between tabs a lot then it could cause maybe a lot of requests which we would need to prevent maybe. I can see though it changes the meaning of "unload". Maybe we'd have to do that trade-off and just make sure we're not triggering the unload on visibility change too often but only about every 30seconds or so. Not sure if it makes sense. |
@tsteur right, that makes sense, I think a debounce maybe helps on tab change, but not perfect, will keep playing around. |
@tsteur I was thinking that if we trigger the I haven't looked at it in detail, but HSR for example hooks the |
Just a random thought: Isn't the main problem that some tracking requests might not be sent if the unload event isn't triggered and the page is closed? |
@sgiehl that would be already some good step towards the goal 👍 . The tracking plugins will still need to also register the tracking requests they have "queued" when visibility is changing to hidden so these will be sent as well. Otherwise they might not be tracked on mobile for example. @bx80 maybe in heatmaps ending the recording is not actually needed. I'm not actually sure why it was done since if the page unloads then we wouldn't need to end the recording anyway. And if the page is not unloaded then we still want to have the recording active. So in either case it might be not needed (unless I miss something which is possible) |
While testing #18135 I noticed that the beforeunload event isn't triggered when forcing a page reload. Was then reading https://developer.mozilla.org/en-US/docs/Web/API/Navigator/sendBeacon .
It also explains why you should use this event instead of unload. It basically may not be triggered in various cases (not sure if it's also the case for
beforeunload
, to be investigated).This could result in few queued requests not being sent and therefore some data may not be tracked. Especially premium features leverage the unload event and queued tracking to send a tracking request (eg for Media Analytics to track how much of a media has been viewed when leaving).
Something like:
Seems this is supported on most browsers: https://caniuse.com/?search=visibilitychange Maybe as a fallback we would still also listen to beforeunload in case this event isn't triggered so it still works on older browsers too maybe? Might not be needed depending on browser compatibility.
We would also need to make sure that this won't cause any regressions by listening to this new event. Also if someone switches a lot between tabs and the page becomes visible and hidden a lot, then we wouldn't want to start sending heaps of tracking requests.
Be good if someone could look into this. The goal of the issue would be to make sending tracking requests more reliable when a user leaves a site.
The text was updated successfully, but these errors were encountered: