-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathexperiments.js
387 lines (358 loc) · 11.8 KB
/
experiments.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/**
* Copyright 2015 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @fileoverview Experiments system allows a developer to opt-in to test
* features that are not yet fully tested.
*
* Experiments page: https://cdn.ampproject.org/experiments.html *
*/
import {dev, user} from './log';
import {getMode} from './mode';
import {hasOwn} from './utils/object';
import {parseQueryString} from './url';
/** @const {string} */
const TAG = 'EXPERIMENTS';
/** @const {string} */
const LOCAL_STORAGE_KEY = 'amp-experiment-toggles';
/** @const {string} */
const TOGGLES_WINDOW_PROPERTY = '__AMP__EXPERIMENT_TOGGLES';
/**
* @typedef {{
* isTrafficEligible: function(!Window):boolean,
* branches: !Array<string>
* }}
*/
export let ExperimentInfo;
/**
* Whether we are in canary.
* @param {!Window} win
* @return {boolean}
*/
export function isCanary(win) {
return !!(win.AMP_CONFIG && win.AMP_CONFIG.canary);
}
/**
* Returns binary type, e.g., canary, production, control, or rc.
* @param {!Window} win
* @return {string}
*/
export function getBinaryType(win) {
return win.AMP_CONFIG && win.AMP_CONFIG.type
? win.AMP_CONFIG.type
: 'unknown';
}
/**
* Whether the specified experiment is on or off.
* @param {!Window} win
* @param {string} experimentId
* @return {boolean}
*/
export function isExperimentOn(win, experimentId) {
const toggles = experimentToggles(win);
return !!toggles[experimentId];
}
/**
* Toggles the experiment on or off. Returns the actual value of the experiment
* after toggling is done.
* @param {!Window} win
* @param {string} experimentId
* @param {boolean=} opt_on
* @param {boolean=} opt_transientExperiment Whether to toggle the
* experiment state "transiently" (i.e., for this page load only) or
* durably (by saving the experiment IDs after toggling).
* Default: false (save durably).
* @return {boolean} New state for experimentId.
*/
export function toggleExperiment(
win,
experimentId,
opt_on,
opt_transientExperiment
) {
const currentlyOn = isExperimentOn(win, /*OK*/ experimentId);
const on = !!(opt_on !== undefined ? opt_on : !currentlyOn);
if (on != currentlyOn) {
const toggles = experimentToggles(win);
toggles[experimentId] = on;
if (!opt_transientExperiment) {
const storedToggles = getExperimentToggles(win);
storedToggles[experimentId] = on;
saveExperimentToggles(win, storedToggles);
// Avoid affecting tests that spy/stub warn().
if (!getMode().test) {
user().warn(
TAG,
'"%s" experiment %s for the domain "%s". See: https://amp.dev/documentation/guides-and-tutorials/learn/experimental',
experimentId,
on ? 'enabled' : 'disabled',
win.location.hostname
);
}
}
}
return on;
}
/**
* Calculate whether the experiment is on or off based off of its default value,
* stored overriden value, or the global config frequency given.
* @param {!Window} win
* @return {!Object<string, boolean>}
*/
export function experimentToggles(win) {
if (win[TOGGLES_WINDOW_PROPERTY]) {
return win[TOGGLES_WINDOW_PROPERTY];
}
win[TOGGLES_WINDOW_PROPERTY] = Object.create(null);
const toggles = win[TOGGLES_WINDOW_PROPERTY];
// Read the default config of this build.
if (win.AMP_CONFIG) {
for (const experimentId in win.AMP_CONFIG) {
const frequency = win.AMP_CONFIG[experimentId];
if (typeof frequency === 'number' && frequency >= 0 && frequency <= 1) {
toggles[experimentId] = Math.random() < frequency;
}
}
}
// Read document level override from meta tag.
if (
win.AMP_CONFIG &&
Array.isArray(win.AMP_CONFIG['allow-doc-opt-in']) &&
win.AMP_CONFIG['allow-doc-opt-in'].length > 0
) {
const allowed = win.AMP_CONFIG['allow-doc-opt-in'];
const meta = win.document.head.querySelector(
'meta[name="amp-experiments-opt-in"]'
);
if (meta) {
const optedInExperiments = meta.getAttribute('content').split(',');
for (let i = 0; i < optedInExperiments.length; i++) {
if (allowed.indexOf(optedInExperiments[i]) != -1) {
toggles[optedInExperiments[i]] = true;
}
}
}
}
Object.assign(toggles, getExperimentToggles(win));
if (
win.AMP_CONFIG &&
Array.isArray(win.AMP_CONFIG['allow-url-opt-in']) &&
win.AMP_CONFIG['allow-url-opt-in'].length > 0
) {
const allowed = win.AMP_CONFIG['allow-url-opt-in'];
const hash = win.location.originalHash || win.location.hash;
const params = parseQueryString(hash);
for (let i = 0; i < allowed.length; i++) {
const param = params[`e-${allowed[i]}`];
if (param == '1') {
toggles[allowed[i]] = true;
}
if (param == '0') {
toggles[allowed[i]] = false;
}
}
}
return toggles;
}
/**
* Returns the cached experiments toggles, or null if they have not been
* computed yet.
* @param {!Window} win
* @return {Object<string, boolean>}
*/
export function experimentTogglesOrNull(win) {
return win[TOGGLES_WINDOW_PROPERTY] || null;
}
/**
* Returns a set of experiment IDs currently on.
* @param {!Window} win
* @return {!Object<string, boolean>}
*/
function getExperimentToggles(win) {
let experimentsString = '';
try {
if ('localStorage' in win) {
experimentsString = win.localStorage.getItem(LOCAL_STORAGE_KEY);
}
} catch (e) {
dev().warn(TAG, 'Failed to retrieve experiments from localStorage.');
}
const tokens = experimentsString ? experimentsString.split(/\s*,\s*/g) : [];
const toggles = Object.create(null);
for (let i = 0; i < tokens.length; i++) {
if (tokens[i].length == 0) {
continue;
}
if (tokens[i][0] == '-') {
toggles[tokens[i].substr(1)] = false;
} else {
toggles[tokens[i]] = true;
}
}
return toggles;
}
/**
* Saves a set of experiment IDs currently on.
* @param {!Window} win
* @param {!Object<string, boolean>} toggles
*/
function saveExperimentToggles(win, toggles) {
const experimentIds = [];
for (const experiment in toggles) {
experimentIds.push((toggles[experiment] === false ? '-' : '') + experiment);
}
try {
if ('localStorage' in win) {
win.localStorage.setItem(LOCAL_STORAGE_KEY, experimentIds.join(','));
}
} catch (e) {
user().error(TAG, 'Failed to save experiments to localStorage.');
}
}
/**
* See getExperimentToggles().
* @param {!Window} win
* @return {!Object<string, boolean>}
* @visibleForTesting
*/
export function getExperimentTogglesForTesting(win) {
return getExperimentToggles(win);
}
/**
* Resets the experimentsToggle cache for testing purposes.
* @param {!Window} win
* @visibleForTesting
*/
export function resetExperimentTogglesForTesting(win) {
saveExperimentToggles(win, {});
win[TOGGLES_WINDOW_PROPERTY] = null;
}
/**
* In some browser implementations of Math.random(), sequential calls of
* Math.random() are correlated and can cause a bias. In particular,
* if the previous random() call was < 0.001 (as it will be if we select
* into an experiment), the next value could be less than 0.5 more than
* 50.7% of the time. This provides an implementation that roots down into
* the crypto API, when available, to produce less biased samples.
*
* @return {number} Pseudo-random floating-point value on the range [0, 1).
*/
function slowButAccuratePrng() {
// TODO(tdrl): Implement.
return Math.random();
}
/**
* Container for alternate random number generator implementations. This
* allows us to set an "accurate" PRNG for branch selection, but to mock it
* out easily in tests.
*
* @visibleForTesting
* @const {!{accuratePrng: function():number}}
*/
export const RANDOM_NUMBER_GENERATORS = {
accuratePrng: slowButAccuratePrng,
};
/**
* Selects, uniformly at random, a single item from the array.
* @param {!Array<string>} arr Object to select from.
* @return {?string} Single item from arr or null if arr was empty.
*/
function selectRandomItem(arr) {
const rn = RANDOM_NUMBER_GENERATORS.accuratePrng();
return arr[Math.floor(rn * arr.length)] || null;
}
/**
* Selects which page-level experiment branches are enabled. If a given
* experiment name is already set (including to the null / no branches selected
* state), this won't alter its state.
*
* Check whether a given experiment is set using isExperimentOn(win,
* experimentName) and, if it is on, look for which branch is selected in
* win.__AMP_EXPERIMENT_BRANCHES[experimentName].
*
* @param {!Window} win Window context on which to save experiment
* selection state.
* @param {!Object<string, !ExperimentInfo>} experiments Set of experiments to
* configure for this page load.
* @return {!Object<string, string>} Map of experiment names to selected
* branches.
*/
export function randomlySelectUnsetExperiments(win, experiments) {
win.__AMP_EXPERIMENT_BRANCHES = win.__AMP_EXPERIMENT_BRANCHES || {};
const selectedExperiments = {};
for (const experimentName in experiments) {
// Skip experimentName if it is not a key of experiments object or if it
// has already been populated by some other property.
if (!hasOwn(experiments, experimentName)) {
continue;
}
if (hasOwn(win.__AMP_EXPERIMENT_BRANCHES, experimentName)) {
selectedExperiments[experimentName] =
win.__AMP_EXPERIMENT_BRANCHES[experimentName];
continue;
}
if (
!experiments[experimentName].isTrafficEligible ||
!experiments[experimentName].isTrafficEligible(win)
) {
win.__AMP_EXPERIMENT_BRANCHES[experimentName] = null;
continue;
}
// If we're in the experiment, but we haven't already forced a specific
// experiment branch (e.g., via a test setup), then randomize the branch
// choice.
if (
!win.__AMP_EXPERIMENT_BRANCHES[experimentName] &&
isExperimentOn(win, /*OK*/ experimentName)
) {
const {branches} = experiments[experimentName];
win.__AMP_EXPERIMENT_BRANCHES[experimentName] = selectRandomItem(
branches
);
selectedExperiments[experimentName] =
win.__AMP_EXPERIMENT_BRANCHES[experimentName];
}
}
return selectedExperiments;
}
/**
* Returns the experiment branch enabled for the given experiment ID.
* For example, 'control' or 'experiment'.
*
* @param {!Window} win Window context to check for experiment state.
* @param {string} experimentName Name of the experiment to check.
* @return {?string} Active experiment branch ID for experimentName (possibly
* null if experimentName has been tested but no branch was enabled).
*/
export function getExperimentBranch(win, experimentName) {
return win.__AMP_EXPERIMENT_BRANCHES
? win.__AMP_EXPERIMENT_BRANCHES[experimentName]
: null;
}
/**
* Force enable (or disable) a specific branch of a given experiment name.
* Disables the experiment name altogether if branchId is falseish.
*
* @param {!Window} win Window context to check for experiment state.
* @param {string} experimentName Name of the experiment to check.
* @param {?string} branchId ID of branch to force or null to disable
* altogether.
* @visibleForTesting
*/
export function forceExperimentBranch(win, experimentName, branchId) {
win.__AMP_EXPERIMENT_BRANCHES = win.__AMP_EXPERIMENT_BRANCHES || {};
toggleExperiment(win, experimentName, !!branchId, true);
win.__AMP_EXPERIMENT_BRANCHES[experimentName] = branchId;
}