Skip to content

Instantly share code, notes, and snippets.

@chrisgoddard
Last active May 2, 2016 22:34
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save chrisgoddard/f6bb9fe79924d207597ea9eb02c73179 to your computer and use it in GitHub Desktop.
function onUnload()
{
var timeout = 300; // maximum time to hold up the page
var nonInteraction; // interaction code not included for brevity
var skip = false
var data = {
eventCategory: 'Category',
eventAction: 'Action',
eventLabel: (everScrolled) ? 'Did Scroll' : 'No Scroll',
eventValue: reach,
nonInteraction: nonInteraction,
hitCallback: function(){
skip = true
}
}
// send hit
ga('send', 'event', data)
// evilness
if (timeout && !window.navigator.sendBeacon){
var start = new Date()
var run = 0
do {
run = new Date() - start
} while (run < timeout && !skip)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment