-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathsplit.js
More file actions
83 lines (72 loc) · 1.96 KB
/
split.js
File metadata and controls
83 lines (72 loc) · 1.96 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
// Split.js, Clientside A/B testing library
// v0.1.2
// http://github.com/splitrb/split.js
// (c) 2011 Andrew Nesbitt [[email protected]]
// released under the MIT license
var _gaq = _gaq || []
Split = (function(){
var config = {},
defaults = {
cookieName: 'abTest',
cookieAge: 30,
customVariableName: 'AB Test alternative',
customVariableIndex: 1
}
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
var nameEQ = name + "=";
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,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
function eraseCookie(name) {
createCookie(name,"",-1);
}
function getKeys(obj)
{
var keys = [];
for(var i in obj) if (obj.hasOwnProperty(i))
{
keys.push(i);
}
return keys;
}
function init(){
alternatives = arguments[0]
options = arguments[1]
keys = getKeys(alternatives);
config = {}
if(options != undefined){
for(k in defaults){
config[k] = (options[k] != undefined) ? options[k] : defaults[k];
}
}else{
config = defaults;
}
var alternative = readCookie(config.cookieName)
if (alternative) {
} else {
alternative = keys[Math.floor(Math.random()*keys.length)]
createCookie(config.cookieName, alternative, config.cookieAge)
}
_gaq.push(['_setCustomVar', config.customVariableIndex, config.customVariableName, alternative, 1]);
alternatives[alternative]();
}
return {
setup: function(args, options){
init(args, options)
}
}
})();