Skip to content

Commit

Permalink
feat: add feature experiment support in web script (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
bgiori authored Dec 3, 2024
1 parent a9dc4e4 commit bf60c05
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
9 changes: 6 additions & 3 deletions packages/experiment-browser/src/factory.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { AnalyticsConnector } from '@amplitude/analytics-connector';
import { safeGlobal } from '@amplitude/experiment-core';

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

const instances = {};
// Global instances for debugging.
safeGlobal.experimentInstances = {};
const instances = safeGlobal.experimentInstances;

const getInstanceName = (config: ExperimentConfig): string => {
return config?.instanceName || Defaults.instanceName;
Expand All @@ -18,7 +21,7 @@ const getInstanceName = (config: ExperimentConfig): string => {
* @param apiKey The deployment API Key
* @param config See {@link ExperimentConfig} for config options
*/
const initialize = (
export const initialize = (
apiKey: string,
config?: ExperimentConfig,
): ExperimentClient => {
Expand Down Expand Up @@ -52,7 +55,7 @@ const initialize = (
* @param apiKey The deployment API Key
* @param config See {@link ExperimentConfig} for config options
*/
const initializeWithAmplitudeAnalytics = (
export const initializeWithAmplitudeAnalytics = (
apiKey: string,
config?: ExperimentConfig,
): ExperimentClient => {
Expand Down
6 changes: 5 additions & 1 deletion packages/experiment-browser/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ export {
AmplitudeAnalyticsProvider,
} from './providers/amplitude';
export { AmplitudeIntegrationPlugin } from './integration/amplitude';
export { Experiment } from './factory';
export {
Experiment,
initialize,
initializeWithAmplitudeAnalytics,
} from './factory';
export { StubExperimentClient } from './stubClient';
export { ExperimentClient } from './experimentClient';
export { Client, FetchOptions } from './types/client';
Expand Down
4 changes: 4 additions & 0 deletions packages/experiment-tag/src/experiment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import {
getGlobalScope,
isLocalStorageAvailable,
} from '@amplitude/experiment-core';
import { safeGlobal } from '@amplitude/experiment-core';
import {
Experiment,
ExperimentUser,
Variant,
Variants,
AmplitudeIntegrationPlugin,
} from '@amplitude/experiment-js-client';
import * as FeatureExperiment from '@amplitude/experiment-js-client';
import mutate, { MutationController } from 'dom-mutator';

import { getInjectUtils } from './inject-utils';
Expand All @@ -24,6 +26,8 @@ import {
concatenateQueryParamsOf,
} from './util';

safeGlobal.Experiment = FeatureExperiment;

const appliedInjections: Set<string> = new Set();
const appliedMutations: MutationController[] = [];
let previousUrl: string | undefined;
Expand Down
5 changes: 5 additions & 0 deletions packages/experiment-tag/test/experiment.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as coreUtil from '@amplitude/experiment-core';
import { safeGlobal } from '@amplitude/experiment-core';
import { ExperimentClient } from '@amplitude/experiment-js-client';
import { initializeExperiment } from 'src/experiment';
import * as experiment from 'src/experiment';
Expand Down Expand Up @@ -736,3 +737,7 @@ describe('initializeExperiment', () => {
expect(mockExposure).not.toHaveBeenCalled();
});
});

test('feature experiment on global Experiment object', () => {
expect(safeGlobal.Experiment).toBeDefined();
});

0 comments on commit bf60c05

Please sign in to comment.