@sentry/browser
Version:
Official Sentry SDK for browsers
50 lines (49 loc) • 2.1 kB
JavaScript
const require_debug_build = require("../debug-build.js");
const require_helpers = require("../helpers.js");
let _sentry_core = require("@sentry/core");
let _sentry_browser_utils = require("@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 = (0, _sentry_core.defineIntegration)((options = {}) => {
const lifecycle = options.lifecycle ?? "page";
return {
name: "BrowserSession",
setupOnce() {
if (typeof require_helpers.WINDOW.document === "undefined") {
require_debug_build.DEBUG_BUILD && _sentry_core.debug.warn("Using the `browserSessionIntegration` in non-browser environments is not supported.");
return;
}
(0, _sentry_core.startSession)({ ignoreDuration: true });
let initialSessionSent = false;
(0, _sentry_browser_utils.whenIdleOrHidden)(() => {
if (!initialSessionSent) {
(0, _sentry_core.captureSession)();
initialSessionSent = true;
}
});
const isolationScope = (0, _sentry_core.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) (0, _sentry_core.captureSession)();
}
});
if (lifecycle === "route") (0, _sentry_browser_utils.addHistoryInstrumentationHandler)(({ from, to }) => {
if (from !== to) {
(0, _sentry_core.startSession)({ ignoreDuration: true });
(0, _sentry_core.captureSession)();
initialSessionSent = true;
}
});
}
};
});
exports.browserSessionIntegration = browserSessionIntegration;
//# sourceMappingURL=browsersession.js.map