Skip to content

Commit 167750a

Browse files
author
Kelly Wallach
committed
feat(sesssion replay): pr feedback
1 parent a18bc3e commit 167750a

File tree

2 files changed

+21
-22
lines changed

2 files changed

+21
-22
lines changed

packages/session-replay-browser/src/session-replay.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ export class SessionReplay implements AmplitudeSessionReplay {
197197
// Restart recording on focus to ensure that when user
198198
// switches tabs, we take a full snapshot
199199
this.stopRecordingEvents();
200-
this.captureEventsIfShould();
200+
this.captureEvents();
201201
};
202202

203203
/**
@@ -239,7 +239,7 @@ export class SessionReplay implements AmplitudeSessionReplay {
239239
});
240240
}
241241

242-
this.captureEventsIfShould();
242+
this.captureEvents();
243243
};
244244

245245
sendEvents(sessionId?: number) {
@@ -288,12 +288,10 @@ export class SessionReplay implements AmplitudeSessionReplay {
288288
`Not capturing replays for session ${this.identifiers.sessionId} due to not matching targeting conditions.`,
289289
);
290290
return false;
291-
} else {
292-
this.loggerProvider.log(
293-
`Capturing replays for session ${this.identifiers.sessionId} due to matching targeting conditions.`,
294-
);
295-
return true;
296291
}
292+
this.loggerProvider.log(
293+
`Capturing replays for session ${this.identifiers.sessionId} due to matching targeting conditions.`,
294+
);
297295
} else {
298296
const isInSample = isSessionInSample(this.identifiers.sessionId, this.config.sampleRate);
299297
if (!isInSample) {
@@ -334,17 +332,18 @@ export class SessionReplay implements AmplitudeSessionReplay {
334332
return maskSelector as unknown as string;
335333
}
336334

337-
captureEventsIfShould() {
338-
const shouldRecord = this.getShouldCapture();
339-
const sessionId = this.identifiers?.sessionId;
340-
if (!shouldRecord || !sessionId || !this.config) {
341-
return;
342-
}
335+
captureEvents() {
343336
if (this.recordCancelCallback) {
344337
this.loggerProvider.debug('captureEvents method fired - Session Replay capture already in progress.');
345338
return;
346339
}
347340

341+
const shouldRecord = this.getShouldCapture();
342+
const sessionId = this.identifiers && this.identifiers.sessionId;
343+
if (!shouldRecord || !sessionId || !this.config) {
344+
return;
345+
}
346+
348347
const privacyConfig = this.config.privacyConfig;
349348

350349
this.loggerProvider.log('Session Replay capture beginning.');

packages/session-replay-browser/test/session-replay.test.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -698,15 +698,15 @@ describe('SessionReplay', () => {
698698
});
699699
});
700700

701-
describe('captureEventsIfShould', () => {
701+
describe('captureEvents', () => {
702702
test('should return early if no config', async () => {
703703
await sessionReplay.init(apiKey, mockOptions).promise;
704704
const createEventsIDBStoreInstance = await (SessionReplayIDB.createEventsIDBStore as jest.Mock).mock.results[0]
705705
.value;
706706

707707
record.mockReset();
708708
sessionReplay.config = undefined;
709-
sessionReplay.captureEventsIfShould();
709+
sessionReplay.captureEvents();
710710
expect(record).not.toHaveBeenCalled();
711711
if (!sessionReplay.eventsManager) {
712712
throw new Error('Did not call init');
@@ -718,7 +718,7 @@ describe('SessionReplay', () => {
718718
await sessionReplay.init(apiKey, mockOptions).promise;
719719
record.mockReset();
720720
sessionReplay.identifiers = undefined;
721-
sessionReplay.captureEventsIfShould();
721+
sessionReplay.captureEvents();
722722
expect(record).not.toHaveBeenCalled();
723723
});
724724

@@ -727,7 +727,7 @@ describe('SessionReplay', () => {
727727
.promise;
728728
const createEventsIDBStoreInstance = await (SessionReplayIDB.createEventsIDBStore as jest.Mock).mock.results[0]
729729
.value;
730-
sessionReplay.captureEventsIfShould();
730+
sessionReplay.captureEvents();
731731
expect(record).not.toHaveBeenCalled();
732732
if (!sessionReplay.eventsManager) {
733733
throw new Error('Did not call init');
@@ -740,7 +740,7 @@ describe('SessionReplay', () => {
740740
await sessionReplay.init(apiKey, mockOptions).promise;
741741
const createEventsIDBStoreInstance = await (SessionReplayIDB.createEventsIDBStore as jest.Mock).mock.results[0]
742742
.value;
743-
sessionReplay.captureEventsIfShould();
743+
sessionReplay.captureEvents();
744744
if (!sessionReplay.eventsManager) {
745745
throw new Error('Did not call init');
746746
}
@@ -762,7 +762,7 @@ describe('SessionReplay', () => {
762762
await sessionReplay.init(apiKey, mockOptions).promise;
763763
const createEventsIDBStoreInstance = await (SessionReplayIDB.createEventsIDBStore as jest.Mock).mock.results[0]
764764
.value;
765-
sessionReplay.captureEventsIfShould();
765+
sessionReplay.captureEvents();
766766
const stopRecordingMock = jest.fn();
767767
sessionReplay.recordCancelCallback = stopRecordingMock;
768768
if (!sessionReplay.eventsManager) {
@@ -788,7 +788,7 @@ describe('SessionReplay', () => {
788788

789789
test('should add an error handler', async () => {
790790
await sessionReplay.init(apiKey, mockOptions).promise;
791-
sessionReplay.captureEventsIfShould();
791+
sessionReplay.captureEvents();
792792
const recordArg = record.mock.calls[0][0];
793793
const errorHandlerReturn = recordArg?.errorHandler && recordArg?.errorHandler(new Error('test error'));
794794
// eslint-disable-next-line @typescript-eslint/unbound-method
@@ -799,7 +799,7 @@ describe('SessionReplay', () => {
799799
test('should rethrow CSSStylesheet errors', async () => {
800800
const sessionReplay = new SessionReplay();
801801
await sessionReplay.init(apiKey, mockOptions).promise;
802-
sessionReplay.captureEventsIfShould();
802+
sessionReplay.captureEvents();
803803
const recordArg = record.mock.calls[0][0];
804804
const stylesheetErrorMessage =
805805
"Failed to execute 'insertRule' on 'CSSStyleSheet': Failed to parse the rule 'body::-ms-expand{display: none}";
@@ -811,7 +811,7 @@ describe('SessionReplay', () => {
811811
test('should rethrow external errors', async () => {
812812
const sessionReplay = new SessionReplay();
813813
await sessionReplay.init(apiKey, mockOptions).promise;
814-
sessionReplay.captureEventsIfShould();
814+
sessionReplay.captureEvents();
815815
const recordArg = record.mock.calls[0][0];
816816
const error = new Error('test') as Error & { _external_?: boolean };
817817
error._external_ = true;

0 commit comments

Comments
 (0)