@sentry/browser
Version:
Official Sentry SDK for browsers
50 lines (49 loc) • 1.91 kB
JavaScript
import { DEBUG_BUILD } from "../debug-build.js";
import { WINDOW } from "../helpers.js";
import { captureSession, debug, defineIntegration, getIsolationScope, startSession } from "@sentry/core";
import { addHistoryInstrumentationHandler, whenIdleOrHidden } from "@sentry/browser-utils";
// src/integrations/browsersession.ts
/**
* When added, automatically creates sessions which allow you to track adoption and crashes (crash free rate) in your Releases in Sentry.
* More information: https://docs.sentry.io/product/releases/health/
*
* Note: In order for session tracking to work, you need to set up Releases: https://docs.sentry.io/product/releases/
*/
const browserSessionIntegration = defineIntegration((options = {}) => {
const lifecycle = options.lifecycle ?? "page";
return {
name: "BrowserSession",
setupOnce() {
if (typeof WINDOW.document === "undefined") {
DEBUG_BUILD && debug.warn("Using the `browserSessionIntegration` in non-browser environments is not supported.");
return;
}
startSession({ ignoreDuration: true });
let initialSessionSent = false;
whenIdleOrHidden(() => {
if (!initialSessionSent) {
captureSession();
initialSessionSent = true;
}
});
const isolationScope = getIsolationScope();
let previousUser = isolationScope.getUser();
isolationScope.addScopeListener((scope) => {
const maybeNewUser = scope.getUser();
if (previousUser?.id !== maybeNewUser?.id || previousUser?.ip_address !== maybeNewUser?.ip_address) {
previousUser = maybeNewUser;
if (initialSessionSent) captureSession();
}
});
if (lifecycle === "route") addHistoryInstrumentationHandler(({ from, to }) => {
if (from !== to) {
startSession({ ignoreDuration: true });
captureSession();
initialSessionSent = true;
}
});
}
};
});
export { browserSessionIntegration };
//# sourceMappingURL=browsersession.js.map