This guide shows you how to verify that the A/B testing infrastructure is working correctly.
Visit the interactive demo page:
http://localhost:3000/abtest-demo
What to test:
- ✅ Page loads without errors
- ✅ Shows either "Version A" or "Version B"
- ✅ Refresh multiple times - should show the same version every time
- ✅ Click "Log Button Click" and "Log Conversion" buttons
- ✅ See events appear in the log
Testing in multiple browsers/incognito:
- Open in Chrome - note which variation you get
- Refresh multiple times - should stay the same
- Open in Incognito/Private window - might get different variation (new anonymous user)
- Refresh incognito multiple times - should stay consistent
Run the automated test suite:
./test-abtest.shThis tests:
- ✅ Variation assignment
- ✅ Consistency (same user gets same variation)
- ✅ Event logging
- ✅ Multiple test assignments
- ✅ Analytics retrieval
- ✅ Error handling
curl -X POST http://localhost:3000/api/abtest/assign \
-H "Content-Type: application/json" \
-d '{"testName":"homepage_layout"}' \
-c cookies.txtExpected output:
{
"testName": "homepage_layout",
"variation": "A",
"userId": "anon_1234567890abcdef"
}# Run this multiple times - should get same variation
for i in {1..5}; do
curl -s -X POST http://localhost:3000/api/abtest/assign \
-H "Content-Type: application/json" \
-d '{"testName":"homepage_layout"}' \
-b cookies.txt | grep variation
doneExpected: All 5 responses show the same variation
curl -X POST http://localhost:3000/api/abtest/event \
-H "Content-Type: application/json" \
-d '{
"testName":"homepage_layout",
"eventType":"button_click",
"eventData":{"buttonId":"follow","clubId":"abc123"}
}' \
-b cookies.txtExpected output:
{"success": true}# All tests
curl http://localhost:3000/api/abtest/analytics
# Specific test
curl http://localhost:3000/api/abtest/analytics?testName=homepage_layoutExpected output:
{
"testName": "homepage_layout",
"assignments": [
{"variation": "A", "count": 5},
{"variation": "B", "count": 7}
],
"events": [
{"variation": "A", "eventType": "assignment", "count": 5},
{"variation": "A", "eventType": "button_click", "count": 2},
{"variation": "B", "eventType": "assignment", "count": 7},
{"variation": "B", "eventType": "button_click", "count": 5}
]
}If you have MongoDB access, verify the data is being stored:
// Check assignments
db.abtestassignments.find().pretty()
// Check events
db.abtestevents.find().pretty()
// Count assignments per variation
db.abtestassignments.aggregate([
{ $match: { testName: "homepage_layout" } },
{ $group: { _id: "$variation", count: { $sum: 1 } } }
])
// Count events by type and variation
db.abtestevents.aggregate([
{ $match: { testName: "homepage_layout" } },
{ $group: {
_id: { variation: "$variation", eventType: "$eventType" },
count: { $sum: 1 }
}}
])Create a test component to verify the React hook:
// pages/test-hook.tsx
"use client";
import { useABTest } from "@/lib/clientABTest";
export default function TestHook() {
const { variation, loading, logEvent } = useABTest("homepage_layout");
console.log("Loading:", loading);
console.log("Variation:", variation);
return (
<div>
<p>Variation: {variation}</p>
<button onClick={() => logEvent("test_click")}>
Log Test Event
</button>
</div>
);
}# Create 100 different users and get their assignments
for i in {1..100}; do
curl -s -X POST http://localhost:3000/api/abtest/assign \
-H "Content-Type: application/json" \
-d '{"testName":"homepage_layout"}' \
-c "cookie_$i.txt" &
done
wait
# Check distribution
curl http://localhost:3000/api/abtest/analytics?testName=homepage_layoutWhat to verify:
- All requests succeed
- Users are distributed roughly 50/50 between variations (may vary due to hashing)
- No errors in server logs
Cause: Cookies not persisting Solution:
- Make sure cookies are enabled
- Check cookie settings in
.envfiles - Verify
ab_user_idcookie is being set
Cause: User hasn't been assigned to the test yet
Solution: Call /api/abtest/assign first, then /api/abtest/event
Cause: MongoDB connection issue or no data logged yet Solution:
- Check MongoDB connection in logs
- Verify MONGODB_URI in environment variables
- Run assignment/event tests first to generate data
Cause: Hashing function not working properly
Solution: Check that different userId values produce different variations
✅ Assignment works correctly:
- Users get assigned a variation
- Same user always gets same variation
- Different users get distributed across variations
✅ Event logging works:
- Events are stored in database
- Events are associated with correct variation
- Event data is preserved
✅ Analytics works:
- Can retrieve assignment counts
- Can retrieve event counts
- Data is grouped correctly by variation
✅ Integration works:
- React hook loads variation
- React hook logs events
- No errors in console
Once all tests pass:
-
Remove the demo page (optional):
rm -rf src/app/abtest-demo
-
Implement your first real A/B test:
- Update
ab-tests/tests.jsonwith your test - Use
useABTest()in your component - Log conversion events
- Analyze results after 1-2 weeks
- Update
-
Monitor in production:
- Check analytics daily
- Look for statistical significance
- Make data-driven decisions
# Quick health check
curl http://localhost:3000/api/abtest/analytics | python3 -m json.tool
# Clean up test cookies
rm -f *.txt
# Run full test suite
./test-abtest.sh
# Monitor server logs
npm run dev | grep -i "abtest"Refer to Milestones.md for complete documentation of the A/B testing infrastructure.