From 5e254cc26fe1000ea902a5869e4a60919fe6183c Mon Sep 17 00:00:00 2001 From: Brian Giori Date: Thu, 19 Dec 2024 17:31:38 -0800 Subject: [PATCH] feat: add options to configure variant function call --- .../experiment-browser/src/experimentClient.ts | 16 +++++++++++++--- packages/experiment-browser/src/types/client.ts | 12 +++++++++++- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/packages/experiment-browser/src/experimentClient.ts b/packages/experiment-browser/src/experimentClient.ts index 9daacf87..6b841ed3 100644 --- a/packages/experiment-browser/src/experimentClient.ts +++ b/packages/experiment-browser/src/experimentClient.ts @@ -29,7 +29,7 @@ import { import { LocalStorage } from './storage/local-storage'; import { FetchHttpClient, WrapperClient } from './transport/http'; import { exposureEvent } from './types/analytics'; -import { Client, FetchOptions } from './types/client'; +import { Client, FetchOptions, VariantOptions } from './types/client'; import { Exposure, ExposureTrackingProvider } from './types/exposure'; import { ExperimentPlugin, IntegrationPlugin } from './types/plugin'; import { ExperimentUserProvider } from './types/provider'; @@ -286,15 +286,25 @@ export class ExperimentClient implements Client { * * @param key The key to get the variant for. * @param fallback The highest priority fallback. + * @param options Additional options to configure the function behavior. * @see ExperimentConfig * @see ExposureTrackingProvider + * @see VariantOptions */ - public variant(key: string, fallback?: string | Variant): Variant { + public variant( + key: string, + fallback?: string | Variant | undefined, + options?: VariantOptions | undefined, + ): Variant { if (!this.apiKey) { return { value: undefined }; } + const shouldTrackExposure = + (this.config.automaticExposureTracking && + options.trackExposure !== false) || + options.trackExposure === true; const sourceVariant = this.variantAndSource(key, fallback); - if (this.config.automaticExposureTracking) { + if (shouldTrackExposure) { this.exposureInternal(key, sourceVariant); } this.debug( diff --git a/packages/experiment-browser/src/types/client.ts b/packages/experiment-browser/src/types/client.ts index e677e4be..541e10f8 100644 --- a/packages/experiment-browser/src/types/client.ts +++ b/packages/experiment-browser/src/types/client.ts @@ -1,4 +1,5 @@ import { ExperimentUserProvider } from './provider'; +import { Source } from './source'; import { ExperimentUser } from './user'; import { Variant, Variants } from './variant'; @@ -6,6 +7,11 @@ export type FetchOptions = { flagKeys?: string[]; }; +export type VariantOptions = { + trackExposure?: boolean; + source: Source; +}; + /** * Interface for the main client. * @category Core Usage @@ -14,7 +20,11 @@ export interface Client { start(user?: ExperimentUser): Promise; stop(): void; fetch(user?: ExperimentUser, options?: FetchOptions): Promise; - variant(key: string, fallback?: string | Variant): Variant; + variant( + key: string, + fallback?: string | Variant | undefined, + options?: VariantOptions | undefined, + ): Variant; all(): Variants; clear(): void; exposure(key: string): void;