@@ -18,8 +18,7 @@ import {
1818import { version as PACKAGE_VERSION } from '../package.json' ;
1919
2020import { Defaults , ExperimentConfig } from './config' ;
21- import { ConnectorUserProvider } from './integration/connector' ;
22- import { DefaultUserProvider } from './integration/default' ;
21+ import { IntegrationManager } from './integration/manager' ;
2322import {
2423 getFlagStorage ,
2524 getVariantStorage ,
@@ -31,6 +30,7 @@ import { FetchHttpClient, WrapperClient } from './transport/http';
3130import { exposureEvent } from './types/analytics' ;
3231import { Client , FetchOptions } from './types/client' ;
3332import { Exposure , ExposureTrackingProvider } from './types/exposure' ;
33+ import { ExperimentPlugin , IntegrationPlugin } from './types/plugin' ;
3434import { ExperimentUserProvider } from './types/provider' ;
3535import { isFallback , Source , VariantSource } from './types/source' ;
3636import { ExperimentUser } from './types/user' ;
@@ -84,6 +84,7 @@ export class ExperimentClient implements Client {
8484 flagPollerIntervalMillis ,
8585 ) ;
8686 private isRunning = false ;
87+ private readonly integrationManager : IntegrationManager ;
8788
8889 // Deprecated
8990 private analyticsProvider : SessionAnalyticsProvider | undefined ;
@@ -136,6 +137,7 @@ export class ExperimentClient implements Client {
136137 this . config . exposureTrackingProvider ,
137138 ) ;
138139 }
140+ this . integrationManager = new IntegrationManager ( this . config , this ) ;
139141 // Setup Remote APIs
140142 const httpClient = new WrapperClient (
141143 this . config . httpClient || FetchHttpClient ,
@@ -252,7 +254,9 @@ export class ExperimentClient implements Client {
252254 options ,
253255 ) ;
254256 } catch ( e ) {
255- console . error ( e ) ;
257+ if ( this . config . debug ) {
258+ console . error ( e ) ;
259+ }
256260 }
257261 return this ;
258262 }
@@ -677,7 +681,7 @@ export class ExperimentClient implements Client {
677681 timeoutMillis : number ,
678682 options ?: FetchOptions ,
679683 ) : Promise < Variants > {
680- user = await this . addContextOrWait ( user , 10000 ) ;
684+ user = await this . addContextOrWait ( user ) ;
681685 user = this . cleanUserPropsForFetch ( user ) ;
682686 this . debug ( '[Experiment] Fetch variants for user: ' , user ) ;
683687 const results = await this . evaluationApi . getVariants ( user , {
@@ -756,28 +760,25 @@ export class ExperimentClient implements Client {
756760
757761 private addContext ( user : ExperimentUser ) : ExperimentUser {
758762 const providedUser = this . userProvider ?. getUser ( ) ;
763+ const integrationUser = this . integrationManager . getUser ( ) ;
759764 const mergedUserProperties = {
760- ...user ?. user_properties ,
761765 ...providedUser ?. user_properties ,
766+ ...integrationUser . user_properties ,
767+ ...user ?. user_properties ,
762768 } ;
763769 return {
764770 library : `experiment-js-client/${ PACKAGE_VERSION } ` ,
765- ...this . userProvider ?. getUser ( ) ,
771+ ...providedUser ,
772+ ...integrationUser ,
766773 ...user ,
767774 user_properties : mergedUserProperties ,
768775 } ;
769776 }
770777
771778 private async addContextOrWait (
772779 user : ExperimentUser ,
773- ms : number ,
774780 ) : Promise < ExperimentUser > {
775- if ( this . userProvider instanceof DefaultUserProvider ) {
776- if ( this . userProvider . userProvider instanceof ConnectorUserProvider ) {
777- await this . userProvider . userProvider . identityReady ( ms ) ;
778- }
779- }
780-
781+ await this . integrationManager . ready ( ) ;
781782 return this . addContext ( user ) ;
782783 }
783784
@@ -798,6 +799,12 @@ export class ExperimentClient implements Client {
798799 }
799800
800801 private exposureInternal ( key : string , sourceVariant : SourceVariant ) : void {
802+ // Variant metadata may disable exposure tracking remotely.
803+ const trackExposure =
804+ ( sourceVariant . variant ?. metadata ?. trackExposure as boolean ) ?? true ;
805+ if ( ! trackExposure ) {
806+ return ;
807+ }
801808 this . legacyExposureInternal (
802809 key ,
803810 sourceVariant . variant ,
@@ -823,6 +830,7 @@ export class ExperimentClient implements Client {
823830 }
824831 if ( metadata ) exposure . metadata = metadata ;
825832 this . exposureTrackingProvider ?. track ( exposure ) ;
833+ this . integrationManager . track ( exposure ) ;
826834 }
827835
828836 private legacyExposureInternal (
@@ -855,6 +863,16 @@ export class ExperimentClient implements Client {
855863 }
856864 return true ;
857865 }
866+
867+ /**
868+ * Add a plugin to the experiment client.
869+ * @param plugin the plugin to add.
870+ */
871+ public addPlugin ( plugin : ExperimentPlugin ) : void {
872+ if ( plugin . type === 'integration' ) {
873+ this . integrationManager . setIntegration ( plugin as IntegrationPlugin ) ;
874+ }
875+ }
858876}
859877
860878type SourceVariant = {
0 commit comments