From fb864a207ef65f125094c01a9ab0fb659407f143 Mon Sep 17 00:00:00 2001 From: Christopher Valles Date: Thu, 11 Jun 2015 17:57:22 +0100 Subject: [PATCH 1/3] Adding the option to store forced alternatives too --- sixpack.js | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/sixpack.js b/sixpack.js index 703fb2a..02d0636 100644 --- a/sixpack.js +++ b/sixpack.js @@ -1,5 +1,5 @@ (function () { - var sixpack = {base_url: "http://localhost:5000", ip_address: null, user_agent: null, timeout: 1000}; + var sixpack = {base_url: "http://localhost:5000", ip_address: null, user_agent: null}; // check for node module loader var on_node = false; @@ -18,7 +18,7 @@ }); }; - sixpack.Session = function (client_id, base_url, ip_address, user_agent, timeout) { + sixpack.Session = function (client_id, base_url, ip_address, user_agent) { 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; @@ -26,11 +26,10 @@ 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, force, callback) { + participate: function(experiment_name, alternatives, force, record_force, callback) { if (typeof force === "function") { callback = force; force = null; @@ -50,8 +49,11 @@ } } var params = {client_id: this.client_id, - experiment: experiment_name, - alternatives: alternatives}; + experiment: experiment_name, + alternatives: alternatives, + force: force, + record_force: record_force + }; if (!on_node && force == null) { var regex = new RegExp("[\\?&]sixpack-force-" + experiment_name + "=([^&#]*)"); var results = regex.exec(window.location.search); @@ -59,7 +61,7 @@ force = decodeURIComponent(results[1].replace(/\+/g, " ")); } } - if (force != null && _in_array(alternatives, force)) { + if (force != null && _in_array(alternatives, force) && !record_force) { return callback(null, {"status": "ok", "alternative": {"name": force}, "experiment": {"version": 0, "name": experiment_name}, "client_id": this.client_id}); } if (this.ip_address) { @@ -68,11 +70,11 @@ if (this.user_agent) { params.user_agent = this.user_agent; } - return _request(this.base_url + "/participate", params, this.timeout, function(err, res) { + return _request(this.base_url + "/participate", params, function(err, res) { if (err) { res = {status: "failed", - error: err, - alternative: {name: alternatives[0]}}; + error: err, + alternative: {name: alternatives[0]}}; } return callback(null, res); }); @@ -83,17 +85,17 @@ } var params = {client_id: this.client_id, - experiment: experiment_name}; + experiment: experiment_name}; 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 + "/convert", params, this.timeout, function(err, res) { + return _request(this.base_url + "/convert", params, function(err, res) { if (err) { res = {status: "failed", - error: err}; + error: err}; } return callback(null, res); }); @@ -102,12 +104,12 @@ var counter = 0; - var _request = function(uri, params, timeout, callback) { + var _request = function(uri, params, callback) { var timed_out = false; var timeout_handle = setTimeout(function () { timed_out = true; return callback(new Error("request timed out")); - }, timeout); + }, 1000); if (!on_node) { var cb = "callback" + (++counter); From 2f3b38742c0f318d58a7eada3e9f5222bae94d82 Mon Sep 17 00:00:00 2001 From: Christopher Valles Date: Thu, 11 Jun 2015 18:04:24 +0100 Subject: [PATCH 2/3] Adding the option to store forced alternatives too --- .gitignore | 1 + sixpack.js | 12 ++++++------ 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 400ecf0..5e7261f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ node_modules *~ *#* +.idea \ No newline at end of file diff --git a/sixpack.js b/sixpack.js index 02d0636..374846a 100644 --- a/sixpack.js +++ b/sixpack.js @@ -1,5 +1,5 @@ (function () { - var sixpack = {base_url: "http://localhost:5000", ip_address: null, user_agent: null}; + var sixpack = {base_url: "http://localhost:5000", ip_address: null, user_agent: null, timeout: 1000}; // check for node module loader var on_node = false; @@ -18,7 +18,7 @@ }); }; - sixpack.Session = function (client_id, base_url, ip_address, user_agent) { + 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; @@ -70,7 +70,7 @@ if (this.user_agent) { params.user_agent = this.user_agent; } - return _request(this.base_url + "/participate", params, function(err, res) { + return _request(this.base_url + "/participate", params, this.timeout, function(err, res) { if (err) { res = {status: "failed", error: err, @@ -92,7 +92,7 @@ if (this.user_agent) { params.user_agent = this.user_agent; } - return _request(this.base_url + "/convert", params, function(err, res) { + return _request(this.base_url + "/convert", params, this.timeout, function(err, res) { if (err) { res = {status: "failed", error: err}; @@ -104,12 +104,12 @@ var counter = 0; - var _request = function(uri, params, callback) { + 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")); - }, 1000); + }, timeout); if (!on_node) { var cb = "callback" + (++counter); From 4bb19dc6e8a7b9d150edbfa19043b8063a02cc28 Mon Sep 17 00:00:00 2001 From: Christopher Valles Date: Thu, 11 Jun 2015 18:05:24 +0100 Subject: [PATCH 3/3] Adding the option to store forced alternatives too --- sixpack.js | 1 + 1 file changed, 1 insertion(+) diff --git a/sixpack.js b/sixpack.js index 374846a..ce94f96 100644 --- a/sixpack.js +++ b/sixpack.js @@ -26,6 +26,7 @@ if (!on_node) { this.user_agent = this.user_agent || (window && window.navigator && window.navigator.userAgent); } + this.timeout = timeout || sixpack.timeout; }; sixpack.Session.prototype = {