From bf8070d17ffd6b8bf005a5a495537eae448bd113 Mon Sep 17 00:00:00 2001 From: Lew Gordon Date: Mon, 13 Jan 2025 16:47:46 -0500 Subject: [PATCH] fix(plugin-session-replay-browser): default forceSessionTracking to false The goal of the Session Replay plugin is to always have it added and control configuration through remote config. However, the plugin currently affects the configuration such that it's impossible to override. Rather than have users specify `forceSessionTracking` explicitly to false we'll have it default to false and have the downside that potentially Session Start and Session End events are missing. --- .../src/session-replay.ts | 6 +-- .../test/session-replay.test.ts | 44 ++++++++++++------- 2 files changed, 30 insertions(+), 20 deletions(-) diff --git a/packages/plugin-session-replay-browser/src/session-replay.ts b/packages/plugin-session-replay-browser/src/session-replay.ts index 0712bdec3..60d55afe0 100644 --- a/packages/plugin-session-replay-browser/src/session-replay.ts +++ b/packages/plugin-session-replay-browser/src/session-replay.ts @@ -13,11 +13,7 @@ export class SessionReplayPlugin implements EnrichmentPlugin { options: SessionReplayOptions; constructor(options?: SessionReplayOptions) { - this.options = { ...options }; - // The user did not explicitly configure forceSessionTracking to false, default to true. - if (this.options.forceSessionTracking !== false) { - this.options.forceSessionTracking = true; - } + this.options = { forceSessionTracking: false, ...options }; } async setup(config: BrowserConfig) { diff --git a/packages/plugin-session-replay-browser/test/session-replay.test.ts b/packages/plugin-session-replay-browser/test/session-replay.test.ts index 750d537c1..1825a6bab 100644 --- a/packages/plugin-session-replay-browser/test/session-replay.test.ts +++ b/packages/plugin-session-replay-browser/test/session-replay.test.ts @@ -94,7 +94,7 @@ describe('SessionReplayPlugin', () => { }); describe('defaultTracking', () => { - test('should not change defaultTracking if its set to true', async () => { + test('should not change defaultTracking forceSessionTracking is not defined', async () => { const sessionReplay = new SessionReplayPlugin(); await sessionReplay.setup({ ...mockConfig, @@ -103,8 +103,17 @@ describe('SessionReplayPlugin', () => { expect(sessionReplay.config.defaultTracking).toBe(true); }); + test('should not change defaultTracking if its set to true', async () => { + const sessionReplay = new SessionReplayPlugin({ forceSessionTracking: true }); + await sessionReplay.setup({ + ...mockConfig, + defaultTracking: true, + }); + expect(sessionReplay.config.defaultTracking).toBe(true); + }); + test('should modify defaultTracking to enable sessions if its set to false', async () => { - const sessionReplay = new SessionReplayPlugin(); + const sessionReplay = new SessionReplayPlugin({ forceSessionTracking: true }); await sessionReplay.setup({ ...mockConfig, defaultTracking: false, @@ -118,7 +127,7 @@ describe('SessionReplayPlugin', () => { }); test('should modify defaultTracking to enable sessions if it is an object', async () => { - const sessionReplay = new SessionReplayPlugin(); + const sessionReplay = new SessionReplayPlugin({ forceSessionTracking: true }); await sessionReplay.setup({ ...mockConfig, defaultTracking: { @@ -223,12 +232,15 @@ describe('SessionReplayPlugin', () => { }); }); + // eslint-disable-next-line jest/expect-expect test('should fail gracefully', async () => { - const sessionReplay = new SessionReplayPlugin(); - init.mockImplementation(() => { - throw new Error('Mock Error'); - }); - await sessionReplay.setup(mockConfig); + expect(async () => { + const sessionReplay = new SessionReplayPlugin(); + init.mockImplementation(() => { + throw new Error('Mock Error'); + }); + await sessionReplay.setup(mockConfig); + }).not.toThrow(); }); }); @@ -333,14 +345,16 @@ describe('SessionReplayPlugin', () => { }); test('internal errors should not be thrown', async () => { - const sessionReplay = sessionReplayPlugin(); - await sessionReplay.setup(mockConfig); + expect(async () => { + const sessionReplay = sessionReplayPlugin(); + await sessionReplay.setup(mockConfig); - // Mock the shutdown function to throw an error - shutdown.mockImplementation(() => { - throw new Error('Mock shutdown error'); - }); - await sessionReplay.teardown?.(); + // Mock the shutdown function to throw an error + shutdown.mockImplementation(() => { + throw new Error('Mock shutdown error'); + }); + await sessionReplay.teardown?.(); + }).not.toThrow(); }); test('should update the session id on any event when using custom session id', async () => {