2017-09-12 00:09:29 +00:00
|
|
|
import hash from 'string-hash';
|
|
|
|
|
2017-11-07 23:54:42 +00:00
|
|
|
const experiments = {
|
2017-12-05 22:06:40 +00:00
|
|
|
XnN0idVWSxO6A0kiNkxzGw: {
|
|
|
|
id: 'XnN0idVWSxO6A0kiNkxzGw',
|
2017-11-07 23:54:42 +00:00
|
|
|
run: function(variant, state, emitter) {
|
2017-12-05 22:06:40 +00:00
|
|
|
state.promo = variant === 1 ? 'blue' : 'grey';
|
2017-11-07 23:54:42 +00:00
|
|
|
emitter.emit('render');
|
|
|
|
},
|
|
|
|
eligible: function() {
|
|
|
|
return (
|
|
|
|
!/firefox/i.test(navigator.userAgent) &&
|
|
|
|
document.querySelector('html').lang === 'en-US'
|
|
|
|
);
|
|
|
|
},
|
|
|
|
variant: function(state) {
|
|
|
|
return this.luckyNumber(state) > 0.5 ? 1 : 0;
|
|
|
|
},
|
|
|
|
luckyNumber: function(state) {
|
|
|
|
return luckyNumber(
|
|
|
|
`${this.id}:${state.storage.get('testpilot_ga__cid')}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2017-09-12 00:09:29 +00:00
|
|
|
|
|
|
|
//Returns a number between 0 and 1
|
2017-10-10 17:20:49 +00:00
|
|
|
// eslint-disable-next-line no-unused-vars
|
2017-09-12 00:09:29 +00: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 19:01:55 +00:00
|
|
|
xp.run(+state.query.v, state, emitter);
|
2017-09-12 00:09:29 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!state.storage.get('testpilot_ga__cid')) {
|
|
|
|
// first ever visit. check again after cid is assigned.
|
|
|
|
emitter.on('DOMContentLoaded', () => {
|
|
|
|
checkExperiments(state, emitter);
|
|
|
|
});
|
|
|
|
} else {
|
2017-11-07 23:54:42 +00:00
|
|
|
const enrolled = state.storage.enrolled.filter(([id, variant]) => {
|
2017-09-12 00:09:29 +00:00
|
|
|
const xp = experiments[id];
|
|
|
|
if (xp) {
|
|
|
|
xp.run(variant, state, emitter);
|
|
|
|
}
|
2017-11-07 23:54:42 +00:00
|
|
|
return !!xp;
|
2017-09-12 00:09:29 +00:00
|
|
|
});
|
|
|
|
// single experiment per session for now
|
|
|
|
if (enrolled.length === 0) {
|
|
|
|
checkExperiments(state, emitter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|