Feature flags let you release features safely, but you only get real value if you're measuring what's happening. PostHog makes it easy to track which users have flags enabled, capture events tied to specific flag variants, and analyze the impact on your product metrics. Here's how to set it up.
Creating a Feature Flag and Tracking Evaluations
Start by creating a feature flag in PostHog, then instrument your code to capture when users interact with flagged features.
Create a feature flag in PostHog
Go to Feature flags in the left sidebar and click New feature flag. Give it a clear name (e.g., new_dashboard_ui) and set your rollout percentage or user targeting rules. PostHog will show you the flag key — you'll use this in your code.
// Feature flag created in PostHog UI with key: 'new_dashboard_ui'
// Set rollout: 50% of users
// PostHog generates the flag key automaticallyCheck if a flag is enabled in your code
Use posthog.getFeatureFlag() to check if the flag is enabled for the current user. This is synchronous after PostHog initializes, so it works in component renders and event handlers.
import posthog from 'posthog-js';
const isDashboardEnabled = posthog.getFeatureFlag('new_dashboard_ui');
if (isDashboardEnabled) {
renderNewDashboard();
} else {
renderLegacyDashboard();
}Capture an event when users interact with the flagged feature
Log an event when a user sees the feature or takes an action with it. Include the flag state in event properties — this ties user behavior to the flag and makes analysis simpler.
posthog.capture('dashboard_viewed', {
flag_enabled: isDashboardEnabled,
flag_variant: posthog.getFeatureFlag('new_dashboard_ui'),
dashboard_type: isDashboardEnabled ? 'new' : 'legacy'
});getFeatureFlag() won't block. Use onFeatureFlags() if you need to listen for real-time flag updates after initialization.Tracking Flag Impact on User Behavior
Once you're capturing events, use PostHog's Insights to compare metrics between flag variants and measure feature impact.
Add the flag variant to every relevant event
Include a consistent property (e.g., variant or flag_variant) on events you want to analyze by flag state. This lets you segment metrics in PostHog without doing manual cohort work later.
posthog.capture('purchase_completed', {
amount: 99.99,
variant: posthog.getFeatureFlag('new_checkout_flow'),
checkout_time: completionSeconds
});
posthog.capture('checkout_abandoned', {
variant: posthog.getFeatureFlag('new_checkout_flow'),
items_in_cart: cartSize
});Build an Insights chart to compare metrics by variant
In PostHog, go to Insights and create a new chart. Select your event, then use Breakdown by to choose the property you used to store the flag variant. PostHog will show side-by-side metrics for each flag state.
// In PostHog UI:
// 1. Insights > New insight
// 2. Select event: 'purchase_completed'
// 3. Breakdown by: variant
// 4. PostHog auto-calculates conversion rates and trends per variantCreate a cohort to isolate users who saw the feature
Go to Cohorts and click New cohort. Add a filter for users where variant = 'new_checkout_flow'. Save this cohort and use it in other analyses to isolate behavior of users exposed to your feature.
// Cohort created in PostHog UI:
// 1. Cohorts > New cohort
// 2. Add filter: variant equals new_checkout_flow
// 3. Save as "Saw New Checkout"
// 4. Use in Retention, Funnels, and other Insights$feature/flag_name for exposure, but custom properties give you richer context for analysis.Handling Multi-Variant Feature Flags
When your flag has multiple variants (A/B test variants or progressive rollouts), track each variant separately for clearer performance comparison.
Get the variant value, not just a boolean
Use posthog.getFeatureFlag() — it returns the variant string (e.g., control, treatment_a, treatment_b) or false. Store it in a variable so you can use it for branching and event tracking.
const checkoutVariant = posthog.getFeatureFlag('checkout_redesign');
// Returns: 'control', 'new_ui', 'ai_assistant', or false
if (checkoutVariant === 'new_ui') {
renderNewCheckout();
} else if (checkoutVariant === 'ai_assistant') {
renderAICheckout();
} else {
renderLegacyCheckout();
}Log the variant on events so you can compare all groups
Include the exact variant in event properties. PostHog will automatically break down metrics across all variants in a single Insights chart, making it easy to compare performance.
posthog.capture('checkout_started', {
variant: checkoutVariant,
cart_value: cartTotal,
product_count: items.length
});
posthog.capture('checkout_completed', {
variant: checkoutVariant,
amount: transactionAmount,
time_in_checkout: completionTime
});$feature/checkout_redesign on all events, but explicit variant properties are clearer in analysis and easier to filter on in Insights.Common Pitfalls
- Forgetting to include the flag variant in event properties — without it, you can't segment metrics by flag state in Insights
- Calling
getFeatureFlag()before PostHog initializes — it will return false untilposthog.init()completes - Not capturing events when users interact with the flagged feature — PostHog tracks flag exposure automatically, but you need explicit events to measure impact on behavior
- Using inconsistent property names for variants across events — pick one convention (e.g.,
variantorflag_variant) and use it everywhere
Wrapping Up
You now know how to create feature flags in PostHog, check them in your code, and track their impact on user behavior by including the variant in your events. By tying flag variants to your event data, you can confidently compare metrics between flag states and decide whether to roll out, iterate, or kill a feature based on real data. If you want to track feature flags and other product metrics automatically across tools, Product Analyst can help.