@@ -2,7 +2,7 @@ import { getAnalyticsConnector, getGlobalScope } from '@amplitude/analytics-clie
22import  {  Logger ,  returnWrapper  }  from  '@amplitude/analytics-core' ; 
33import  {  Logger  as  ILogger  }  from  '@amplitude/analytics-types' ; 
44import  {  pack ,  record  }  from  '@amplitude/rrweb' ; 
5- import  {  TargetingParameters  }  from  '@amplitude/targeting' ; 
5+ import  {  TargetingParameters ,   evaluateTargeting  }  from  '@amplitude/targeting' ; 
66import  {  createSessionReplayJoinedConfigGenerator  }  from  './config/joined-config' ; 
77import  {  SessionReplayJoinedConfig ,  SessionReplayJoinedConfigGenerator  }  from  './config/types' ; 
88import  { 
@@ -14,11 +14,10 @@ import {
1414import  {  createEventsManager  }  from  './events/events-manager' ; 
1515import  {  generateHashCode ,  isSessionInSample ,  maskFn  }  from  './helpers' ; 
1616import  {  SessionIdentifiers  }  from  './identifiers' ; 
17+ import  *  as  TargetingIDBStore  from  './targeting-idb-store' ; 
1718import  { 
1819  AmplitudeSessionReplay , 
1920  SessionReplayEventsManager  as  AmplitudeSessionReplayEventsManager , 
20-   SessionReplayRemoteConfigFetch  as  AmplitudeSessionReplayRemoteConfigFetch , 
21-   SessionReplaySessionIDBStore  as  AmplitudeSessionReplaySessionIDBStore , 
2221  SessionIdentifiers  as  ISessionIdentifiers , 
2322  SessionReplayOptions , 
2423}  from  './typings/session-replay' ; 
@@ -28,11 +27,10 @@ export class SessionReplay implements AmplitudeSessionReplay {
2827  config : SessionReplayJoinedConfig  |  undefined ; 
2928  joinedConfigGenerator : SessionReplayJoinedConfigGenerator  |  undefined ; 
3029  identifiers : ISessionIdentifiers  |  undefined ; 
31-   remoteConfigFetch : AmplitudeSessionReplayRemoteConfigFetch  |  undefined ; 
3230  eventsManager : AmplitudeSessionReplayEventsManager  |  undefined ; 
33-   sessionIDBStore : AmplitudeSessionReplaySessionIDBStore  |  undefined ; 
3431  loggerProvider : ILogger ; 
3532  recordCancelCallback : ReturnType < typeof  record >  |  null  =  null ; 
33+   sessionTargetingMatch  =  false ; 
3634
3735  constructor ( )  { 
3836    this . loggerProvider  =  new  Logger ( ) ; 
@@ -104,7 +102,7 @@ export class SessionReplay implements AmplitudeSessionReplay {
104102    } 
105103
106104    if  ( globalScope  &&  globalScope . document  &&  globalScope . document . hasFocus ( ) )  { 
107-       this . initialize ( true ) ; 
105+       await   this . initialize ( true ) ; 
108106    } 
109107  } 
110108
@@ -171,20 +169,51 @@ export class SessionReplay implements AmplitudeSessionReplay {
171169  } ; 
172170
173171  focusListener  =  ( )  =>  { 
174-     this . initialize ( ) ; 
172+     void   this . initialize ( ) ; 
175173  } ; 
176174
177175  evaluateTargeting  =  async  ( targetingParams ?: Pick < TargetingParameters ,  'event'  |  'userProperties' > )  =>  { 
178-     if  ( ! this . identifiers  ||  ! this . identifiers . sessionId  ||  ! this . remoteConfigFetch   ||   ! this . config )  { 
176+     if  ( ! this . identifiers  ||  ! this . identifiers . sessionId  ||  ! this . config )  { 
179177      this . loggerProvider . error ( 'Session replay init has not been called, cannot evaluate targeting.' ) ; 
180178      return ; 
181179    } 
182180
183-     await  this . remoteConfigFetch . evaluateTargeting ( { 
181+     const  idbTargetingMatch  =  await  TargetingIDBStore . getTargetingMatchForSession ( { 
182+       loggerProvider : this . config . loggerProvider , 
183+       apiKey : this . config . apiKey , 
184184      sessionId : this . identifiers . sessionId , 
185-       deviceId : this . getDeviceId ( ) , 
186-       ...targetingParams , 
187185    } ) ; 
186+     if  ( idbTargetingMatch  ===  true )  { 
187+       this . sessionTargetingMatch  =  true ; 
188+       return ; 
189+     } 
190+ 
191+     // Finally evaluate targeting if previous two checks were false or undefined 
192+     try  { 
193+       if  ( this . config . targetingConfig )  { 
194+         const  targetingResult  =  evaluateTargeting ( { 
195+           ...targetingParams , 
196+           flag : this . config . targetingConfig , 
197+           sessionId : this . identifiers . sessionId , 
198+         } ) ; 
199+         this . sessionTargetingMatch  = 
200+           this . sessionTargetingMatch  ===  false  &&  targetingResult . sr_targeting_config . key  ===  'on' ; 
201+       }  else  { 
202+         // If the targeting config is undefined or an empty object, 
203+         // assume the response was valid but no conditions were set, 
204+         // so all users match targeting 
205+         this . sessionTargetingMatch  =  true ; 
206+       } 
207+       void  TargetingIDBStore . storeTargetingMatchForSession ( { 
208+         loggerProvider : this . config . loggerProvider , 
209+         apiKey : this . config . apiKey , 
210+         sessionId : this . identifiers . sessionId , 
211+         targetingMatch : this . sessionTargetingMatch , 
212+       } ) ; 
213+     }  catch  ( err : unknown )  { 
214+       const  knownError  =  err  as  Error ; 
215+       this . config . loggerProvider . warn ( knownError . message ) ; 
216+     } 
188217  } ; 
189218
190219  stopRecordingAndSendEvents ( sessionId ?: number )  { 
@@ -198,7 +227,7 @@ export class SessionReplay implements AmplitudeSessionReplay {
198227      this . eventsManager . sendCurrentSequenceEvents ( {  sessionId : sessionIdToSend ,  deviceId } ) ; 
199228  } 
200229
201-   initialize ( shouldSendStoredEvents  =  false )  { 
230+   async   initialize ( shouldSendStoredEvents  =  false )  { 
202231    if  ( ! this . identifiers ?. sessionId )  { 
203232      this . loggerProvider . log ( `Session is not being recorded due to lack of session id.` ) ; 
204233      return ; 
0 commit comments