ngx-growthbook
Version:
Angular library for GrowthBook
1 lines • 12.1 kB
Source Map (JSON)
{"version":3,"file":"ngx-growthbook.mjs","sources":["../../../projects/ngx-growthbook/src/lib/ngx-growthbook.tokens.ts","../../../projects/ngx-growthbook/src/lib/ngx-growthbook.service.ts","../../../projects/ngx-growthbook/src/lib/ngx-growthbook.providers.ts","../../../projects/ngx-growthbook/src/public-api.ts","../../../projects/ngx-growthbook/src/ngx-growthbook.ts"],"sourcesContent":["import { InjectionToken } from '@angular/core';\nimport { NgxGrowthbookConfiguration } from './ngx-growthbook.config';\n\nexport const NGX_GROWTHBOOK_CONFIG_TOKEN =\n new InjectionToken<NgxGrowthbookConfiguration>('ngx-growthbook.config');\n","import { Injectable, OnDestroy, Inject } from '@angular/core';\nimport {\n GrowthBook,\n Context,\n Attributes,\n RefreshFeaturesOptions,\n FeatureResult,\n} from '@growthbook/growthbook';\nimport { Subject, takeUntil, Observable } from 'rxjs';\nimport { NGX_GROWTHBOOK_CONFIG_TOKEN } from './ngx-growthbook.tokens';\nimport {\n NgxGrowthbookConfiguration,\n TrackingService,\n} from './ngx-growthbook.config';\n\nexport { type FeatureResult, type Result } from '@growthbook/growthbook';\n\n@Injectable({\n providedIn: 'root',\n})\nexport class NgxGrowthbookService implements OnDestroy {\n private growthBook!: GrowthBook;\n private readonly growthBookSource = new Subject<number>();\n private eventCount = 0;\n private readonly destroy$ = new Subject<void>();\n\n constructor(\n @Inject(NGX_GROWTHBOOK_CONFIG_TOKEN)\n private config: NgxGrowthbookConfiguration\n ) {}\n\n ngOnDestroy() {\n this.growthBookSource.unsubscribe();\n this.destroy$.next();\n this.destroy$.complete();\n }\n\n async init(config: Context, trackingService?: TrackingService | null) {\n if (!config.clientKey) {\n throw new Error('GrowthBook clientKey is required');\n }\n\n console.log('Initializing GrowthBook...');\n\n this.growthBook = new GrowthBook({\n trackingCallback: trackingService\n ? (experiment, result) => {\n trackingService?.trackExperiment(experiment.key, result);\n }\n : config.trackingCallback,\n onFeatureUsage: trackingService\n ? (featureKey, result) => {\n trackingService?.trackFeature?.(featureKey, result);\n }\n : config.onFeatureUsage,\n ...config,\n });\n\n await this.growthBook.init({ skipCache: true, streaming: true });\n\n this.growthBook.setRenderer(() => {\n this.triggerUpdate();\n });\n\n console.log('...GrowthBook initialized');\n\n return this.growthBook;\n }\n\n triggerUpdate(): void {\n this.growthBookSource.next((this.eventCount += 1));\n }\n\n subscribe(callback: (n: number) => void) {\n callback(0);\n return this.growthBookSource\n .pipe(takeUntil(this.destroy$))\n .subscribe(() => {\n callback;\n });\n }\n\n public getFeature<T>(featureKey: string, defaultValue?: T): FeatureResult<T> {\n return this.evaluateFeatureSync<T>(featureKey, defaultValue);\n }\n\n /**\n * Evaluates a feature and returns an observable that will emit whenever the feature changes\n * @param featureKey The feature key to evaluate\n * @param defaultValue The default value if the feature is not found\n */\n public evaluateFeature<T>(featureKey: string, defaultValue?: T) {\n const evaluate = () => {\n const result = this.growthBook.evalFeature(featureKey);\n return result;\n };\n\n // Initial evaluation\n const initial = evaluate();\n\n // Create an observable that emits whenever features are updated\n return new Observable<FeatureResult<T>>((subscriber) => {\n subscriber.next(initial);\n\n const subscription = this.growthBookSource\n .pipe(takeUntil(this.destroy$))\n .subscribe(() => {\n subscriber.next(evaluate());\n });\n\n return () => subscription.unsubscribe();\n });\n }\n\n /**\n * Evaluates a feature and returns the current value synchronously\n * @param featureKey The feature key to evaluate\n * @param defaultValue The default value if the feature is not found\n */\n public evaluateFeatureSync<T>(\n featureKey: string,\n defaultValue?: T\n ): FeatureResult<T> {\n const result = this.growthBook.evalFeature(featureKey);\n return result;\n }\n\n public loadFeatures(): Promise<void> {\n return this.growthBook.loadFeatures();\n }\n\n public isOn(featureKey: string): boolean {\n return this.growthBook.isOn(featureKey);\n }\n\n public isOff(featureKey: string): boolean {\n return this.growthBook.isOff(featureKey);\n }\n\n public setFeatureValue(featureKey: string) {\n if (featureKey) {\n this.growthBook.setFeatures({\n featureKey: { defaultValue: true },\n });\n }\n }\n\n public getFeatureValue(featureKey: string, defaultValue: any) {\n return this.growthBook.getFeatureValue(featureKey, defaultValue);\n }\n\n /**\n * In addition to the isOn and getFeatureValue helper methods,\n * there is the evalFeature method that gives you more detailed\n * information about why the value was assigned to the user.\n * @param featureKey\n * @returns\n */\n public evalFeature(featureKey: string) {\n return this.growthBook.evalFeature(featureKey);\n }\n\n /**\n * Completely replaces attributes\n * @param attributes\n */\n public setAttributes(attributes: Attributes) {\n return this.growthBook.setAttributes(attributes);\n }\n\n public updateAttributes(attributes: Attributes) {\n const existingAttributes = this.getAttributes();\n return this.growthBook.setAttributes({\n ...existingAttributes,\n ...attributes,\n });\n }\n\n public refreshFeatures(options?: RefreshFeaturesOptions) {\n return this.growthBook.refreshFeatures(options);\n }\n\n public setURL(url: string) {\n return this.growthBook.setURL(url);\n }\n\n public getAttributes() {\n return this.growthBook.getAttributes();\n }\n\n public getFeatures() {\n return this.growthBook.getFeatures();\n }\n\n public getExperiments() {\n return this.growthBook.getExperiments();\n }\n}\n","import {\n inject,\n provideAppInitializer,\n Provider,\n makeEnvironmentProviders,\n} from '@angular/core';\nimport { NGX_GROWTHBOOK_CONFIG_TOKEN } from './ngx-growthbook.tokens';\nimport { NgxGrowthbookService } from './ngx-growthbook.service';\nimport { NgxGrowthbookConfiguration } from './ngx-growthbook.config';\n\nexport function provideNgxGrowthbook(config: NgxGrowthbookConfiguration) {\n if (!config.clientKey) {\n throw new Error('GrowthBook clientKey is required');\n }\n\n const providers: Provider[] = [\n {\n provide: NGX_GROWTHBOOK_CONFIG_TOKEN,\n useValue: config,\n },\n NgxGrowthbookService,\n ];\n\n if (config.trackingService) {\n providers.push(config.trackingService);\n }\n\n return makeEnvironmentProviders([\n ...providers,\n provideAppInitializer(async () => {\n const growthbookService = inject(NgxGrowthbookService);\n const trackingService = config.trackingService\n ? inject(config.trackingService)\n : null;\n await growthbookService.init(config, trackingService);\n }),\n ]);\n}\n","/*\n * Public API Surface of ngx-growthbook\n */\n\nexport * from './lib/ngx-growthbook.service';\nexport * from './lib/ngx-growthbook.providers';\nexport * from './lib/ngx-growthbook.tokens';\nexport * from './lib/ngx-growthbook.config';\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './public-api';\n"],"names":[],"mappings":";;;;;MAGa,2BAA2B,GACtC,IAAI,cAAc,CAA6B,uBAAuB;;MCgB3D,oBAAoB,CAAA;AAQrB,IAAA,MAAA;AAPF,IAAA,UAAU;AACD,IAAA,gBAAgB,GAAG,IAAI,OAAO,EAAU;IACjD,UAAU,GAAG,CAAC;AACL,IAAA,QAAQ,GAAG,IAAI,OAAO,EAAQ;AAE/C,IAAA,WAAA,CAEU,MAAkC,EAAA;QAAlC,IAAM,CAAA,MAAA,GAAN,MAAM;;IAGhB,WAAW,GAAA;AACT,QAAA,IAAI,CAAC,gBAAgB,CAAC,WAAW,EAAE;AACnC,QAAA,IAAI,CAAC,QAAQ,CAAC,IAAI,EAAE;AACpB,QAAA,IAAI,CAAC,QAAQ,CAAC,QAAQ,EAAE;;AAG1B,IAAA,MAAM,IAAI,CAAC,MAAe,EAAE,eAAwC,EAAA;AAClE,QAAA,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE;AACrB,YAAA,MAAM,IAAI,KAAK,CAAC,kCAAkC,CAAC;;AAGrD,QAAA,OAAO,CAAC,GAAG,CAAC,4BAA4B,CAAC;AAEzC,QAAA,IAAI,CAAC,UAAU,GAAG,IAAI,UAAU,CAAC;AAC/B,YAAA,gBAAgB,EAAE;AAChB,kBAAE,CAAC,UAAU,EAAE,MAAM,KAAI;oBACrB,eAAe,EAAE,eAAe,CAAC,UAAU,CAAC,GAAG,EAAE,MAAM,CAAC;;kBAE1D,MAAM,CAAC,gBAAgB;AAC3B,YAAA,cAAc,EAAE;AACd,kBAAE,CAAC,UAAU,EAAE,MAAM,KAAI;oBACrB,eAAe,EAAE,YAAY,GAAG,UAAU,EAAE,MAAM,CAAC;;kBAErD,MAAM,CAAC,cAAc;AACzB,YAAA,GAAG,MAA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