How to set up a b testing workflow on Vercel
Set up A/B testing on Vercel by creating feature flags in your dashboard, implementing conditional rendering in your code, and configuring audience targeting. Vercel's Edge Config and Analytics provide the infrastructure to run experiments and measure results effectively.
Prerequisites
- Vercel account and project deployed
- Basic understanding of React/Next.js
- Git repository connected to Vercel
- Node.js and npm installed locally
Step-by-Step Instructions
Enable Edge Config for Feature Flags
ab-testing-config. Click Create to set up your feature flag storage that will control your A/B tests.Configure Environment Variables
EDGE_CONFIG with the connection string from your Edge Config database. Also add VERCEL_ENV to distinguish between preview and production environments. Click Save to apply the changes.Install Required Dependencies
npm install @vercel/edge-config @vercel/analyticsThe
@vercel/edge-config package allows you to read feature flags, while @vercel/analytics helps track conversion events for your experiments.Create A/B Testing Utility Functions
lib/ab-testing.js and add:import { get } from '@vercel/edge-config';
export async function getVariant(experimentId, userId) {
const experiment = await get(experimentId);
if (!experiment?.enabled) return 'control';
const hash = simpleHash(userId + experimentId);
return hash % 100 < experiment.percentage ? 'variant' : 'control';
}
function simpleHash(str) {
let hash = 0;
for (let i = 0; i < str.length; i++) {
hash = ((hash << 5) - hash + str.charCodeAt(i)) & 0xffffffff;
}
return Math.abs(hash);
}Implement A/B Test in Your Components
import { getVariant } from '../lib/ab-testing';
import { track } from '@vercel/analytics';
export default function HomePage({ variant, userId }) {
const handleConversion = () => {
track('conversion', { variant, experiment: 'homepage-cta' });
};
return (
<div>
{variant === 'variant' ? (
<button onClick={handleConversion}>Try Free Now</button>
) : (
<button onClick={handleConversion}>Get Started</button>
)}
</div>
);
}Set Up Edge Function for User Assignment
middleware.js in your project root:import { NextResponse } from 'next/server';
import { getVariant } from './lib/ab-testing';
export async function middleware(request) {
const userId = request.cookies.get('user-id')?.value || crypto.randomUUID();
const variant = await getVariant('homepage-cta', userId);
const response = NextResponse.next();
response.cookies.set('user-id', userId);
response.cookies.set('ab-variant', variant);
return response;
}Configure Your Experiment in Edge Config
homepage-cta and value as:{
"enabled": true,
"percentage": 50,
"description": "Test CTA button text"
}Click Save to activate your experiment with 50% traffic allocation.
Monitor Results and Analyze Data
Common Issues & Troubleshooting
Edge Config returns null values
Check that your EDGE_CONFIG environment variable is correctly set and that you've deployed your project after adding the variable.
Users seeing different variants on page refresh
Ensure you're using a consistent user identifier in cookies and that your middleware is properly setting the user-id cookie.
Analytics events not appearing
Verify that @vercel/analytics is properly imported and initialized. Check that you're calling track() with the correct event name and properties.
Experiment not running in production
Make sure your Edge Config is connected to the production environment and that the experiment is enabled with "enabled": true in your configuration.