Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions packages/core/src/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type { Primitive } from './types/misc';
import type { Session, SessionContext } from './types/session';
import type { SeverityLevel } from './types/severity';
import type { User } from './types/user';
import { filterKeyValueData } from './utils/data-collection/filterKeyValueData';
import { debug } from './utils/debug-logger';
import type { ExclusiveEventHintOrCaptureContext } from './utils/prepareEvent';
import { parseEventHintOrCaptureContext } from './utils/prepareEvent';
Expand Down Expand Up @@ -252,12 +253,15 @@ export function startSession(context?: SessionContext): Session {

const { user } = getCombinedScopeData(isolationScope, getCurrentScope());

// Will fetch userAgent if called from browser sdk
const { userAgent } = GLOBAL_OBJ.navigator || {};
let userAgent = GLOBAL_OBJ.navigator?.userAgent;
if (userAgent) {
const headerCollection = getClient()?.getDataCollectionOptions().httpHeaders.request ?? true;
userAgent = filterKeyValueData({ 'User-Agent': userAgent }, headerCollection)['User-Agent'];
}

const session = makeSession({
user,
...(userAgent && { userAgent }),
userAgent,
...context,
});

Expand Down
35 changes: 34 additions & 1 deletion packages/core/test/lib/session.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,40 @@
import { describe, expect, it, test } from 'vitest';
import { afterEach, beforeEach, describe, expect, it, test, vi } from 'vitest';
import { getCurrentScope } from '../../src/currentScopes';
import { startSession } from '../../src/exports';
import { closeSession, makeSession, updateSession } from '../../src/session';
import type { SessionContext } from '../../src/types/session';
import { timestampInSeconds } from '../../src/utils/time';
import { getDefaultTestClientOptions, TestClient } from '../mocks/client';
import { resetGlobals } from '../testutils';

describe('startSession', () => {
const userAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/130.0.0.0 Safari/537.36';

beforeEach(() => {
resetGlobals();
vi.stubGlobal('navigator', { userAgent });
getCurrentScope().setClient(
new TestClient(getDefaultTestClientOptions({ dataCollection: { httpHeaders: { request: false } } })),
);
});

afterEach(() => {
vi.unstubAllGlobals();
resetGlobals();
});

it('omits the automatic user agent when request header collection is disabled', () => {
const session = startSession();

expect(session.toJSON().attrs?.user_agent).toBeUndefined();
});

it('preserves an explicit user agent when request header collection is disabled', () => {
const session = startSession({ userAgent: 'custom-user-agent' });

expect(session.toJSON().attrs?.user_agent).toBe('custom-user-agent');
});
});

describe('Session', () => {
it('initializes with the proper defaults', () => {
Expand Down
Loading