Skip to content

Commit

Permalink
fix(plugin-session-replay-browser): default forceSessionTracking to f…
Browse files Browse the repository at this point in the history
…alse

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.
  • Loading branch information
lewgordon-amplitude committed Jan 13, 2025
1 parent 581d91b commit bf8070d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 20 deletions.
6 changes: 1 addition & 5 deletions packages/plugin-session-replay-browser/src/session-replay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
44 changes: 29 additions & 15 deletions packages/plugin-session-replay-browser/test/session-replay.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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: {
Expand Down Expand Up @@ -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();
});
});

Expand Down Expand Up @@ -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 () => {
Expand Down

0 comments on commit bf8070d

Please sign in to comment.