engauge
Version:
Javascript A/B Testing Library for Engauge
116 lines (99 loc) • 3.56 kB
JavaScript
import utils from './utils';
import Storage from './storage';
const Adapters = {};
//# Adapter for using the gimel backend. See https://github.com/Alephbet/gimel
//# uses jQuery to send data if `$.ajax` is found. Falls back on plain js xhr
//# params:
//# - url: Gimel track URL to post events to
//# - namepsace: namespace for Gimel (allows setting different environments etc)
//# - storage (optional) - storage adapter for the queue
Adapters.GimelAdapter = class GimelAdapter {
static queue_name() {
return '_gimel_queue';
}
__guard__(value, transform) {
return (typeof value !== 'undefined' && value !== null) ? transform(value) : undefined;
}
constructor(namespace, activeList) {
utils.log('got list', activeList);
this._storage = new Adapters.LocalStorageAdapter(namespace);
this.url = process.env.NODE_ENV === 'production' ? `https://engaugeab.com/api/public/track/${namespace}` : `https://engaugeab.app/api/public/track/${namespace}`;
this.namespace = namespace;
this._queue = JSON.parse(this._storage.get(this.queue_name) || '[]');
this._flush();
}
_remove_uuid(uuid) {
return (err, res) => {
if (err) { return; }
utils.remove(this._queue, el => el.properties.uuid === uuid);
return this._storage.set(this.queue_name, JSON.stringify(this._queue));
};
}
_plain_js_get(url, data, callback) {
let xhr = new XMLHttpRequest();
let params = ((() => {
let result = [];
for (let k in data) {
let v = data[k];
result.push(`${encodeURIComponent(k)}=${encodeURIComponent(v)}`);
}
return result;
})());
params = params.join('&').replace(/%20/g, '+');
xhr.open('GET', `${url}?${params}`);
xhr.onload = function() {
if (xhr.status === 200) {
return callback();
}
};
return xhr.send();
}
_ajax_get(url, data, callback) {
return this._plain_js_get(url, data, callback);
/*if (this.__guard__(window.jQuery, x => x.ajax)) {
return this._jquery_get(url, data, callback);
} else {
}*/
}
_flush() {
let callback;
return this._queue.map((item) =>
(callback = this._remove_uuid(item.properties.uuid),
this._ajax_get(this.url, item.properties, callback),
null));
}
_track(experiment_name, variant, event) {
utils.log(`Persistent Queue Gimel track: ${this.namespace}, ${experiment_name}, ${variant}, ${event}, ${utils.uuid()}`);
if (this._queue.length > 100) { this._queue.shift(); }
this._queue.push({
properties: {
experiment: experiment_name,
uuid: utils.uuid(),
variant,
event,
namespace: this.namespace
}
});
this._storage.set(this.queue_name, JSON.stringify(this._queue));
return this._flush();
}
experiment_start(experiment_name, variant) {
return this._track(experiment_name, variant, 'participate');
}
goal_complete(experiment_name, variant, goal) {
return this._track(experiment_name, variant, goal);
}
}
Adapters.LocalStorageAdapter = class LocalStorageAdapter {
constructor(namespace) {
console.info(namespace);
this.namespace = namespace;
}
set(key, value) {
return new Storage(this.namespace).set(key, value);
}
get(key) {
return new Storage(this.namespace).get(key);
}
}
export default Adapters;