Adding experiment code
Contents
Once you've created your experiment in PostHog, the next step is to add your code.
Fetch the feature flag
In your experiment, each user is randomly assigned to a variant (usually either 'control' or 'test'). To check which variant a user has been assigned to, fetch the experiment feature flag. You can then customize their experience based on the value in the feature flag:
You must use getFeatureFlag() (or its framework equivalent like useFeatureFlagVariantKey()) to check variants. Other methods like getAllFlags(), getFeatureFlags(), or getFeatureFlagPayload() alone do not record an exposure event. Users evaluated with those methods won't be included in your experiment results.
To run an experiment using our API (without any SDK), see our docs on how to run experiments without feature flags.