A simple A/B testing setup with Simple Analytics
Last updated
import Cookies from 'js-cookie'
import { useEffect, useState } from 'react'
function Page() {
const [variant, setVariant] = useState(null)
useEffect(() => {
let cookieValue = Cookies.get('variant')
if (!cookieValue) {
cookieValue = Math.random() < 0.5 ? 'a' : 'b'
Cookies.set('variant', cookieValue, {
expires: 30,
})
}
setVariant(cookieValue)
}, [])
return (
<h1>
{variant === 'a' && <span>Current headline</span>}
{variant === 'b' && <span>New headline</span>}
{variant === null && <span> </span>}
</h1>
)
}function Hero() {
const handleBeforeDownload = () => {
const variant = Cookies.get('variant') || null
if variant {
window.sa_event && window.sa_event('click_download', { variant })
} else {
window.sa_event && window.sa_event('click_download')
}
}
return (
<Button onClick={handleBeforeDownload}>
<span className="mx-2">Download</span>
</Button>
)
}