-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat: Sessions Health Tracking #2973
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
a73d3ef
bb2968a
d82f916
45e263f
db493a1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -84,7 +84,15 @@ export function init(options: BrowserOptions = {}): void { | |||||
options.release = window.SENTRY_RELEASE.id; | ||||||
} | ||||||
} | ||||||
if (options.autoSessionTracking === undefined) { | ||||||
options.autoSessionTracking = false; | ||||||
} | ||||||
|
||||||
initAndBind(BrowserClient, options); | ||||||
|
||||||
if (options.autoSessionTracking) { | ||||||
startSessionTracking(); | ||||||
} | ||||||
} | ||||||
|
||||||
/** | ||||||
|
@@ -166,3 +174,67 @@ export function close(timeout?: number): PromiseLike<boolean> { | |||||
export function wrap(fn: (...args: any) => any): any { | ||||||
return internalWrap(fn)(); | ||||||
} | ||||||
|
||||||
/** | ||||||
* Enable automatic Session Tracking for the initial page load. | ||||||
*/ | ||||||
function startSessionTracking(): void { | ||||||
const window = getGlobalObject<Window>(); | ||||||
const hub = getCurrentHub(); | ||||||
|
||||||
/** | ||||||
* We should be using `Promise.all([windowLoaded, firstContentfulPaint])` here, | ||||||
* but, as always, it's not available in the IE10-11. Thanks IE. | ||||||
*/ | ||||||
let loadResolved = document.readyState === 'complete'; | ||||||
let fcpResolved = false; | ||||||
const possiblyEndSession = (): void => { | ||||||
if (fcpResolved && loadResolved) { | ||||||
hub.endSession(); | ||||||
} | ||||||
}; | ||||||
const resolveWindowLoaded = (): void => { | ||||||
loadResolved = true; | ||||||
possiblyEndSession(); | ||||||
window.removeEventListener('load', resolveWindowLoaded); | ||||||
}; | ||||||
|
||||||
hub.startSession(); | ||||||
|
||||||
if (!loadResolved) { | ||||||
// IE doesn't support `{ once: true }` for event listeners, so we have to manually | ||||||
// attach and then detach it once completed. | ||||||
window.addEventListener('load', resolveWindowLoaded); | ||||||
} | ||||||
|
||||||
try { | ||||||
const po = new PerformanceObserver((entryList, po) => { | ||||||
entryList.getEntries().forEach(entry => { | ||||||
if (entry.name === 'first-contentful-paint' && entry.startTime < firstHiddenTime) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should guarantee that the if condition is run at most once. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Observer is disconnected after the first call, so I'm not sure how it'd be ever called twice? |
||||||
po.disconnect(); | ||||||
fcpResolved = true; | ||||||
possiblyEndSession(); | ||||||
} | ||||||
}); | ||||||
}); | ||||||
|
||||||
// There's no need to even attach this listener if `PerformanceObserver` constructor will fail, | ||||||
// so we do it below here. | ||||||
let firstHiddenTime = document.visibilityState === 'hidden' ? 0 : Infinity; | ||||||
document.addEventListener( | ||||||
'visibilitychange', | ||||||
event => { | ||||||
firstHiddenTime = Math.min(firstHiddenTime, event.timeStamp); | ||||||
}, | ||||||
{ once: true }, | ||||||
); | ||||||
|
||||||
po.observe({ | ||||||
type: 'paint', | ||||||
buffered: true, | ||||||
}); | ||||||
} catch (e) { | ||||||
fcpResolved = true; | ||||||
possiblyEndSession(); | ||||||
} | ||||||
} |
Uh oh!
There was an error while loading. Please reload this page.