2017-09-12 00:09:29 +00:00
|
|
|
import hash from 'string-hash';
|
2019-05-03 17:10:56 +00:00
|
|
|
import Account from './ui/account';
|
2017-09-12 00:09:29 +00:00
|
|
|
|
2019-05-03 17:10:56 +00:00
|
|
|
const experiments = {
|
|
|
|
signin_button_color: {
|
|
|
|
eligible: function() {
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
variant: function() {
|
2022-04-12 13:58:58 +00:00
|
|
|
return ['white-primary', 'primary', 'white-violet', 'violet'][
|
2019-05-03 17:10:56 +00:00
|
|
|
Math.floor(Math.random() * 4)
|
|
|
|
];
|
|
|
|
},
|
|
|
|
run: function(variant, state) {
|
|
|
|
const account = state.cache(Account, 'account');
|
|
|
|
account.buttonClass = variant;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
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
|
|
|
}
|
|
|
|
});
|
2019-05-03 17:10:56 +00:00
|
|
|
const enrolled = state.storage.enrolled;
|
|
|
|
// single experiment per session for now
|
|
|
|
const id = Object.keys(enrolled)[0];
|
|
|
|
if (Object.keys(experiments).includes(id)) {
|
|
|
|
experiments[id].run(enrolled[id], state, emitter);
|
2017-09-12 00:09:29 +00:00
|
|
|
} else {
|
2019-05-03 17:10:56 +00:00
|
|
|
checkExperiments(state, emitter);
|
2017-09-12 00:09:29 +00:00
|
|
|
}
|
|
|
|
}
|