Skip to content

Commit 98db1ca

Browse files
author
Luca Forstner
committed
feat(browser): Add browserSessionIntegration
1 parent 0b349eb commit 98db1ca

File tree

4 files changed

+52
-40
lines changed

4 files changed

+52
-40
lines changed

packages/angular/src/sdk.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { VERSION } from '@angular/core';
22
import type { BrowserOptions } from '@sentry/browser';
33
import {
44
breadcrumbsIntegration,
5+
browserSessionIntegration,
56
globalHandlersIntegration,
67
httpContextIntegration,
78
init as browserInit,
@@ -22,7 +23,7 @@ import { IS_DEBUG_BUILD } from './flags';
2223
/**
2324
* Get the default integrations for the Angular SDK.
2425
*/
25-
export function getDefaultIntegrations(): Integration[] {
26+
export function getDefaultIntegrations(options: BrowserOptions = {}): Integration[] {
2627
// Don't include the BrowserApiErrors integration as it interferes with the Angular SDK's `ErrorHandler`:
2728
// BrowserApiErrors would catch certain errors before they reach the `ErrorHandler` and
2829
// thus provide a lower fidelity error than what `SentryErrorHandler`
@@ -31,7 +32,7 @@ export function getDefaultIntegrations(): Integration[] {
3132
// see:
3233
// - https://github.com/getsentry/sentry-javascript/issues/5417#issuecomment-1453407097
3334
// - https://github.com/getsentry/sentry-javascript/issues/2744
34-
return [
35+
const integrations = [
3536
inboundFiltersIntegration(),
3637
functionToStringIntegration(),
3738
breadcrumbsIntegration(),
@@ -40,6 +41,12 @@ export function getDefaultIntegrations(): Integration[] {
4041
dedupeIntegration(),
4142
httpContextIntegration(),
4243
];
44+
45+
if (options.autoSessionTracking !== false) {
46+
integrations.push(browserSessionIntegration());
47+
}
48+
49+
return integrations;
4350
}
4451

4552
/**

packages/browser/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,4 @@ export type { Span } from '@sentry/core';
7777
export { makeBrowserOfflineTransport } from './transports/offline';
7878
export { browserProfilingIntegration } from './profiling/integration';
7979
export { spotlightBrowserIntegration } from './integrations/spotlight';
80+
export { browserSessionIntegration } from './integrations/session';
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { addHistoryInstrumentationHandler } from '@sentry-internal/browser-utils';
2+
import { captureSession, defineIntegration, logger, startSession } from '@sentry/core';
3+
import { DEBUG_BUILD } from '../debug-build';
4+
import { WINDOW } from '../helpers';
5+
6+
export const browserSessionIntegration = defineIntegration(() => {
7+
return {
8+
name: 'Session',
9+
setupOnce() {
10+
if (typeof WINDOW.document === 'undefined') {
11+
DEBUG_BUILD && logger.warn('Using the sessionIntegration in non-browser environments is not supported.');
12+
return;
13+
}
14+
15+
// The session duration for browser sessions does not track a meaningful
16+
// concept that can be used as a metric.
17+
// Automatically captured sessions are akin to page views, and thus we
18+
// discard their duration.
19+
startSession({ ignoreDuration: true });
20+
captureSession();
21+
22+
// We want to create a session for every navigation as well
23+
addHistoryInstrumentationHandler(({ from, to }) => {
24+
// Don't create an additional session for the initial route or if the location did not change
25+
if (from !== undefined && from !== to) {
26+
startSession({ ignoreDuration: true });
27+
captureSession();
28+
}
29+
});
30+
},
31+
};
32+
});

packages/browser/src/sdk.ts

Lines changed: 10 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import { addHistoryInstrumentationHandler } from '@sentry-internal/browser-utils';
21
import {
3-
captureSession,
42
consoleSandbox,
53
dedupeIntegration,
64
functionToStringIntegration,
@@ -13,7 +11,6 @@ import {
1311
lastEventId,
1412
logger,
1513
stackParserFromStackParserOptions,
16-
startSession,
1714
supportsFetch,
1815
} from '@sentry/core';
1916
import type { Client, DsnLike, Integration, Options, UserFeedback } from '@sentry/core';
@@ -26,16 +23,17 @@ import { browserApiErrorsIntegration } from './integrations/browserapierrors';
2623
import { globalHandlersIntegration } from './integrations/globalhandlers';
2724
import { httpContextIntegration } from './integrations/httpcontext';
2825
import { linkedErrorsIntegration } from './integrations/linkederrors';
26+
import { browserSessionIntegration } from './integrations/session';
2927
import { defaultStackParser } from './stack-parsers';
3028
import { makeFetchTransport } from './transports/fetch';
3129

3230
/** Get the default integrations for the browser SDK. */
33-
export function getDefaultIntegrations(_options: Options): Integration[] {
31+
export function getDefaultIntegrations(options: Options): Integration[] {
3432
/**
3533
* Note: Please make sure this stays in sync with Angular SDK, which re-exports
3634
* `getDefaultIntegrations` but with an adjusted set of integrations.
3735
*/
38-
return [
36+
const integrations = [
3937
inboundFiltersIntegration(),
4038
functionToStringIntegration(),
4139
browserApiErrorsIntegration(),
@@ -45,6 +43,12 @@ export function getDefaultIntegrations(_options: Options): Integration[] {
4543
dedupeIntegration(),
4644
httpContextIntegration(),
4745
];
46+
47+
if (options.autoSessionTracking !== false) {
48+
integrations.push(browserSessionIntegration());
49+
}
50+
51+
return integrations;
4852
}
4953

5054
function applyDefaultOptions(optionsArg: BrowserOptions = {}): BrowserOptions {
@@ -187,13 +191,7 @@ export function init(browserOptions: BrowserOptions = {}): Client | undefined {
187191
transport: options.transport || makeFetchTransport,
188192
};
189193

190-
const client = initAndBind(BrowserClient, clientOptions);
191-
192-
if (options.autoSessionTracking) {
193-
startSessionTracking();
194-
}
195-
196-
return client;
194+
return initAndBind(BrowserClient, clientOptions);
197195
}
198196

199197
/**
@@ -308,32 +306,6 @@ export function onLoad(callback: () => void): void {
308306
callback();
309307
}
310308

311-
/**
312-
* Enable automatic Session Tracking for the initial page load.
313-
*/
314-
function startSessionTracking(): void {
315-
if (typeof WINDOW.document === 'undefined') {
316-
DEBUG_BUILD && logger.warn('Session tracking in non-browser environment with @sentry/browser is not supported.');
317-
return;
318-
}
319-
320-
// The session duration for browser sessions does not track a meaningful
321-
// concept that can be used as a metric.
322-
// Automatically captured sessions are akin to page views, and thus we
323-
// discard their duration.
324-
startSession({ ignoreDuration: true });
325-
captureSession();
326-
327-
// We want to create a session for every navigation as well
328-
addHistoryInstrumentationHandler(({ from, to }) => {
329-
// Don't create an additional session for the initial route or if the location did not change
330-
if (from !== undefined && from !== to) {
331-
startSession({ ignoreDuration: true });
332-
captureSession();
333-
}
334-
});
335-
}
336-
337309
/**
338310
* Captures user feedback and sends it to Sentry.
339311
*

0 commit comments

Comments
 (0)