Skip to content

Instantly share code, notes, and snippets.

@Sheraff
Last active July 3, 2021 08:38
Show Gist options
  • Save Sheraff/b765113a8f6995b9608497e8e7e42f24 to your computer and use it in GitHub Desktop.
Save Sheraff/b765113a8f6995b9608497e8e7e42f24 to your computer and use it in GitHub Desktop.
send GET request even if user leaves page w/ `sendBeacon`
// last reliably fired event before a user leaves
document.addEventListener('visibilitychange', () => {
if (document.visibilityState === 'hidden') {
// queues up a request to be sent even if the user leaves
navigator.sendBeacon('/log', analyticsData)
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment