-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathab-angular.js
109 lines (86 loc) · 2.71 KB
/
ab-angular.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
angular.module('abAngular', ['ngCookies'])
.service('abAngularService', ['$cookies', function($cookies) {
var runningExperiment = null;
var experimentsDefinitions = [];
var storageKey = 'defaultStorageKey';
var analyticsHandlingCallback = null;
function initializeService(options) {
setExperimentsDefinitions(options.experimentsDefinitions);
setStorageKey(options.storageKey);
analyticsHandlingCallback = options.analyticsHandlingCallback;
runningExperiment = getExperimentFromStorage();
if (!experimentsDefinitions.length) {
return;
}
if (!runningExperiment) {
var fairDiceRoll = Math.floor(Math.random() * experimentsDefinitions.length);
var selectedExperiment = experimentsDefinitions[fairDiceRoll];
var variant = 'A';
if (selectedExperiment.displayProbability < Math.random()) {
variant = 'B';
}
runningExperiment = {
name: selectedExperiment.name,
variant: variant
};
$cookies.putObject(storageKey, runningExperiment);
}
if (analyticsHandlingCallback) {
var fullExperimentInfo = experimentsDefinitions.filter(function (experiment) {
return experiment.name === runningExperiment.name;
});
analyticsHandlingCallback(fullExperimentInfo[0], runningExperiment.variant);
}
}
function getExperimentFromStorage() {
var experimentFromStorage = $cookies.getObject(storageKey);
if (experimentFromStorage) {
var exists = experimentsDefinitions.filter(function(experiment) {
return experiment.name === experimentFromStorage.name
});
if(!exists.length) {
$cookies.remove(storageKey);
experimentFromStorage = null;
}
}
return experimentFromStorage;
}
function setStorageKey(key) {
storageKey = key;
runningExperiment = getExperimentFromStorage();
}
function setExperimentsDefinitions(definitions) {
experimentsDefinitions = definitions;
}
function initializeExperiment(experimentName) {
if(runningExperiment && runningExperiment.name === experimentName) {
return runningExperiment.variant;
}
return null;
}
return {
'initializeExperiment': initializeExperiment,
'initializeService': initializeService
};
}])
.component('experiment', {
template: [
'<div ng-transclude="variantA" ng-if="$ctrl.selectedVariant === \'A\'"></div>',
'<div ng-transclude="variantB" ng-if="$ctrl.selectedVariant === \'B\'"></div>'
].join(''),
bindings: {
name: '<'
},
transclude: {
variantA: 'variantA',
variantB: 'variantB'
},
controller: function (abAngularService) {
var vm = this;
vm.selectedVariant = null;
vm.$onInit = function () {
selectedVariant = abAngularService.initializeExperiment(vm.name);
vm.selectedVariant = selectedVariant ? selectedVariant : 'A';
}
}
});