Working tree is now the canonical repo. Off-chain play-money + trustless on-chain (FPMM AMM + CLOB via UMA settlement), AI market factory, creator/social/distribution, mobile shell + push, one-command demo + deploy-kit. Secrets excluded (see ENV-AND-KEYS.md). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
38 lines
1.1 KiB
TypeScript
38 lines
1.1 KiB
TypeScript
import { createRNG } from 'common/util/random'
|
|
import { track } from 'web/lib/service/analytics'
|
|
import { ensureDeviceToken } from 'web/lib/util/device-token'
|
|
import { useEffectCheckEquality } from './use-effect-check-equality'
|
|
import { usePersistentInMemoryState } from 'client-common/hooks/use-persistent-in-memory-state'
|
|
|
|
const AB_TEST_CACHE: Record<string, boolean> = {}
|
|
|
|
export const useABTest = <T extends string>(
|
|
testName: string,
|
|
variants: T[],
|
|
trackingProperties?: Record<string, unknown>
|
|
) => {
|
|
const [variant, setVariant] = usePersistentInMemoryState<T | undefined>(
|
|
undefined,
|
|
`ab-test-${testName}`
|
|
)
|
|
|
|
useEffectCheckEquality(() => {
|
|
const deviceId = ensureDeviceToken()
|
|
if (!deviceId) return
|
|
|
|
const rand = createRNG(testName + deviceId)
|
|
const keys = variants.sort()
|
|
const randomVariant = keys[Math.floor(rand() * keys.length)]
|
|
|
|
setVariant(randomVariant)
|
|
|
|
// Only track once per user session
|
|
if (!AB_TEST_CACHE[testName]) {
|
|
AB_TEST_CACHE[testName] = true
|
|
track(testName, { ...trackingProperties, variant: randomVariant })
|
|
}
|
|
}, [testName, trackingProperties, variants])
|
|
|
|
return variant
|
|
}
|