-
Notifications
You must be signed in to change notification settings - Fork 1
/
Abrantes.js
210 lines (190 loc) · 5.99 KB
/
Abrantes.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
/* jshint esversion:6 */
const Abrantes = Object.create(null);
Abrantes.testId = undefined;
Abrantes.variant = undefined;
Abrantes.version = "0.19+";
/**
* Assigns a variant to a user
* @param {string} testId
*/
Abrantes.assignVariant = function (testId, trafficAllocation = 1) {
if (typeof (testId) !== "string" || testId.length === 0) {
throw ("You need to provide an ID when assigning a variant");
}
this.testId = testId;
if (typeof (this.readPersistent()) === "number") {
this.variant = this.readPersistent();
window.dispatchEvent(new CustomEvent("abrantes:assignVariant", {
detail: {
testId: this.testId,
variant: this.variant
}
}));
return;
}
const n = Math.random();
if (n > trafficAllocation) {
this.variant = -1;
} else {
this.variant = this.randomVar();
window.dispatchEvent(new CustomEvent("abrantes:assignVariant", {
detail: {
testId: this.testId,
variant: this.variant
}
}));
}
};
/**
* Imports the variant from the url
* @param {string} testId
*/
Abrantes.importVariant = function (testId) {
if (typeof (testId) !== "string" || testId.length === 0) {
throw ("You need to provide an ID when importing a variant");
}
this.testId = testId;
const urlParams = new URLSearchParams(window.location.search);
if (urlParams.has(testId)) {
this.variant = Number(urlParams.get(testId));
window.dispatchEvent(new CustomEvent("abrantes:assignVariant", {
detail: {
testId: this.testId,
variant: this.variant
}
}));
} else {
this.variant = -1;
}
};
/**
* Adds the variant to the URL, to be imported in a different domain
* @param {string} linkURLstring
* @returns {string}
*/
Abrantes.makeCrossSiteURL = function (linkURLstring) {
const linkURL = new URL(linkURLstring);
const windowSearch = new URLSearchParams(window.location.search);
windowSearch.forEach(function (v, k) {
if (!linkURL.searchParams.has(k)) {
linkURL.searchParams.set(k, v);
}
});
linkURL.searchParams.set(this.testId, this.variant);
return linkURL.href;
};
/**
* It transforms the hrefs of the <a> in the selector to make cross site experiments
* @param {string} selector Css selector
*/
Abrantes.crossSiteLink = function (selector) {
const self = this;
const addLinks = function (selector) {
const elements = document.querySelectorAll(selector);
elements.forEach(function (element) {
const elHref = element.getAttribute("href")
const newHref = self.makeCrossSiteURL(elHref);
element.setAttribute("href", newHref);
});
};
if (this.settings.crossSiteLink.triggerEvent) {
window.addEventListener(this.settings.crossSiteLink.triggerEvent, function () {
addLinks(selector);
});
} else {
addLinks(selector);
}
};
/**
* Renders the variant and adds the class variant-x to the <body> tag
* @param {Number} variant
*/
Abrantes.renderVariant = function (variant = this.variant) {
if (variant === -1) {
return;
}
if (typeof (this.variants[variant]) === "function") {
this.variants[variant]();
document.getElementsByTagName("body")[0].classList.add(this.testId + "-" + variant);
} else {
throw ("The variant " + variant + " does not exist");
}
};
/**
* Stores the test id (object name) and variant in localStorage
*/
Abrantes.persist = function (context) {
if (context === "user" || context=="local") {
localStorage.setItem(this.testId, this.variant);
} else if (context === "session") {
sessionStorage.setItem(this.testId, this.variant);
} else if ( context === "cookie" ) {
document.cookie = `${this.testId}=${this.variant}; expires=${new Date(Date.now() + 86400000 * this.settings.cookie.expires).toUTCString()}; path=/; SameSite=Strict;`;
} else {
throw ("You must use either 'user', 'session' or 'cookie' with persist");
}
};
/**
* Reads the value in the storage
* @returns
*/
Abrantes.readPersistent = function () {
const sessionData = sessionStorage.getItem(this.testId);
if (typeof (sessionData) === "string" || typeof (sessionData) === "number") {
return Number(sessionData);
}
const userData = localStorage.getItem(this.testId);
if (typeof (userData) === "string" || typeof (userData) === "number") {
return Number(userData);
}
const cookieData = document.cookie.split('; ').find(row => row.startsWith(this.testId + '='));
if (typeof (cookieData) === "string") {
return Number(cookieData.split('=')[1]);
}
return undefined;
};
/**
* Object where the user can read and write the settings
*/
Abrantes.settings = {
crossSiteLink: {
triggerEvent: "DOMContentLoaded"
},
cookie: {
expires: 7
}
};
/**
* Array of functions with the changes of each variant. The variants are also defined in an object that extends Abrantes.
*/
Abrantes.variants = [
];
/**
* Selects a random variation from the list of available variants
* @returns number
*/
Abrantes.randomVar = function () {
const numberOfVariants = this.variants.length;
if (window.crypto && window.crypto.getRandomValues) {
const values = new Uint32Array(1);
window.crypto.getRandomValues(values);
this.variant = values[0] % (numberOfVariants);
}
else {
this.variant = Math.floor(Math.random() * (numberOfVariants));
}
return this.variant;
};
/**
* Redirects to another url maintaining the url params and the hash
* @param {string} url
*/
Abrantes.redirectTo = function (url) {
location.href = url + location.search + location.hash;
}
/**
* Tracks the user using whatever is defined in an Abrantes plugin
*/
Abrantes.track = function () {
throw ("Missing plugin to track results");
};