How are you doing A/B experiments in Sanity?
Great question! Sanity has solid support for A/B testing through the @sanity/personalization-plugin. Here's what you need to know:
The Plugin
The personalization plugin lets you run A/B (or A/B/N) experiments at the field level in your content. Instead of duplicating entire documents or pages, you can create variants for specific fields like headlines, button text, descriptions, or any other content field you want to test.
How It Works
You configure which field types should support experiments in your sanity.config.ts:
import {fieldLevelExperiments} from '@sanity/personalization-plugin'
export default defineConfig({
plugins: [
fieldLevelExperiments({
fields: ['string', 'text'], // field types to experiment on
experiments: [
{
id: 'hero-headline',
label: 'Hero Headline Test',
variants: [
{ id: 'control', label: 'Control' },
{ id: 'variant-a', label: 'Variant A' },
],
},
],
})
],
})This creates new field types (like experimentString) that your editors can use in Studio to manage multiple content variants.
Integration with External Tools
The plugin works great with external experimentation platforms like LaunchDarkly, GrowthBook, and Amplitude. You can either hardcode your experiments in the config or use an async function to fetch them dynamically from your experimentation service.
A real-world example: Zego (UK insurance company) uses this setup with LaunchDarkly—their marketing team creates variants in Sanity while controlling traffic distribution through LaunchDarkly.
Querying Variants
You can filter variants either server-side using GROQ's coalesce function or client-side in JavaScript. The server-side approach looks like:
*[_type == "page"][0]{
"headline": coalesce(
headline.variants[experimentId == $experiment && variantId == $variant][0].value,
headline.default
)
}Learning Resources
Sanity has a full A/B Testing course on Sanity Learn that walks through setup, implementation with Next.js, and integration with external services.
The approach is pretty flexible—you control content variants in Sanity while your experimentation platform handles user bucketing and analytics. Companies like Eurostar have reported that CMS tasks that used to take 2-3 weeks now take 2-3 days with this setup!
Show original thread15 replies
Sanity – Build the way you think, not the way your CMS thinks
Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.