@@ -21,6 +21,8 @@ import { AmplitudeServerZone, getEventLogApi } from './server-zone';
2121import ConfigManager from './config-manager' ;
2222import GlobalScope from './global-scope' ;
2323
24+ import { AnalyticsConnector } from '@amplitude/analytics-connector' ;
25+
2426/**
2527 * AmplitudeClient SDK API - instance constructor.
2628 * The Amplitude class handles creation of client instances, all you need to do is call amplitude.getInstance()
@@ -56,6 +58,9 @@ var AmplitudeClient = function AmplitudeClient(instanceName) {
5658 this . _sessionId = null ;
5759 this . _isInitialized = false ;
5860
61+ // used to integrate with experiment SDK (client-side exposure tracking & real-time user properties)
62+ this . _connector = null ;
63+
5964 this . _userAgent = ( navigator && navigator . userAgent ) || null ;
6065} ;
6166
@@ -80,6 +85,9 @@ AmplitudeClient.prototype.init = function init(apiKey, opt_userId, opt_config, o
8085 }
8186
8287 try {
88+ // used to integrate with experiment SDK (client-side exposure tracking & real-time user properties)
89+ this . _connector = AnalyticsConnector . getInstance ( this . _instanceName ) ;
90+
8391 _parseConfig ( this . options , opt_config ) ;
8492 if (
8593 ( isBrowserEnv ( ) || utils . isWebWorkerEnvironment ( ) ) &&
@@ -263,6 +271,21 @@ AmplitudeClient.prototype.init = function init(apiKey, opt_userId, opt_config, o
263271 ) ;
264272 }
265273 }
274+
275+ // Sets an event receiver to receive and forward exposure events from the experiment SDK.
276+ this . _connector . eventBridge . setEventReceiver ( ( event ) => {
277+ this . _logEvent ( event . eventType , event . eventProperties , event . userProperties ) ;
278+ } ) ;
279+
280+ // Set the user ID and device ID in the core identity store to enable fetching variants.
281+ const editor = this . _connector . identityStore . editIdentity ( ) ;
282+ if ( this . options . deviceId ) {
283+ editor . setDeviceId ( this . options . deviceId ) ;
284+ }
285+ if ( this . options . userId ) {
286+ editor . setUserId ( this . options . userId ) ;
287+ }
288+ editor . commit ( ) ;
266289 } catch ( err ) {
267290 utils . log . error ( err ) ;
268291 if ( opt_config && type ( opt_config . onError ) === 'function' ) {
@@ -925,6 +948,12 @@ AmplitudeClient.prototype.setUserId = function setUserId(userId, startNewSession
925948 }
926949
927950 _saveCookieData ( this ) ;
951+
952+ // Update core identity store to propagate new user info
953+ // to experiment SDK and trigger a fetch if the ID has changed.
954+ if ( this . _connector ) {
955+ this . _connector . identityStore . editIdentity ( ) . setUserId ( this . options . userId ) . commit ( ) ;
956+ }
928957 } catch ( e ) {
929958 utils . log . error ( e ) ;
930959 }
@@ -1054,6 +1083,12 @@ AmplitudeClient.prototype.setDeviceId = function setDeviceId(deviceId) {
10541083 if ( ! utils . isEmptyString ( deviceId ) ) {
10551084 this . options . deviceId = '' + deviceId ;
10561085 _saveCookieData ( this ) ;
1086+
1087+ // Update core identity store to propagate new user info
1088+ // to experiment SDK and trigger a fetch if the ID has changed.
1089+ if ( this . _connector ) {
1090+ this . _connector . identityStore . editIdentity ( ) . setDeviceId ( this . options . deviceId ) . commit ( ) ;
1091+ }
10571092 }
10581093 } catch ( e ) {
10591094 utils . log . error ( e ) ;
@@ -1395,6 +1430,15 @@ AmplitudeClient.prototype._logEvent = function _logEvent(
13951430
13961431 this . _sendEventsIfReady ( ) ;
13971432
1433+ // In the case of an identify event, update the core user store so the experiment SDK can fetch new variants and
1434+ // utilize user properties in real time.
1435+ if ( eventType === Constants . IDENTIFY_EVENT && this . _connector ) {
1436+ this . _connector . identityStore
1437+ . editIdentity ( )
1438+ . updateUserProperties ( utils . truncate ( utils . validateProperties ( userProperties ) ) )
1439+ . commit ( ) ;
1440+ }
1441+
13981442 return eventId ;
13991443 } catch ( e ) {
14001444 utils . log . error ( e ) ;
0 commit comments