-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.js
129 lines (98 loc) · 3.73 KB
/
api.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
(function(w, d, _cxApi, q) {
if (w[_cxApi] && w[_cxApi].q) {
q = q.concat(w[_cxApi].q)
}
_cxApi = _cxApi === w["_cxApiObject"]
? w[_cxApi]
: w[w["_cxApiObject"] = _cxApi] = function(experimentId, methodName, methodArgs, config, callback, errback) {
if (experimentId === undefined) {
throw new Error("no experimentId provided");
}
if (typeof config === "function") {
config = config.apply(self, arguments)
}
config = (typeof config === "number") ? {
"timeout": config
} : config || {};
var _cxApiExperiment = _cxApi[experimentId]
? _cxApi[experimentId]
: _cxApi[experimentId] = (function(head, script, src, queue, done) {
var before = function() {
done = true;
script.onload = script.onreadystatechange = script.onerror = null;
head.removeChild(script);
};
var after = function() {
var scope;
var args;
var timeout;
while (queue.length) {
scope = queue.shift();
args = queue.shift();
timeout = queue.shift();
if (timeout !== true) {
if (timeout) {
w.clearTimeout(timeout);
}
_cxApiExperiment.apply(scope, args);
}
}
}
script.onload = script.onreadystatechange = function() {
var readyState = this.readyState;
if (!done && (!readyState
|| readyState === "loaded"
|| readyState === "complete")) {
before();
_cxApiExperiment = _cxApi[experimentId] = (function(cxApi) {
delete w.cxApi;
return function(_methodName, _methodArgs, _config, _callback, _errback) {
var result = _methodName === "cxApi" ? cxApi : cxApi[_methodName];
try {
if (typeof result === "function") {
result = result.apply(this, _methodArgs);
}
_callback(result);
} catch (e) {
_errback(e);
}
return _cxApiExperiment;
}
})(w.cxApi);
after();
}
};
script.onerror = function(e) {
if (!done) {
before();
_cxApiExperiment = _cxApi[experimentId] = function(_methodName, _methodArgs, _config, _callback, _errback) {
_errback(new Error("Unable to load [" + src + "]"), e);
};
after();
}
};
script.src = src;
head.appendChild(script);
return function(_methodName, _methodArgs, _config, _callback, _errback) {
var timeout = _config.timeout;
var index = queue.push(this, arguments, timeout) - 1;
if (timeout) {
queue[index] = w.setTimeout(function() {
queue[index] = true;
_errback(new Error("Timeout [" + timeout + "]"), timeout);
}, timeout - (new Date() - this.ts));
}
return _cxApiExperiment;
};
})(d.getElementsByTagName("head")[0], d.createElement("script"), "https://www.google-analytics.com/cx/api.js?experiment=" + experimentId, [], false);
_cxApiExperiment.call({
"ts": new Date()
}, methodName || "cxApi", methodArgs || [], config, callback || config.callback || function() {}, errback || config.errback || function() {});
return _cxApi;
};
while (q.length) {
_cxApi.apply({
"ts": q.shift()
}, q.shift());
}
})(window, document, "_cxApi", []);