@@ -128,14 +128,17 @@ export class SessionReplay implements AmplitudeSessionReplay {
128
128
129
129
this . teardownEventListeners ( false ) ;
130
130
131
- await this . evaluateTargetingAndRecord ( { userProperties : options . userProperties } ) ;
131
+ this . stopRecordingEvents ( ) ;
132
+ await this . evaluateTargetingAndCapture ( { userProperties : options . userProperties } ) ;
132
133
}
133
134
134
135
setSessionId ( sessionId : number , deviceId ?: string , options ?: { userProperties ?: { [ key : string ] : any } } ) {
135
136
return returnWrapper ( this . asyncSetSessionId ( sessionId , deviceId , options ) ) ;
136
137
}
137
138
138
139
async asyncSetSessionId ( sessionId : number , deviceId ?: string , options ?: { userProperties ?: { [ key : string ] : any } } ) {
140
+ this . stopRecordingEvents ( ) ;
141
+
139
142
const previousSessionId = this . identifiers && this . identifiers . sessionId ;
140
143
if ( previousSessionId ) {
141
144
this . sendEvents ( previousSessionId ) ;
@@ -152,7 +155,7 @@ export class SessionReplay implements AmplitudeSessionReplay {
152
155
if ( this . joinedConfigGenerator && previousSessionId ) {
153
156
this . config = await this . joinedConfigGenerator . generateJoinedConfig ( this . identifiers . sessionId ) ;
154
157
}
155
- await this . evaluateTargetingAndRecord ( { userProperties : options ?. userProperties } ) ;
158
+ await this . evaluateTargetingAndCapture ( { userProperties : options ?. userProperties } ) ;
156
159
}
157
160
158
161
getSessionReplayDebugPropertyValue ( ) {
@@ -171,7 +174,7 @@ export class SessionReplay implements AmplitudeSessionReplay {
171
174
return { } ;
172
175
}
173
176
174
- const shouldRecord = this . getShouldRecord ( ) ;
177
+ const shouldRecord = this . getShouldCapture ( ) ;
175
178
176
179
if ( shouldRecord ) {
177
180
const eventProperties : { [ key : string ] : string | null } = {
@@ -193,9 +196,8 @@ export class SessionReplay implements AmplitudeSessionReplay {
193
196
focusListener = ( ) => {
194
197
// Restart recording on focus to ensure that when user
195
198
// switches tabs, we take a full snapshot
196
- if ( this . sessionTargetingMatch ) {
197
- this . recordEvents ( ) ;
198
- }
199
+ this . stopRecordingEvents ( ) ;
200
+ this . captureEvents ( ) ;
199
201
} ;
200
202
201
203
/**
@@ -209,44 +211,35 @@ export class SessionReplay implements AmplitudeSessionReplay {
209
211
} ) ;
210
212
} ;
211
213
212
- evaluateTargetingAndRecord = async ( targetingParams ?: Pick < TargetingParameters , 'event' | 'userProperties' > ) => {
214
+ evaluateTargetingAndCapture = async ( targetingParams ?: Pick < TargetingParameters , 'event' | 'userProperties' > ) => {
213
215
if ( ! this . identifiers || ! this . identifiers . sessionId || ! this . config ) {
214
- if ( ! this . identifiers ? .sessionId ) {
215
- this . loggerProvider . warn ( 'Session ID has not been set, cannot evaluate targeting for Session Replay.' ) ;
216
+ if ( this . identifiers && ! this . identifiers . sessionId ) {
217
+ this . loggerProvider . log ( 'Session ID has not been set yet , cannot evaluate targeting for Session Replay.' ) ;
216
218
} else {
217
219
this . loggerProvider . warn ( 'Session replay init has not been called, cannot evaluate targeting.' ) ;
218
220
}
219
- return false ;
220
- }
221
- // Return early if a targeting match has already been made
222
- if ( this . sessionTargetingMatch ) {
223
- if ( ! this . recordCancelCallback ) {
224
- this . loggerProvider . log ( 'Session replay capture beginning due to targeting match.' ) ;
225
- this . recordEvents ( ) ;
226
- }
227
- return this . sessionTargetingMatch ;
228
- }
229
-
230
- let eventForTargeting = targetingParams ?. event ;
231
- if (
232
- eventForTargeting &&
233
- Object . values ( SpecialEventType ) . includes ( eventForTargeting . event_type as SpecialEventType )
234
- ) {
235
- eventForTargeting = undefined ;
221
+ return ;
236
222
}
237
223
238
- this . sessionTargetingMatch = await evaluateTargetingAndStore ( {
239
- sessionId : this . identifiers . sessionId ,
240
- config : this . config ,
241
- targetingParams : { userProperties : targetingParams ?. userProperties , event : eventForTargeting } ,
242
- } ) ;
224
+ if ( this . config . targetingConfig && ! this . sessionTargetingMatch ) {
225
+ let eventForTargeting = targetingParams ?. event ;
226
+ if (
227
+ eventForTargeting &&
228
+ Object . values ( SpecialEventType ) . includes ( eventForTargeting . event_type as SpecialEventType )
229
+ ) {
230
+ eventForTargeting = undefined ;
231
+ }
243
232
244
- if ( this . sessionTargetingMatch ) {
245
- this . loggerProvider . log ( 'Session replay capture beginning due to targeting match.' ) ;
246
- this . recordEvents ( ) ;
233
+ this . sessionTargetingMatch = await evaluateTargetingAndStore ( {
234
+ sessionId : this . identifiers . sessionId ,
235
+ targetingConfig : this . config . targetingConfig ,
236
+ loggerProvider : this . loggerProvider ,
237
+ apiKey : this . config . apiKey ,
238
+ targetingParams : { userProperties : targetingParams ?. userProperties , event : eventForTargeting } ,
239
+ } ) ;
247
240
}
248
241
249
- return this . sessionTargetingMatch ;
242
+ this . captureEvents ( ) ;
250
243
} ;
251
244
252
245
sendEvents ( sessionId ?: number ) {
@@ -268,9 +261,9 @@ export class SessionReplay implements AmplitudeSessionReplay {
268
261
return identityStoreOptOut !== undefined ? identityStoreOptOut : this . config ?. optOut ;
269
262
}
270
263
271
- getShouldRecord ( ) {
264
+ getShouldCapture ( ) {
272
265
if ( ! this . identifiers || ! this . config || ! this . identifiers . sessionId ) {
273
- this . loggerProvider . warn ( `Session is not being recorded due to lack of config, please call sessionReplay.init.` ) ;
266
+ this . loggerProvider . warn ( `Session is not being captured due to lack of config, please call sessionReplay.init.` ) ;
274
267
return false ;
275
268
}
276
269
if ( ! this . config . captureEnabled ) {
@@ -281,14 +274,35 @@ export class SessionReplay implements AmplitudeSessionReplay {
281
274
}
282
275
283
276
if ( this . shouldOptOut ( ) ) {
284
- this . loggerProvider . log ( `Opting session ${ this . identifiers . sessionId } out of recording due to optOut config.` ) ;
277
+ this . loggerProvider . log (
278
+ `Opting session ${ this . identifiers . sessionId } out of replay capture due to optOut config.` ,
279
+ ) ;
285
280
return false ;
286
281
}
287
282
288
- const isInSample = isSessionInSample ( this . identifiers . sessionId , this . config . sampleRate ) ;
289
- if ( ! isInSample ) {
290
- this . loggerProvider . log ( `Opting session ${ this . identifiers . sessionId } out of recording due to sample rate.` ) ;
291
- return false ;
283
+ // If targetingConfig exists, we'll use the sessionTargetingMatch to determine whether to record
284
+ // Otherwise, we'll evaluate the session against the overall sample rate
285
+ if ( this . config . targetingConfig ) {
286
+ if ( ! this . sessionTargetingMatch ) {
287
+ this . loggerProvider . log (
288
+ `Not capturing replays for session ${ this . identifiers . sessionId } due to not matching targeting conditions.` ,
289
+ ) ;
290
+ return false ;
291
+ }
292
+ this . loggerProvider . log (
293
+ `Capturing replays for session ${ this . identifiers . sessionId } due to matching targeting conditions.` ,
294
+ ) ;
295
+ } else {
296
+ const isInSample = isSessionInSample ( this . identifiers . sessionId , this . config . sampleRate ) ;
297
+ if ( ! isInSample ) {
298
+ this . loggerProvider . log (
299
+ `Opting session ${ this . identifiers . sessionId } out of replay capture due to sample rate.` ,
300
+ ) ;
301
+ return false ;
302
+ }
303
+ this . loggerProvider . log (
304
+ `Capturing replays for session ${ this . identifiers . sessionId } due to inclusion in sample rate.` ,
305
+ ) ;
292
306
}
293
307
294
308
return true ;
@@ -318,20 +332,25 @@ export class SessionReplay implements AmplitudeSessionReplay {
318
332
return maskSelector as unknown as string ;
319
333
}
320
334
321
- recordEvents ( ) {
322
- const shouldRecord = this . getShouldRecord ( ) ;
323
- const sessionId = this . identifiers ?. sessionId ;
335
+ captureEvents ( ) {
336
+ if ( this . recordCancelCallback ) {
337
+ this . loggerProvider . debug ( 'captureEvents method fired - Session Replay capture already in progress.' ) ;
338
+ return ;
339
+ }
340
+
341
+ const shouldRecord = this . getShouldCapture ( ) ;
342
+ const sessionId = this . identifiers && this . identifiers . sessionId ;
324
343
if ( ! shouldRecord || ! sessionId || ! this . config ) {
325
344
return ;
326
345
}
327
- this . stopRecordingEvents ( ) ;
346
+
328
347
const privacyConfig = this . config . privacyConfig ;
329
348
330
349
this . loggerProvider . log ( 'Session Replay capture beginning.' ) ;
331
350
this . recordCancelCallback = record ( {
332
351
emit : ( event ) => {
333
352
if ( this . shouldOptOut ( ) ) {
334
- this . loggerProvider . log ( `Opting session ${ sessionId } out of recording due to optOut config.` ) ;
353
+ this . loggerProvider . log ( `Opting session ${ sessionId } out of replay capture due to optOut config.` ) ;
335
354
this . stopRecordingEvents ( ) ;
336
355
this . sendEvents ( ) ;
337
356
return ;
0 commit comments