Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add integration plugin #126

Merged
merged 22 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 22 additions & 12 deletions packages/experiment-browser/src/experimentClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ import {
import { version as PACKAGE_VERSION } from '../package.json';

import { Defaults, ExperimentConfig } from './config';
import { ConnectorUserProvider } from './integration/connector';
import { DefaultUserProvider } from './integration/default';
import { IntegrationManager } from './integration/manager';
import {
getFlagStorage,
getVariantStorage,
Expand All @@ -31,6 +30,7 @@ import { FetchHttpClient, WrapperClient } from './transport/http';
import { exposureEvent } from './types/analytics';
import { Client, FetchOptions } from './types/client';
import { Exposure, ExposureTrackingProvider } from './types/exposure';
import { ExperimentPlugin, IntegrationPlugin } from './types/plugin';
import { ExperimentUserProvider } from './types/provider';
import { isFallback, Source, VariantSource } from './types/source';
import { ExperimentUser } from './types/user';
Expand Down Expand Up @@ -84,6 +84,7 @@ export class ExperimentClient implements Client {
flagPollerIntervalMillis,
);
private isRunning = false;
private readonly integrationManager: IntegrationManager;

// Deprecated
private analyticsProvider: SessionAnalyticsProvider | undefined;
Expand Down Expand Up @@ -136,6 +137,7 @@ export class ExperimentClient implements Client {
this.config.exposureTrackingProvider,
);
}
this.integrationManager = new IntegrationManager(this.config, this);
// Setup Remote APIs
const httpClient = new WrapperClient(
this.config.httpClient || FetchHttpClient,
Expand Down Expand Up @@ -677,7 +679,7 @@ export class ExperimentClient implements Client {
timeoutMillis: number,
options?: FetchOptions,
): Promise<Variants> {
user = await this.addContextOrWait(user, 10000);
user = await this.addContextOrWait(user);
user = this.cleanUserPropsForFetch(user);
this.debug('[Experiment] Fetch variants for user: ', user);
const results = await this.evaluationApi.getVariants(user, {
Expand Down Expand Up @@ -756,28 +758,25 @@ export class ExperimentClient implements Client {

private addContext(user: ExperimentUser): ExperimentUser {
const providedUser = this.userProvider?.getUser();
const integrationUser = this.integrationManager.getUser();
const mergedUserProperties = {
...user?.user_properties,
...providedUser?.user_properties,
...integrationUser.user_properties,
...user?.user_properties,
};
return {
library: `experiment-js-client/${PACKAGE_VERSION}`,
...this.userProvider?.getUser(),
...providedUser,
...integrationUser,
...user,
user_properties: mergedUserProperties,
};
}

private async addContextOrWait(
user: ExperimentUser,
ms: number,
): Promise<ExperimentUser> {
if (this.userProvider instanceof DefaultUserProvider) {
if (this.userProvider.userProvider instanceof ConnectorUserProvider) {
await this.userProvider.userProvider.identityReady(ms);
}
}

await this.integrationManager.ready();
return this.addContext(user);
}

Expand Down Expand Up @@ -823,6 +822,7 @@ export class ExperimentClient implements Client {
}
if (metadata) exposure.metadata = metadata;
this.exposureTrackingProvider?.track(exposure);
this.integrationManager.track(exposure);
}

private legacyExposureInternal(
Expand Down Expand Up @@ -855,6 +855,16 @@ export class ExperimentClient implements Client {
}
return true;
}

/**
* Add a plugin to the experiment client.
* @param plugin the plugin to add.
*/
public addPlugin(plugin: ExperimentPlugin): void {
if (plugin.type === 'integration') {
this.integrationManager.setIntegration(plugin as IntegrationPlugin);
}
}
}

type SourceVariant = {
Expand Down
33 changes: 14 additions & 19 deletions packages/experiment-browser/src/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@ import { AnalyticsConnector } from '@amplitude/analytics-connector';

import { Defaults, ExperimentConfig } from './config';
import { ExperimentClient } from './experimentClient';
import {
ConnectorExposureTrackingProvider,
ConnectorUserProvider,
} from './integration/connector';
import { DefaultUserProvider } from './integration/default';
import { AmplitudeIntegrationPlugin } from './integration/amplitude';
import { DefaultUserProvider } from './providers/default';

const instances = {};

Expand All @@ -25,15 +22,10 @@ const initialize = (
// initializing multiple default instances for different api keys.
const instanceName = config?.instanceName || Defaults.instanceName;
const instanceKey = `${instanceName}.${apiKey}`;
const connector = AnalyticsConnector.getInstance(instanceName);
if (!instances[instanceKey]) {
config = {
...config,
userProvider: new DefaultUserProvider(
connector.applicationContextProvider,
config?.userProvider,
apiKey,
),
userProvider: new DefaultUserProvider(config?.userProvider, apiKey),
};
instances[instanceKey] = new ExperimentClient(apiKey, config);
}
Expand Down Expand Up @@ -63,17 +55,20 @@ const initializeWithAmplitudeAnalytics = (
if (!instances[instanceKey]) {
connector.eventBridge.setInstanceName(instanceName);
config = {
userProvider: new DefaultUserProvider(
connector.applicationContextProvider,
new ConnectorUserProvider(connector.identityStore),
userProvider: new DefaultUserProvider(undefined, apiKey),
...config,
};
const client = new ExperimentClient(apiKey, config);
client.addPlugin(
new AmplitudeIntegrationPlugin(
apiKey,
),
exposureTrackingProvider: new ConnectorExposureTrackingProvider(
connector.identityStore,
connector.eventBridge,
connector.applicationContextProvider,
10000,
),
...config,
};
instances[instanceKey] = new ExperimentClient(apiKey, config);
);
instances[instanceKey] = client;
if (config.automaticFetchOnAmplitudeIdentityChange) {
connector.identityStore.addIdentityListener(() => {
instances[instanceKey].fetch();
Expand Down
2 changes: 1 addition & 1 deletion packages/experiment-browser/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export { ExperimentConfig } from './config';
export {
AmplitudeUserProvider,
AmplitudeAnalyticsProvider,
} from './integration/amplitude';
} from './providers/amplitude';
export { Experiment } from './factory';
export { StubExperimentClient } from './stubClient';
export { ExperimentClient } from './experimentClient';
Expand Down
Loading
Loading