import { HostExtractor } from '../util/HostExtractor'; export class OptimizelyService { private isEnabled: boolean; private experimentList: { id: string, name: string }[]; constructor(private $window: OptimizelyWindow, $location: angular.ILocationService, host: HostExtractor) { // Query parameter to completely disable optimizely, and it's only enabled if the global var exists // Also disable experiments for move in, for CSA mode and for communities if ($location.search()['optimizely_disable'] || !$window.optimizely || !$window.optimizely.allExperiments || host.mode === 'MoveIn' || host.isAgentMode || host.retailer !== 'OVO') this.isEnabled = false; else { this.isEnabled = true; // Store the experiment map as a list so it's easier to filter by experiment name this.experimentList = Object.keys($window.optimizely.allExperiments).map(id => ({ id: id, name: $window.optimizely.allExperiments[id].name })); } } getActiveVariation(experimentName: string): string { if (this.isEnabled) { let experimentId = this.experimentList.filter(e => e.name === experimentName).map(e => e.id)[0]; // No need to activate it if it's already in this list if (experimentId && this.$window.optimizely.activeExperiments.indexOf(experimentId) === -1) this.$window.optimizely.push(['activate', experimentId]); return this.$window.optimizely.variationNamesMap[experimentId] || 'Original'; } else return 'Original'; } } export interface Optimizely { activeExperiments: string[], allExperiments: { [id: string]: { name: string } }, variationNamesMap: { [id: string]: string } push(params: string[]): void } export interface OptimizelyWindow extends angular.IWindowService { optimizely: Optimizely; }