Setting up A/B Tests
This guide outlines how to run an application-level A/B test by running two distinct versions of your application simultaneously. Codesphere’s load balancer handles the traffic distribution natively, ensuring users are assigned to a version and stay there.
Environment Isolation
To perform a test, you must isolate your "Control" (current production) from your "Variant" (the experiment).
- Deploy the Control (v1):
- Deploy your current stable, production-ready code to a Landscape in Codesphere.
- Name this Landscape
production-control.
- Deploy the Variant (v2):
- Create a duplicate Landscape (or clone the first one).
- Name this Landscape
production-variant. - Push your experimental code (new features, architectural changes, etc.) to this specific Landscape.
Because these are separate containers, you can test backend changes (like a new API structure or database query).
Analytics Instrumentation (PostHog Example)
Since both Landscapes will eventually live on the same public URL, your analytics tool will not naturally know which version a user is seeing. You must explicitly tag the user session using "Super Properties" (or "User Traits").
In the Control Landscape Code
Locate your analytics initialization script (usually in the header or a config file) and add the following immediately after initialization:
// App v1 (Control)
posthog.register({
experiment_group: "Control"
});
In the Variant Landscape Code
In the experimental codebase, update the snippet to reflect the new version:
// App v2 (Variant)
posthog.register({
experiment_group: "Variant-A"
});
The posthog.register function attaches the experiment_group property to every subsequent event (pageview, click, purchase) sent by that user. You do not need to manually tag individual events across your codebase.
Traffic Routing
Now that you have two running apps and analytics set up, you need to route real traffic to them. Codesphere handles this natively without requiring external tools like Nginx or HAProxy.
Configure the Domain
- Navigate to the Domains tab in Codesphere.
- Select your public custom domain (e.g.,
app.example.com). - Attach both landscapes:
production-controlproduction-variant
Traffic Split
Codesphere automatically distributes incoming requests 50/50 between the two landscapes.
Session Stickiness
The load balancer sets a Sticky Session Cookie on the user's first visit.
If a user is routed to the Variant, they remain locked to that landscape for their entire session, ensuring they do not switch versions on reload.
Verification
Before announcing the update, verify that the split and tagging are working correctly.
1. Open an Incognito Window
Navigate to your public domain.
2. Inspect the Console
Open your browser’s developer tools (F12) and run:
posthog.get_property('experiment_group')
The command should return either "Control" or "Variant-A".
3. Check Live Events
Go to:
PostHog → Live Events
Ensure that the incoming events from your session include the experiment_group property.
4. Test Routing
- Close the Incognito window.
- Open a new one.
- Repeat multiple times.
Eventually, the load balancer should route you to the opposing version, confirming that both environments are accessible.
Analysis & Decision
Once traffic is flowing, analyze the data by segmentation rather than complex mathematics.
The Analysis
- Open your dashboard (e.g., PostHog Insights).
- Create a trend graph or funnel (e.g., "User Signups").
- Apply a Breakdown by the property:
experiment_group
- Compare the results.
You will see two distinct lines:
- Control
- Variant-A
The Rollout
Based on the results, execute one of the following scenarios:
Scenario A: The Variant Wins
(Higher conversion or better performance)
- Go to the Codesphere Domains tab.
- Remove
production-controlfrom the domain.
→ 100% of traffic now flows toproduction-variant. - Decommission the old Control landscape.
Scenario B: The Variant Fails
(Bugs or lower conversion)
- Go to the Codesphere Domains tab.
- Remove
production-variantfrom the domain.
→ 100% of traffic reverts to the stableproduction-control. - Decommission the Variant landscape and investigate the code offline.