-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathOptimizelyAbCallback.js
74 lines (55 loc) · 1.98 KB
/
OptimizelyAbCallback.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
/*global window:false, document:false */
(function(window, document, undefined) {
"use strict";
/* OptimizelyAbCallback main */
var _OptimizelyAbCallback = function() {
this.experiments = {};
this.initialized = false;
};
_OptimizelyAbCallback.prototype.includeABScript = function(optimizelyId){
var g = document.createElement("script"),
s = document.getElementsByTagName("script")[0];
g.src = '//cdn.optimizely.com/js/' + optimizelyId + '.js';
g.async = true;
s.parentNode.insertBefore(g, s);
};
_OptimizelyAbCallback.prototype.init = function() {
if (this.initialized) {
return;
}
this.initialized = true;
window._optiab = window._optiab || [];
var len = window.optimizely.data.state.activeExperiments.length,
that = this, i, experimentID, variation;
for(i = 0; i < len; i++){
experimentID = window.optimizely.data.state.activeExperiments[i];
variation = window.optimizely.data.state.variationNamesMap[experimentID] || 'Original';
variation = variation.toLowerCase().replace(' ','-').replace('#','');
this.experiments[experimentID] = variation;
}
for(i = 0; i < window._optiab.length; i++){
this._callHome(window._optiab.shift());
}
window._optiab.push = function(args) {
that._callHome.apply(that, [args]);
};
};
_OptimizelyAbCallback.prototype.addExperimentClass = function(experimentID, variation) {
var result = " ab-" + experimentID + "-" + variation;
if (document.documentElement.className.indexOf(result) === -1) {
document.documentElement.className += " ab-" + experimentID + "-" + variation;
}
};
_OptimizelyAbCallback.prototype._callHome = function(param) {
var variation = 'original';
if (param.length < 2 || typeof param[1] != 'function') {
return;
}
if (this.experiments[param[0]] !== undefined) {
variation = this.experiments[param[0]];
}
this.addExperimentClass(param[0], variation);
param[1](variation);
};
window.OptimizelyAbCallback = new _OptimizelyAbCallback();
}(window, document));