-
Notifications
You must be signed in to change notification settings - Fork 13
/
study.js
312 lines (265 loc) · 9.54 KB
/
study.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/**
studyjs - A client side A/B tester
@version v5.0.3
@link https://github.com/dollarshaveclub/study
@author Jacob Kelley <[email protected]>
@license MIT
**/
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global = global || self, global.Study = factory());
}(this, function () { 'use strict';
var rand = function rand(min, max) {
return Math.random() * (max - min) + min;
}; // choose a random value with the specified weights
var chooseWeightedItem = function chooseWeightedItem(names, weights) {
if (names.length !== weights.length) throw new Error('names and weights must have equal length!');
var sum = weights.reduce(function (a, b) {
return a + b;
}, 0);
var limit = 0;
var n = rand(0, sum);
for (var i = 0; i < names.length; i++) {
limit += weights[i];
if (n <= limit) return names[i];
} // by default, return the last weight
return names[names.length - 1];
}; // get the default bucket,
// which is either the default/winner,
// otherwise whichever is returned first
var getDefaultBucket = function getDefaultBucket(buckets) {
var defaultBuckets = Object.keys(buckets).filter(function (name) {
var x = buckets[name];
return x["default"] || x.winner;
});
return defaultBuckets[0] || Object.keys(buckets)[0];
};
var validateStore = function validateStore(store) {
if (!store) throw new Error('You must supply a store!');
if (typeof store.get !== 'function') throw new Error('The store must implement .get()');
if (typeof store.set !== 'function') throw new Error('The store must implement .set()');
if (typeof store.isSupported !== 'function') throw new Error('The store must implement .isSupported()');
if (!store.isSupported()) throw new Error('The store is not supported.');
};
var getRandomAssignment = function getRandomAssignment(test) {
var names = Object.keys(test.buckets);
var weights = [];
names.forEach(function (innerBucketName) {
var weight = test.buckets[innerBucketName].weight;
if (weight == null) weight = 1;
weights.push(weight);
});
return chooseWeightedItem(names, weights);
};
var Study =
/*#__PURE__*/
function () {
function Study(options) {
if (options === void 0) {
options = {};
}
Object.assign(this, {
storageKey: 'ab-tests',
root: typeof document !== 'undefined' ? document.body : null
}, options);
validateStore(this.store);
this.previousAssignments = {};
try {
// assert that the data is a JSON string
// that represents a JSON object
// saw a bug where it was, for some reason, stored as `null`
var data = this.store.get(this.storageKey);
if (typeof data === 'string' && data[0] === '{') {
this.previousAssignments = JSON.parse(data);
}
} catch (_) {// ignore
}
this.userAssignments = {};
this.persistedUserAssignments = {};
this.providedTests = [];
}
var _proto = Study.prototype;
_proto.define = function define(tests) {
var _this = this;
var normalizedData = tests;
if (!Array.isArray(tests)) normalizedData = [tests];
normalizedData.forEach(function (test) {
if (!test.name) throw new Error('Tests must have a name');
if (!test.buckets) throw new Error('Tests must have buckets');
if (!Object.keys(test.buckets)) throw new Error('Tests must have buckets');
_this.providedTests.push(test);
});
};
_proto.definitions = function definitions() {
return this.providedTests;
};
_proto.removeClasses = function removeClasses(testName, exceptClassName) {
try {
var root = this.root;
if (!root) return; // classList does not support returning all classes
var currentClassNames = root.className.split(/\s+/g).map(function (x) {
return x.trim();
}).filter(Boolean);
currentClassNames.filter(function (x) {
return x.indexOf(testName + "--") === 0;
}).filter(function (className) {
return className !== exceptClassName;
}).forEach(function (className) {
return root.classList.remove(className);
});
} catch (_) {// Ignore
}
};
_proto.applyClasses = function applyClasses() {
var _this2 = this;
try {
var userAssignments = this.userAssignments,
root = this.root;
if (!root) return;
Object.keys(userAssignments).forEach(function (testName) {
var bucket = userAssignments[testName];
var className = bucket ? testName + "--" + bucket : null; // remove all classes related to this bucket
_this2.removeClasses(testName, className); // only assign a class is the test is assigned to a bucket
// this removes then adds a class, which is not ideal but is clean
if (className) root.classList.add(className);
});
} catch (_) {// Ignore
}
};
_proto.assignAll = function assignAll() {
var previousAssignments = this.previousAssignments,
userAssignments = this.userAssignments,
persistedUserAssignments = this.persistedUserAssignments;
this.providedTests.forEach(function (test) {
// winners take precedence
{
var winner = Object.keys(test.buckets).filter(function (name) {
return test.buckets[name].winner;
})[0];
if (winner) {
userAssignments[test.name] = winner;
return;
}
} // already assigned, probably because someone
// called `.assignAll()` twice.
if (userAssignments[test.name]) return;
{
// previously assigned, so we continue to persist it
var bucket = previousAssignments[test.name];
if (bucket && test.buckets[bucket]) {
var assignment = previousAssignments[test.name];
persistedUserAssignments[test.name] = assignment;
userAssignments[test.name] = assignment;
test.active = true;
return;
}
} // inactive tests should be set to default
if (test.active === false) {
userAssignments[test.name] = getDefaultBucket(test.buckets);
return;
} // randomly assign
{
var _assignment = getRandomAssignment(test);
persistedUserAssignments[test.name] = _assignment;
userAssignments[test.name] = _assignment;
}
});
this.persist();
this.applyClasses();
};
_proto.assign = function assign(testName, bucketName) {
if (!testName) return this.assignAll();
var test = this.providedTests.filter(function (x) {
return x.name === testName;
})[0];
if (bucketName === null || !test) {
delete this.userAssignments[testName];
delete this.persistedUserAssignments[testName];
this.persist();
this.removeClasses(testName);
return;
}
var assignment = bucketName || getRandomAssignment(test);
this.userAssignments[testName] = assignment;
this.persistedUserAssignments[testName] = assignment;
test.active = true;
this.persist();
this.applyClasses();
};
_proto.extendAssignments = function extendAssignments(assignments) {
return assignments;
};
_proto.assignments = function assignments() {
return this.extendAssignments(this.userAssignments);
};
_proto.persist = function persist() {
this.store.set(this.storageKey, JSON.stringify(this.persistedUserAssignments));
};
return Study;
}();
// NOTE: use a module
var browserCookie = (function () {
return {
type: 'browserCookie',
/*eslint-disable */
get: function get(key) {
return decodeURIComponent(document.cookie.replace(new RegExp("(?:(?:^|.*;)\\s*" + encodeURIComponent(key).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*([^;]*).*$)|^.*$"), "$1")) || null;
},
set: function set(key, val) {
var expirationDate = new Date('12/31/9999').toUTCString();
document.cookie = encodeURIComponent(key) + "=" + encodeURIComponent(val) + "; expires=" + expirationDate + "; path=/";
},
/* eslint-enable */
isSupported: function isSupported() {
return typeof document !== 'undefined';
}
};
});
var local = (function () {
return {
type: 'local',
get: function get(key) {
return localStorage.getItem(key);
},
set: function set(key, val) {
return localStorage.setItem(key, val);
},
isSupported: function isSupported() {
if (typeof localStorage !== 'undefined') return true;
var uid = new Date();
try {
localStorage.setItem(uid, uid);
localStorage.removeItem(uid);
return true;
} catch (e) {
return false;
}
}
};
});
var memory = (function () {
var store = Object.create(null);
return {
type: 'memory',
get: function get(key) {
return store[key];
},
set: function set(key, val) {
store[key] = val;
},
isSupported: function isSupported() {
return true;
}
};
});
// this is the build for webpack and UMD builds
var stores = {
browserCookie: browserCookie(),
local: local(),
memory: memory()
};
window.Study = Study;
Study.stores = stores;
return Study;
}));