@segment/analytics-next
Version:
Analytics Next (aka Analytics 2.0) is the latest version of Segment’s JavaScript SDK - enabling you to send your data to any tool without having to learn, test, or use a new API every time.
35 lines • 1.27 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.onPageChange = void 0;
/**
* Register event listener on document that fires when:
* * tab change or tab close (in mobile or desktop)
* * click back / forward button
* * click any link or perform any other navigation action
* * soft refresh / hard refresh
*
* adapted from https://stackoverflow.com/questions/3239834/window-onbeforeunload-not-working-on-the-ipad/52864508#52864508,
*/
var onPageChange = function (cb) {
var unloaded = false; // prevents double firing if both are supported
window.addEventListener('pagehide', function () {
if (unloaded)
return;
unloaded = true;
cb(unloaded);
});
// using document instead of window because of bug affecting browsers before safari 14 (detail in footnotes https://caniuse.com/?search=visibilitychange)
document.addEventListener('visibilitychange', function () {
if (document.visibilityState == 'hidden') {
if (unloaded)
return;
unloaded = true;
}
else {
unloaded = false;
}
cb(unloaded);
});
};
exports.onPageChange = onPageChange;
//# sourceMappingURL=on-page-change.js.map