-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathprojectjs.js
129 lines (127 loc) · 4.45 KB
/
projectjs.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
window.optlyHelper = (function () {
return {
v: "1.26",
logEnabled: false,
helperLog: function (msg) {
if (this.logEnabled) {
console.log("OPTLY HELPER (v" + this.v + ") ---- " + msg);
}
},
getCookieValue: function (a) {
var b = document.cookie.match('(^|[^;]+)\\s*' + a + '\\s*=\\s*([^;]+)');
return b ? b.pop() : '';
},
setCookieValue: function (cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
var expires = "expires=" + d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";domain=.{YOUR_DOMAIN}.com;path=/";
},
activeExperiments: {}
};
})();
// Module to save all Experiment Information, also from redirect cookie
window.optlyHelper.getExperimentInfo = (function () {
var activeExperiments,
redirectCookie,
redirectVariables;
function _getCookieValue(a) {
var b = document.cookie.match('(^|[^;]+)\\s*' + a + '\\s*=\\s*([^;]+)');
return b ? b.pop() : '';
}
function _getQueryVariable(cookieValue) {
var query = {};
var pairs = (cookieValue[0] === '?' ? cookieValue.substr(1) : cookieValue).split('&');
for (var i = 0; i < pairs.length; i++) {
var pair = pairs[i].split('=');
query[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1] || '');
}
return query;
}
function _writeRedirectInfo(r) {
optlyHelper.redirectInfo = r;
}
function init() {
optlyHelper.helperLog("ADDING EXPERIMENT INFORMATION TO STATE OBJECT");
activeExperiments = window.optimizelyEdge.get('state').getActiveExperiments();
redirectCookie = _getCookieValue("optimizelyRedirectData");
// Init redirect object
// optlyHelper.redirectInfo = {};
if (redirectCookie.length > 0) {
optlyHelper.helperLog("GOT REDIRECT COOKIE VALUE");
redirectVariables = _getQueryVariable(redirectCookie);
if (redirectVariables.hasOwnProperty('r')) {
_writeRedirectInfo(redirectVariables);
}
// Override Referrer of Page
if (redirectVariables.hasOwnProperty('r') && redirectVariables.r.length > 0) {
Object.defineProperty(document, "referrer", { get: function () { return redirectVariables.r; } });
}
activeExperiments[redirectVariables.x] = {
'id': redirectVariables.x,
'variation': {
'id': redirectVariables.v
}
};
optlyHelper.helperLog("Added redirect cookie information to active experiments");
}
optlyHelper.activeExperiments = activeExperiments;
}
return {
init: init
};
})();
// GA INTEGRATION MODULE
window.optlyHelper.gaIntegration = (function () {
var timeoutInterval = 100,
maxWait = 10000,
waited = 0,
allActiveExperiments;
function _sendGaEvent(decisionString) {
if (!!decisionString) {
var prefix = '{custom_tracker}' + '.'; // Assign to ‘’ if no custom tracker
optlyHelper.helperLog("Sending decision event to GA");
window.ga(prefix + 'send', 'event', 'Optimizely', 'Assigned to Experiment', decisionString, { nonInteraction: true });
}
}
// Needs to be customized for specific websites. Defines how we identify if GA is available
function waitForGaEventApi(method) {
if (window.ga && window.ga.getByName) {
optlyHelper.helperLog("Found GA on the site");
method();
} else {
if (waited < maxWait) {
setTimeout(function () {
waited = waited + timeoutInterval;
waitForGaEventApi(method);
}, timeoutInterval);
} else {
optlyHelper.helperLog("failed_to_find_ga");
}
}
}
function _getDecisionString(experimentInfo) {
return "(" + experimentInfo.id + ":" + experimentInfo.variation.id + ")";
}
function init() {
optlyHelper.helperLog("GA INTEGRATION START");
waitForGaEventApi(function () {
allActiveExperiments = optlyHelper.activeExperiments;
for (var exp in allActiveExperiments) {
if (allActiveExperiments.hasOwnProperty(exp)) {
var experimentInfo = allActiveExperiments[exp];
// Manage sending GA event
var decisionString = _getDecisionString(experimentInfo);
optlyHelper.helperLog("Got decision string");
_sendGaEvent(decisionString);
}
}
});
}
return {
init: init,
waitForGaEventApi: waitForGaEventApi
};
})();
window.optlyHelper.getExperimentInfo.init();
window.optlyHelper.gaIntegration.init();