* Move use session to its own hook file * Trying useSWR * Try one hour html cache * blah blah lint * Update to include loading state * Update components/hooks/useSession.ts Co-authored-by: Peter Bengtsson <peterbe@github.com> Co-authored-by: Peter Bengtsson <peterbe@github.com>
34 lines
908 B
TypeScript
34 lines
908 B
TypeScript
import murmur from 'imurmurhash'
|
|
import { getUserEventsId, sendEvent, EventType } from './events'
|
|
|
|
let initialized = false
|
|
|
|
const TREATMENT = 'TREATMENT'
|
|
const CONTROL = 'CONTROL'
|
|
|
|
export function bucket(test: string) {
|
|
const id = getUserEventsId()
|
|
const hash = murmur(test).hash(id).result()
|
|
return hash % 2 ? TREATMENT : CONTROL
|
|
}
|
|
|
|
export function sendSuccess(test: string) {
|
|
return sendEvent({
|
|
type: EventType.experiment,
|
|
experiment_name: test,
|
|
experiment_variation: bucket(test).toLowerCase(),
|
|
experiment_success: true,
|
|
})
|
|
}
|
|
|
|
export default function experiment() {
|
|
if (initialized) return
|
|
initialized = true
|
|
// *** Example test code ***
|
|
// const testName = '$test-name$'
|
|
// const xbucket = bucket(testName)
|
|
// const x = document.querySelector(...)
|
|
// x.addEventListener('click', () => { sendSuccess(testName) })
|
|
// if (xbucket === TREATMENT) applyTreatment(x)
|
|
}
|