Use visibilitychange event instead of unload event
Reason: the unload event is incompatible with the back/forward cache
Some browsers, such as Firefox, handle this incompatibility by excluding pages from the bfcache if they contain unload handlers, thus hurting performance. Others, such as Safari and Chrome on Android, handle it by not firing the unload event when the user navigates to another page in the same tab.
unload event does not fire on mobile browsers
Unload can be used in case of internet explorer but that does not support beacon.
reference: https://developer.mozilla.org/en-US/docs/Web/API/Navigator/sendBeacon#sending_analytics_at_the_end_of_a_session
solution:



Use
visibilitychangeevent instead of unload eventReason: the
unloadevent is incompatible with the back/forward cacheSome browsers, such as Firefox, handle this incompatibility by excluding pages from the bfcache if they contain unload handlers, thus hurting performance. Others, such as Safari and Chrome on Android, handle it by not firing the unload event when the user navigates to another page in the same tab.
unloadevent does not fire on mobile browsersUnload can be used in case of internet explorer but that does not support beacon.
reference: https://developer.mozilla.org/en-US/docs/Web/API/Navigator/sendBeacon#sending_analytics_at_the_end_of_a_session
solution:
