forked from martin-didomenico-olx/sixpack-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsixpack.js
More file actions
211 lines (197 loc) · 7.65 KB
/
Copy pathsixpack.js
File metadata and controls
211 lines (197 loc) · 7.65 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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
(function () {
var sixpack = {base_url: "http://localhost:5000", ip_address: null, user_agent: null, timeout: 1000};
// check for node module loader
var on_node = false;
if (typeof module !== "undefined" && typeof require !== "undefined") {
on_node = true;
module.exports = sixpack;
} else {
window["sixpack"] = sixpack;
}
sixpack.generate_client_id = function () {
// from http://stackoverflow.com/questions/105034
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
return v.toString(16);
});
};
sixpack.Session = function (client_id, base_url, ip_address, user_agent, timeout) {
this.client_id = client_id || sixpack.generate_client_id();
this.base_url = base_url || sixpack.base_url;
this.ip_address = ip_address || sixpack.ip_address;
this.user_agent = user_agent || sixpack.user_agent;
if (!on_node) {
this.user_agent = this.user_agent || (window && window.navigator && window.navigator.userAgent);
}
this.timeout = timeout || sixpack.timeout;
};
sixpack.Session.prototype = {
participate: function(experiment_name, alternatives, traffic_fraction, force, callback) {
if (typeof traffic_fraction === "function") {
callback = traffic_fraction;
traffic_fraction = null;
force = null;
}
else if (typeof traffic_fraction === "string") {
callback = force;
force = traffic_fraction;
traffic_fraction = null;
}
if (typeof force === "function") {
callback = force;
force = null;
}
if (!(/^[a-z0-9][a-z0-9\-_ ]*$/).test(experiment_name)) {
return callback(new Error("Bad experiment_name"));
}
if (alternatives.length < 2) {
return callback(new Error("Must specify at least 2 alternatives"));
}
for (var i = 0; i < alternatives.length; i += 1) {
if (!(/^[a-z0-9][a-z0-9\-_ ]*$/).test(alternatives[i])) {
return callback(new Error("Bad alternative name: " + alternatives[i]));
}
}
var params = {client_id: this.client_id,
experiment: experiment_name,
alternatives: alternatives};
if (!on_node && force == null) {
var regex = new RegExp("[\\?&]sixpack-force-" + experiment_name + "=([^&#]*)");
var results = regex.exec(window.location.search);
if(results != null) {
force = decodeURIComponent(results[1].replace(/\+/g, " "));
}
}
if (traffic_fraction !== null && !isNaN(traffic_fraction)) {
params.traffic_fraction = traffic_fraction;
}
if (force != null && _in_array(alternatives, force)) {
return callback(null, {"status": "ok", "alternative": {"name": force}, "experiment": {"version": 0, "name": experiment_name}, "client_id": this.client_id});
}
if (this.ip_address) {
params.ip_address = this.ip_address;
}
if (this.user_agent) {
params.user_agent = this.user_agent;
}
return _request(this.base_url + "/participate", params, this.timeout, function(err, res) {
if (err) {
res = {status: "failed",
error: err,
alternative: {name: alternatives[0]}};
}
return callback(null, res);
});
},
convert: function(experiment_name, kpi, callback) {
if (typeof kpi === 'function') {
callback = kpi;
kpi = null;
}
if (!(/^[a-z0-9][a-z0-9\-_ ]*$/).test(experiment_name)) {
return callback(new Error("Bad experiment_name"));
}
var params = {client_id: this.client_id,
experiment: experiment_name};
if (this.ip_address) {
params.ip_address = this.ip_address;
}
if (this.user_agent) {
params.user_agent = this.user_agent;
}
if (kpi) {
params.kpi = kpi;
}
return _request(this.base_url + "/convert", params, this.timeout, function(err, res) {
if (err) {
res = {status: "failed",
error: err};
}
return callback(null, res);
});
}
};
var counter = 0;
var _request = function(uri, params, timeout, callback) {
var timed_out = false;
var timeout_handle = setTimeout(function () {
timed_out = true;
return callback(new Error("request timed out"));
}, timeout);
if (!on_node) {
var cb = "callback" + (++counter);
params.callback = "sixpack." + cb
sixpack[cb] = function (res) {
if (!timed_out) {
clearTimeout(timeout_handle);
return callback(null, res);
}
}
}
var url = _request_uri(uri, params);
if (!on_node) {
script = document.createElement('script');
script.type = 'text/javascript';
script.src = url;
script.async = true;
document.body.appendChild(script);
} else {
var http = require('http');
var req = http.get(url, function(res) {
var body = "";
res.on('data', function(chunk) {
return body += chunk;
});
return res.on('end', function() {
var data;
if (res.statusCode == 500) {
data = {status: "failed", response: body};
} else {
try {
data = JSON.parse(body);
} catch (e) {
data = {status: "failed", response: body};
}
}
if (!timed_out) {
clearTimeout(timeout_handle);
return callback(null, data);
}
});
});
req.on('error', function(err) {
if (!timed_out) {
clearTimeout(timeout_handle);
return callback(err);
}
});
}
};
var _request_uri = function(endpoint, params) {
var query_string = [];
var e = encodeURIComponent;
for (var key in params) {
if (params.hasOwnProperty(key)) {
var vals = params[key];
if (Object.prototype.toString.call(vals) !== '[object Array]') {
vals = [vals];
}
for (var i = 0; i < vals.length; i += 1) {
query_string.push(e(key) + '=' + e(vals[i]));
}
}
}
if (query_string.length) {
endpoint += '?' + query_string.join('&');
}
return endpoint;
};
var _in_array = function(a, v) {
for(var i = 0; i < a.length; i++) {
if(a[i] === v) {
return true;
}
}
return false;
};
})();