@@ -21,6 +21,8 @@ import { AmplitudeServerZone, getEventLogApi } from './server-zone';
21
21
import ConfigManager from './config-manager' ;
22
22
import GlobalScope from './global-scope' ;
23
23
24
+ import { AnalyticsConnector } from '@amplitude/analytics-connector' ;
25
+
24
26
/**
25
27
* AmplitudeClient SDK API - instance constructor.
26
28
* 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) {
56
58
this . _sessionId = null ;
57
59
this . _isInitialized = false ;
58
60
61
+ // used to integrate with experiment SDK (client-side exposure tracking & real-time user properties)
62
+ this . _connector = null ;
63
+
59
64
this . _userAgent = ( navigator && navigator . userAgent ) || null ;
60
65
} ;
61
66
@@ -80,6 +85,9 @@ AmplitudeClient.prototype.init = function init(apiKey, opt_userId, opt_config, o
80
85
}
81
86
82
87
try {
88
+ // used to integrate with experiment SDK (client-side exposure tracking & real-time user properties)
89
+ this . _connector = AnalyticsConnector . getInstance ( this . _instanceName ) ;
90
+
83
91
_parseConfig ( this . options , opt_config ) ;
84
92
if (
85
93
( isBrowserEnv ( ) || utils . isWebWorkerEnvironment ( ) ) &&
@@ -263,6 +271,21 @@ AmplitudeClient.prototype.init = function init(apiKey, opt_userId, opt_config, o
263
271
) ;
264
272
}
265
273
}
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 ( ) ;
266
289
} catch ( err ) {
267
290
utils . log . error ( err ) ;
268
291
if ( opt_config && type ( opt_config . onError ) === 'function' ) {
@@ -925,6 +948,12 @@ AmplitudeClient.prototype.setUserId = function setUserId(userId, startNewSession
925
948
}
926
949
927
950
_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
+ }
928
957
} catch ( e ) {
929
958
utils . log . error ( e ) ;
930
959
}
@@ -1054,6 +1083,12 @@ AmplitudeClient.prototype.setDeviceId = function setDeviceId(deviceId) {
1054
1083
if ( ! utils . isEmptyString ( deviceId ) ) {
1055
1084
this . options . deviceId = '' + deviceId ;
1056
1085
_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
+ }
1057
1092
}
1058
1093
} catch ( e ) {
1059
1094
utils . log . error ( e ) ;
@@ -1395,6 +1430,15 @@ AmplitudeClient.prototype._logEvent = function _logEvent(
1395
1430
1396
1431
this . _sendEventsIfReady ( ) ;
1397
1432
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
+
1398
1442
return eventId ;
1399
1443
} catch ( e ) {
1400
1444
utils . log . error ( e ) ;
0 commit comments