-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCookbook Random.html
More file actions
86 lines (76 loc) · 2.78 KB
/
Cookbook Random.html
File metadata and controls
86 lines (76 loc) · 2.78 KB
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Redirecting...</title>
<script type="text/javascript">
/*
This file randomly redirects a user to one of the URLs in a list. It is built
with the intent of randomization in a controlled experiment.
For example, create three Google Docs forms (or equivalent) and add their URLs
to the list below. That's it!
The user is then automatically and randomly assigned to one of the URLs when
the user visits this file. If the user decides to close and reopen the browser,
the user is still redirected to the same URL since a cookie is saved in the
user's browser.
Peter M. Dahlgren
@peterdalle
peterdahlgren.com
2016-01-05
*/
// List of URLs (two or more) for the experimental treatment and control groups.
var RedirectURLs = {
"urls" : [
{ "url" : "https://sites.google.com/view/mad-cookbook/recipes/appetizers-dips/cashew-queso" },
{ "url" : "https://sites.google.com/view/mad-cookbook/recipes/breads/beer-bread" },
{ "url" : "https://sites.google.com/view/mad-cookbook/recipes/soups-stews/broccoli-cheddar-soup" }
]
}
// Count the number of URLs in list.
var Groups = RedirectURLs.urls.length;
// Randomize number and get the corresponding index for the URL in list.
var gotoUrl = RedirectURLs.urls[GetRandomNumber(Groups)].url
// Perform redirection.
if (window.location.search == "?debug") {
// Debug mode: If user supplies a ?debug querystring,
// then only show the redirect URL, but don't actually redirect.
document.write("Redirect URL: " + gotoUrl);
} else {
// Redirect the user to the new URL.
document.location = gotoUrl;
}
// Perform randomization and save it to a cookie.
function GetRandomNumber(Groups) {
// Has cookie been set?
var rndIndex = null;
if (GetCookie() != "") {
// Yes, get randomization from cookie.
rndIndex = GetCookie()
} else {
// No, set random value to cookie.
rndIndex = Math.floor((Math.random() * Groups));
SetCookie(rndIndex, 30) // Set cookie for 30 days.
}
return rndIndex
}
// Set cookie with randomization value.
function SetCookie(CookieValue, DaysUntilExpire) {
var d = new Date();
d.setTime(d.getTime() + (DaysUntilExpire * 24 * 60 * 60 * 1000));
document.cookie = "rnd=" + CookieValue + ";expires=" + d.toUTCString();
}
// Get cookie with randomization value. Return empty string if not set.
function GetCookie() {
var ca = document.cookie.split(';');
for(var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') c = c.substring(1);
if (c.indexOf("rnd=") == 0) return c.substring(4, c.length);
}
return "";
}
</script>
</head>
<body>
</body>
</html>