1
0
mirror of synced 2025-12-21 19:06:49 -05:00
Files
docs/javascripts/experiment.js
Sarah Schneider 297cf047f9 AB test!
2021-04-27 12:56:14 -04:00

42 lines
1.1 KiB
JavaScript

import murmur from 'imurmurhash'
import { getUserEventsId, sendEvent } from './events'
import toggleImages from './toggle-images'
// import h from './hyperscript'
const TREATMENT = 'TREATMENT'
const CONTROL = 'CONTROL'
export function bucket (test) {
const id = getUserEventsId()
const hash = murmur(test).hash(id).result()
return hash % 2 ? TREATMENT : CONTROL
}
export function sendSuccess (test) {
return sendEvent({
type: 'experiment',
experiment_name: test,
experiment_variation: bucket(test).toLowerCase(),
experiment_success: true
})
}
function applyTreatment (toggleButton) {
// Treatment-specific options.
const hideImagesByDefault = true
const focusButtonByDefault = true
toggleImages(hideImagesByDefault, focusButtonByDefault)
}
export default function () {
// *** Example test code ***
const testName = 'toggle-images'
const xbucket = bucket(testName)
const x = document.getElementById('js-toggle-images')
if (!x) return
x.addEventListener('click', () => { sendSuccess(testName) })
if (xbucket === TREATMENT) applyTreatment(x)
}