diff --git a/lib/index.browser.ts b/lib/index.browser.ts index 0f644a844..ffa291f33 100644 --- a/lib/index.browser.ts +++ b/lib/index.browser.ts @@ -33,13 +33,18 @@ export const createInstance = function(config: Config): Client { }); if (client) { - const unloadEvent = 'onpagehide' in window ? 'pagehide' : 'unload'; - window.addEventListener( - unloadEvent, - () => { + if ('onvisibilitychange' in document) { + document.addEventListener('visibilitychange', () => { + if (document.hidden) { + client.flushImmediately(); + } + }); + } else { + const unloadEvent = 'onpagehide' in window ? 'pagehide' : 'unload'; + window.addEventListener(unloadEvent, () => { client.flushImmediately(); - }, - ); + }); + } } return client;