diff --git a/spec/mocha.helpers.js b/spec/mocha.helpers.js index e3ee9060..165e5131 100644 --- a/spec/mocha.helpers.js +++ b/spec/mocha.helpers.js @@ -21,7 +21,7 @@ const triggerResize = () => { const triggerUnload = () => { const unloadEvent = document.createEvent('Event'); - unloadEvent.initEvent('beforeunload', true, true); + unloadEvent.initEvent('visibilitychange', true, true); global.window.unload = () => { global.window.dispatchEvent(unloadEvent); diff --git a/src/utils/request-queue.js b/src/utils/request-queue.js index 02146c9a..7158d2e0 100644 --- a/src/utils/request-queue.js +++ b/src/utils/request-queue.js @@ -20,8 +20,10 @@ class RequestQueue { : false; // Defaults to 'false' // Mark if page environment is unloading - helpers.addEventListener('beforeunload', () => { - this.pageUnloading = true; + helpers.addEventListener('visibilitychange', () => { + if (document.visibilityState === 'hidden') { + this.pageUnloading = true; + } }); if (this.sendTrackingEvents) { @@ -182,7 +184,7 @@ class RequestQueue { if (this.options && this.options.trackingSendDelay === 0) { this.sendEvents(); } else { - // Defer sending of events to give beforeunload time to register (avoids race condition) + // Defer sending of events to give visibilitychange time to register (avoids race condition) setTimeout(this.sendEvents.bind(this), (this.options && this.options.trackingSendDelay) || 250); } }