Skip to content

Instantly share code, notes, and snippets.

@dcvogi
Last active April 4, 2023 18:54
Show Gist options
  • Save dcvogi/e6555a7aa724cecc87e49d329394de29 to your computer and use it in GitHub Desktop.
Save dcvogi/e6555a7aa724cecc87e49d329394de29 to your computer and use it in GitHub Desktop.
(function pageVisibility(){
function initializeTracking(){
//...
}
function handleVisibilityChange(event){
if(document.hidden){
return;
}
event.target.removeEventListener(event.type, handleVisibilityChange);
initializeTracking();
}
function initWhenVisible(){
if (typeof document.addEventListener === "undefined" || typeof document.hidden === "undefined") {
ga('send', 'event', 'browser support features', 'visibility', 'unable to handle visibility API');
initializeTracking();
} else {
document.addEventListener(visibilityChange, handleVisibilityChange, false);
}
};
var hidden, visibilityChange;
if (typeof document.hidden !== "undefined") { // Opera 12.10 and Firefox 18 and later support
hidden = "hidden";
visibilityChange = "visibilitychange";
} else if (typeof document.msHidden !== "undefined") {
hidden = "msHidden";
visibilityChange = "msvisibilitychange";
} else if (typeof document.webkitHidden !== "undefined") {
hidden = "webkitHidden";
visibilityChange = "webkitvisibilitychange";
}
if(document.hidden){
initWhenVisible();
} else {
initializeTracking();
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment