-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathab_testing.js
106 lines (100 loc) · 3.07 KB
/
ab_testing.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
function createCookie(name, value, days) {
var expires = "";
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toUTCString();
}
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 createABtest() {
var randomChange = randomNumber % variants.length;
createCookie(prefix + '-cookie', variants[randomChange], 9);
if (variants[randomChange] != "0") {
var newcookie = variants[randomChange].split('.');
var changeID = newcookie[0];
var variantID = newcookie[1];
changes[changeID]['variants'][variantID].execute();
sendDimension(changeID, variantID);
} else {
sendDimension(0)
}
}
function sendDimension(changeID, variantID) {
if (readCookie('_ga')) {
if (changeID != 0) {
dataLayer.push({ 'event':'abTest', 'eventCategory': 'ab-test', 'eventAction': prefix + '-' + changeID + '-' + variantID, 'eventLabel': prefix + '-' + changeID + '-' + variantID, 'eventNonInteraction': 1 });
} else {
dataLayer.push({ 'event':'abTest', 'eventCategory': 'ab-test', 'eventAction': prefix + '-0-0', 'eventLabel': prefix + '-0-0', 'eventNonInteraction': 1 });
}
}
}
function checkCookie(name) {
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(name) > -1) return true;
}
return false;
}
function eraseCookie(name) {
createCookie(name, "", -1);
}
var prefix = 'tnw';
var randomNumber = {{Random Number}};
var changes = {
1: {
variants: {
1: {
execute: function () {
}
}
}
},
2: {
variants: {
1: {
execute: function () {
}
},
2: {
execute: function () {
}
}
}
}
};
var variants = ["0"];
for (var j in changes) {
for (var x in changes[j]['variants']) {
variants.push(j + '.' + x);
}
}
if (readCookie(prefix + '-cookie')) {
if (variants.indexOf(readCookie(prefix + '-cookie')) != -1) {
var currentCookie = readCookie(prefix + '-cookie').split('.');
var currentChangeID = currentCookie[0];
var currentVariantID = 0;
if (currentChangeID != 0) {
currentVariantID = currentCookie[1];
changes[currentChangeID]['variants'][currentVariantID].execute();
}
sendDimension(currentChangeID, currentVariantID);
} else {
eraseCookie(prefix + '-cookie');
createABtest();
}
} else {
createABtest();
}