Skip to content

Ensure JS tracker unload event is triggered in edge cases to make sure tracking requests are sent #18810

@tsteur

Description

@tsteur

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 .

Web sites often want to send analytics or diagnostics to the server when the user has finished with the page. The most reliable way to do this is to send the data on the visibilitychange event

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:

        addEventListener(windowAlias, 'visibilitychange', function () {
  if (document.visibilityState === 'hidden') {
          executePluginMethod('unload');
}
        }, false);

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.

Metadata

Metadata

Assignees

Labels

BugFor errors / faults / flaws / inconsistencies etc.MajorIndicates the severity or impact or benefit of an issue is much higher than normal but not critical.

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions