Prompt
I need a server-side A/B testing platform with real analytics integration
Latest observation
Here’s a practical blueprint for a server-side A/B testing platform with real analytics integration.
What you’re building
A system that:
- Assigns users to variants on the server
- Persists the assignment so users stay in the same variant
- Logs exposure events to analytics
- Tracks downstream conversions
- Supports experimentation rules like targeting, traffic splits, and feature flags
Core architecture
1) Experiment service
Responsible for:
- Defining experiments
- Assigning variants
- Storing allocations
- Exposing APIs to apps/services
Key functions:
getAssignment(userId, experimentKey)logExposure(userId, experimentKey, variant)recordConversion(userId, experimentKey, goalKey)
2) Persistence layer
Store:
- Experiments
- Variants and weights
- Targeting rules
- Assignment history
- Event logs
Common stores:
- PostgreSQL for config and assignments
- Redis for fast assignment lookup/cache
- Kafka / SQS / PubSub for analytics event streaming
3) Analytics integration
Send events to:
- Segment
- Amplitude
- Mixpanel
- GA4
- BigQuery / Snowflake / Redshift for warehouse analytics
Events you should emit:
experiment_assignedexperiment_exposedexperiment_converted
Recommended flow
Assignment flow
- Request comes in with
userId - Server checks if user already has an assignment
- If not, it deterministically assigns a variant using a hash:
hash(userId + experimentKey) % 100
- Stores assignment
- Returns variant to app
Exposure flow
- Variant is actually rendered or used
- Emit exposure event once per user per experiment
- Include:
- userId
- experimentKey
- variant
- timestamp
- device/session metadata
Conversion flow
- User completes target action
- Emit conversion event referencing the experiment and variant
- Later analyze conversion rates by variant
Deterministic bucketing
Use a hash-based assignment so users always get the same variant without needing a lookup every time.
Example logic:
bucket = hash(userId + experimentKey) % 10000
if bucket < 5000 -> control
if bucket < 10000 -> treatment
This supports:
- stable assignment
- easy traffic splits
- reproducibility
Server-side API design
GET /experiments/assignments
Returns active assignments for a user.
Response:
{
"userId": "123",
"assignments": [
{
"experimentKey": "homepage_cta_test",
"variant": "treatment"
}
]
}
POST /experiments/exposure
Logs exposure when the user actually sees the variant.
{
"userId": "123",
"experimentKey": "homepage_cta_test",
"variant": "treatment"
}
POST /experiments/conversion
Logs conversion events.
{
"userId": "123",
"experimentKey": "homepage_cta_test",
"goalKey": "signup_completed"
}
Analytics integration options
Option A: Direct integration
Your backend sends events directly to the analytics provider.
Pros:
- Simple
- Low latency
Cons:
- Tighter coupling
- More vendor-specific code
Option B: Event pipeline
Your backend writes to a queue/stream, then a worker sends data to analytics tools and warehouse.
Pros:
- Reliable
- Scalable
- Easier to fan out to multiple destinations
Cons:
- More infrastructure
Best practice: Use a stream/queue plus workers.
Data model example
experiments
idkeynamestatustraffic_splitstart_atend_at
experiment_variants
idexperiment_idkeyweightpayload
assignments
idexperiment_iduser_idvariant_keyassigned_at
exposure_events
idexperiment_iduser_idvariant_keysession_idtimestamp
conversion_events
idexperiment_iduser_idgoal_keyvaluetimestamp
Important product features
Targeting
Support rules like:
- country = US
- platform = iOS
- plan = paid
- new users only
Holdouts
Keep a percentage of users out of experiments for baseline comparisons.
Feature flags
Use the same system for:
- beta releases
- kill switches
- staged rollouts
Guardrails
Track:
- latency
- error rate
- churn
- revenue impact
Analytics and reporting
Build dashboards for:
- assignment counts
- exposure counts
- conversion rates
- lift vs control
- statistical significance
Useful metrics:
- sample ratio mismatch
- exposure-to-assignment ratio
- conversion by variant
- time-to-conversion
Suggested tech stack
Backend
- Node.js / TypeScript, Go, or Python
- PostgreSQL
- Redis
- Kafka or managed queue
- Docker/Kubernetes
Analytics
- Segment or direct warehouse ingestion
- BigQuery/Snowflake
- dbt for transformations
- Metabase/Looker/Tableau for dashboards
Statistical analysis
- Bayesian or frequentist testing engine
- Sequential testing support
- CUPED for variance reduction if needed
Security and compliance
- Avoid putting sensitive PII in analytics events
- Use hashed or internal user IDs
- Add consent controls for GDPR/CCPA
- Audit all experiment changes
MVP scope
Start with:
- Experiment config CRUD
- Deterministic assignment
- Exposure logging
- Conversion logging
- Analytics export to one destination
- Basic dashboard
Then add:
- targeting rules
- multi-armed experiments
- feature flags
- warehouse reporting
- statistical significance calculations
If you want, I can next provide one of these:
- A database schema
- An API specification
- A production-ready architecture diagram
- Sample implementation in Node.js/TypeScript
- A build-vs-buy comparison for existing tools