forked from ampproject/amphtml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexperiments.js
217 lines (193 loc) · 6.31 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
/**
* 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 {getCookie, setCookie} from './cookies';
import {parseQueryString} from './url';
/** @const {string} */
const COOKIE_NAME = 'AMP_EXP';
/** @const {number} */
const COOKIE_MAX_AGE_DAYS = 180; // 6 month
/** @const {time} */
const COOKIE_EXPIRATION_INTERVAL = COOKIE_MAX_AGE_DAYS * 24 * 60 * 60 * 1000;
/** @type {Object<string, boolean>|undefined} */
let toggles_;
/**
* Whether we are in canary.
* @param {!Window} win
* @return {boolean}
*/
export function isCanary(win) {
return !!(win.AMP_CONFIG && win.AMP_CONFIG.canary);
}
/**
* 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];
}
/**
* Check whether an experiment is on while allowing viewers to force
* the experiment state via a viewer URL param of the form:
* `e-$experimentId=1` (on) or `e-$experimentId=0` (off).
* NOTE: This should only be used if it is needed and if turning the
* experiment on or off does not have security implications.
* @param {!Window} win
* @param {string} experimentId
* @return {boolean}
*/
export function isExperimentOnAllowUrlOverride(win, experimentId) {
const hash = win.location.originalHash || win.location.hash;
if (hash) {
// Note: If this is used a lot, this should be optimized to only
// parse once per page load.
const param = parseQueryString(hash)['e-' + experimentId];
if (param == '1') {
return true;
}
if (param == '0') {
return false;
}
}
return isExperimentOn(win, 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 to the cookie 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, experimentId);
const on = !!(opt_on !== undefined ? opt_on : !currentlyOn);
if (on != currentlyOn) {
const toggles = experimentToggles(win);
toggles[experimentId] = on;
if (!opt_transientExperiment) {
const cookieToggles = getExperimentTogglesFromCookie(win);
cookieToggles[experimentId] = on;
saveExperimentTogglesToCookie(win, cookieToggles);
}
}
return on;
}
/**
* Calculate whether the experiment is on or off based off of the
* cookieFlag or the global config frequency given.
* @param {!Window} win
* @return {!Object<string, boolean>}
*/
export function experimentToggles(win) {
if (toggles_) {
return toggles_;
}
toggles_ = Object.create(null);
// 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_, getExperimentTogglesFromCookie(win));
return toggles_;
}
/**
* Returns a set of experiment IDs currently on.
* @param {!Window} win
* @return {!Object<string, boolean>}
*/
function getExperimentTogglesFromCookie(win) {
const experimentCookie = getCookie(win, COOKIE_NAME);
const tokens = experimentCookie ? experimentCookie.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 saveExperimentTogglesToCookie(win, toggles) {
const experimentIds = [];
for (const experiment in toggles) {
experimentIds.push((toggles[experiment] === false ? '-' : '') + experiment);
}
setCookie(win, COOKIE_NAME, experimentIds.join(','),
Date.now() + COOKIE_EXPIRATION_INTERVAL, {
// Set explicit domain, so the cookie gets send to sub domains.
domain: win.location.hostname,
});
}
/**
* See getExperimentTogglesFromCookie().
* @param {!Window} win
* @return {!Object<string, boolean>}
* @visibleForTesting
*/
export function getExperimentToglesFromCookieForTesting(win) {
return getExperimentTogglesFromCookie(win);
}
/**
* Resets the experimentsToggle cache for testing purposes.
* @visibleForTesting
*/
export function resetExperimentTogglesForTesting() {
toggles_ = undefined;
}