Skip to content

Commit

Permalink
fix(session replay): opt out should take effect immediately (#586)
Browse files Browse the repository at this point in the history
  • Loading branch information
jxiwang authored Sep 26, 2023
2 parents 5af910a + 938b5d5 commit 5374314
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/session-replay-browser/src/session-replay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ export class SessionReplay implements AmplitudeSessionReplay {
identityStoreOptOut = identityStore.getIdentity().optOut;
}

return identityStoreOptOut || this.config?.optOut;
return identityStoreOptOut !== undefined ? identityStoreOptOut : this.config?.optOut;
}

getShouldRecord() {
Expand Down Expand Up @@ -242,7 +242,7 @@ export class SessionReplay implements AmplitudeSessionReplay {
this.stopRecordingEvents = record({
emit: (event) => {
const globalScope = getGlobalScope();
if (globalScope && globalScope.document && !globalScope.document.hasFocus()) {
if ((globalScope && globalScope.document && !globalScope.document.hasFocus()) || !this.getShouldRecord()) {
this.stopRecordingAndSendEvents();
return;
}
Expand Down
47 changes: 47 additions & 0 deletions packages/session-replay-browser/test/session-replay.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,20 @@ describe('SessionReplayPlugin', () => {
await sessionReplay.init(apiKey, { ...mockOptions, instanceName: 'my_instance' }).promise;
expect(sessionReplay.shouldOptOut()).toEqual(true);
});
test('should return opt out from identity store even if set to false', async () => {
jest.spyOn(AnalyticsClientCommon, 'getAnalyticsConnector').mockReturnValue({
identityStore: {
getIdentity: () => {
return {
optOut: false,
};
},
},
} as unknown as ReturnType<typeof AnalyticsClientCommon.getAnalyticsConnector>);
const sessionReplay = new SessionReplay();
await sessionReplay.init(apiKey, { ...mockOptions, instanceName: 'my_instance', optOut: true }).promise;
expect(sessionReplay.shouldOptOut()).toEqual(false);
});
test('should return config device id if set', async () => {
const sessionReplay = new SessionReplay();
await sessionReplay.init(apiKey, { ...mockOptions, instanceName: 'my_instance', optOut: true }).promise;
Expand Down Expand Up @@ -673,6 +687,15 @@ describe('SessionReplayPlugin', () => {
expect(record).not.toHaveBeenCalled();
expect(sessionReplay.events).toEqual([]);
});

test('should return early if user opts out', async () => {
const sessionReplay = new SessionReplay();
await sessionReplay.init(apiKey, { ...mockOptions, optOut: true }).promise;
sessionReplay.recordEvents();
expect(record).not.toHaveBeenCalled();
expect(sessionReplay.events).toEqual([]);
});

test('should store events in class and in IDB', async () => {
const sessionReplay = new SessionReplay();
await sessionReplay.init(apiKey, mockOptions).promise;
Expand Down Expand Up @@ -784,6 +807,30 @@ describe('SessionReplayPlugin', () => {
expect(sessionReplay.events).toEqual([mockEventString]); // events should not change, emmitted event should be ignored
});

test('should stop recording and send events if user opts out during recording', async () => {
const sessionReplay = new SessionReplay();
await sessionReplay.init(apiKey, mockOptions).promise;
sessionReplay.recordEvents();
const stopRecordingMock = jest.fn();
sessionReplay.stopRecordingEvents = stopRecordingMock;
expect(sessionReplay.events).toEqual([]);
sessionReplay.events = [mockEventString]; // Add one event to list to trigger sending in stopRecordingAndSendEvents
// eslint-disable-next-line @typescript-eslint/no-empty-function
const sendEventsListMock = jest.spyOn(sessionReplay, 'sendEventsList').mockImplementationOnce(() => {});
sessionReplay.shouldOptOut = () => true;
const recordArg = record.mock.calls[0][0];
recordArg?.emit && recordArg?.emit(mockEvent);
expect(sendEventsListMock).toHaveBeenCalledTimes(1);
expect(sendEventsListMock).toHaveBeenCalledWith({
events: [mockEventString],
sequenceId: 0,
sessionId: 123,
});
expect(stopRecordingMock).toHaveBeenCalled();
expect(sessionReplay.stopRecordingEvents).toEqual(null);
expect(sessionReplay.events).toEqual([mockEventString]); // events should not change, emmitted event should be ignored
});

test('should add an error handler', async () => {
const sessionReplay = new SessionReplay();
await sessionReplay.init(apiKey, mockOptions).promise;
Expand Down

0 comments on commit 5374314

Please sign in to comment.