1
0
mirror of https://gitlab.com/timvisee/send.git synced 2024-11-14 15:12:48 +01:00
send/app/experiments.js

48 lines
1.2 KiB
JavaScript
Raw Normal View History

2017-09-12 02:09:29 +02:00
import hash from 'string-hash';
const experiments = {};
2017-09-12 02:09:29 +02:00
//Returns a number between 0 and 1
2017-10-10 19:20:49 +02:00
// eslint-disable-next-line no-unused-vars
2017-09-12 02:09:29 +02:00
function luckyNumber(str) {
return hash(str) / 0xffffffff;
}
function checkExperiments(state, emitter) {
const all = Object.keys(experiments);
const id = all.find(id => experiments[id].eligible(state));
if (id) {
const variant = experiments[id].variant(state);
state.storage.enroll(id, variant);
experiments[id].run(variant, state, emitter);
}
}
export default function initialize(state, emitter) {
emitter.on('DOMContentLoaded', () => {
const xp = experiments[state.query.x];
if (xp) {
2017-09-13 21:01:55 +02:00
xp.run(+state.query.v, state, emitter);
2017-09-12 02:09:29 +02:00
}
});
if (!state.storage.get('testpilot_ga__cid')) {
// first ever visit. check again after cid is assigned.
emitter.on('DOMContentLoaded', () => {
checkExperiments(state, emitter);
});
} else {
const enrolled = state.storage.enrolled.filter(([id, variant]) => {
2017-09-12 02:09:29 +02:00
const xp = experiments[id];
if (xp) {
xp.run(variant, state, emitter);
}
return !!xp;
2017-09-12 02:09:29 +02:00
});
// single experiment per session for now
if (enrolled.length === 0) {
checkExperiments(state, emitter);
}
}
}