Skip to content

Commit

Permalink
More accurate page close event handling (#115)
Browse files Browse the repository at this point in the history
* tweak: more accurate page close event handling

* fix: typo

* revert: page close event listener condition

* tweak: better readability
  • Loading branch information
hsynlms authored Jul 23, 2021
1 parent 162ad72 commit e434534
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,19 @@ for (var i = 0, l = pixelFunc.queue.length; i < l; i++) {
pixelFunc.process.apply(pixelFunc, pixelFunc.queue[i]);
}

window.addEventListener('unload', function() {
// https://github.com/GoogleChromeLabs/page-lifecycle/blob/master/src/Lifecycle.mjs
// Safari does not reliably fire the `pagehide` or `visibilitychange`
var isNotSafari = !(typeof safari === 'object' && safari.pushNotification);
var isPageHideSupported = 'onpageshow' in self;

// IE9-10 do not support the pagehide event, so we fall back to unload
// pagehide event is more reliable but less broad than unload event for mobile and modern browsers
var pageCloseEvent =
isPageHideSupported && isNotSafari
? 'pagehide'
: 'unload';

window.addEventListener(pageCloseEvent, function() {
if (!Config.pageCloseOnce) {
Config.pageCloseOnce = true;
new Pixel('pageclose', Helper.now(), function() {
Expand Down

0 comments on commit e434534

Please sign in to comment.