How to Ship Features with PostHog Flags and A/B Tests
You ship with PostHog by creating a feature flag, evaluating it in your app with posthog.getFeatureFlag(), and tying an experiment to it. The flag returns a variant string or boolean per user, decided server-side, so the same user sees the same version across sessions.
The mechanic takes an afternoon. Getting a reliable experiment result takes planning, and PostHog’s own UI will happily run an underpowered test until you decide it is done.
How do you create and evaluate a flag?
- Feature flags → New flag, name it
new-checkout-flow. - Choose a flag type: boolean for on/off, multivariate for A/B/C.
- Set rollout: 50/50 percentage split, or target only internal users first.
- Add release conditions — country, plan, email domain — before 100% rollout.
- Evaluate in code:
const variant = posthog.getFeatureFlag('new-checkout-flow');
For server-side rendering, getFeatureFlag returns undefined on the first call because flags load asynchronously. Handle that case explicitly or your SSR renders the wrong version for a split second, which is a hydration mismatch in React.
Why is server-side evaluation worth the effort?
Because a client-side rollout can be bypassed. Anyone can read posthog.flags in devtools and see the assignment logic, and a determined user can flip it.
More practically: flags evaluated client-side are inconsistent across a user’s two devices until flags sync. For a pricing test, that means the same customer sees two prices — a support ticket and, in some jurisdictions, a legal problem.
The /decide endpoint evaluates flags server-side using the user’s distinct ID. Cache the result for the session and re-fetch on posthog.onFeatureFlags().
How do you run the experiment properly?
Create an experiment on the flag, then define exactly one primary metric. Two primary metrics means you will find a winner in one and a loser in the other and pick the one you like.
PostHog computes sample size for you if you give it the baseline conversion rate and a minimum detectable effect. Use that number. A test with a 12% baseline conversion and a 2 percentage point MDE needs around 3,800 users per variant — most B2B products take three to four weeks to accumulate that, and starting with 400 users will not give you an answer.
PostHog runs Bayesian analysis by default. That means no p-values; you get a probability the variant beats control. It stops the constant “can I peek yet” anxiety, but it does not immunise you against stopping early on a random spike.
What breaks experiments most often?
- Exposure skew. Your metric fires from a page the variant does not reach, so exposed users never appear in the denominator. Check that exposure and metric events come from the same surface.
- Changing the flag mid-test. Adjusting rollout from 50% to 30% halfway invalidates the data you have. Freeze the flag until the test ends.
- Session replay and experimentation colliding. Replays will show the variant UI out of context; tag replays with the variant so support can tell which one they are looking at.
- Ad blockers. 15-25% of traffic never loads the SDK. If exposure is client-side, your measured rate is computed on the subset that permits tracking, which is not random.
How do you clean up afterwards?
Every flag is technical debt with a name. Write the winning variant into the code, delete the flag, and remove the dead branch within one sprint.
Teams that keep 40 stale flags accumulate a combinatorial testing problem nobody can reason about, and PostHog’s flag payloads start slowing down /decide responses — noticeable above roughly 50 active flags per project.
Honest limits
PostHog experiments work well for a product team already using PostHog for analytics. If experimentation is your main need, LaunchDarkly or Statsig have deeper guardrail metrics (checking that a variant did not hurt revenue while you measure signups). PostHog added these but they are less configurable.
And for a B2B product with 200 accounts, honest answer: most experiments cannot reach significance. Use flags for safe rollout and skip the experiment.
Key Takeaways
- Create a flag, pick boolean or multivariate, then evaluate with
getFeatureFlag()— it returnsundefinedon the first SSR call. - Use server-side
/decideevaluation for pricing and anything that must be consistent across devices. - One primary metric per experiment; use PostHog’s sample size calculator before you start.
- Freeze flags during a test; changing rollout mid-flight invalidates results.
- Delete flags within a sprint of shipping — stale flags slow
/decideand rot the code.
