Skip to content

Overview

Posthog Feature Flags

We use Posthog Feature Flags to manage the feature flags. Everything is managed in Posthog's UI, including the creation, activation, rollout, and deletion of feature flags. We only need to update the codebase to use the new feature flag using posthog's sdk.

How to use feature flags

Feature flags are used to rollout large features gradually. We can define a cohort of users to rollout to and increase the percentage of users over time until all the users are included.

We can filter the users by any property stored in Posthog like email, tenant, name or environment. As of writing this, we have a Cohort named Non-Prod users that includes users that are in staging, development or in a demo/test tenant in prod.

Best practices

We try to follow Posthog's best practices for feature flags. Here are some other recommendations when naming and using the feature flags in the codebase:

Naming recommendations:

  • When creating a new feature flag in posthog, the name should be in snake_case (e.g. is_funnel_view_enabled)
  • Flag names should reflect their return type (e.g. is_funnel_view_enabled instead of funnel_view)
  • Use positive language for boolean feature flags (e.g. is_funnel_view_enabled instead of is_funnel_view_disabled or is_not_funnel_view_enabled)
  • The naming convention for the feature flags in the frontend is prepending ff to the feature flag name and converting it to camelCase (e.g. ffIsFunnelViewEnabled)
  • The naming convention for the feature flags is the same as in Posthog (e.g. is_funnel_view_enabled), see Backend section for more details

To rollout a feature flag, we need to:

  1. Create the feature flag in Posthog
  2. Set a minimal percentage of users to rollout to
  3. When testing a feature flag in development, filter the users by your email or test users cohort
  4. When code is ready to be rolled out, either set the percentage of users or filter by a cohort of users
  5. Enable the feature flag in Posthog
  6. Gradually increase the percentage of users to rollout to
  7. If you filtered by a cohort, remember to modify the filters to include more users until all users are included
  8. Clean up the feature flag when it's completely rolled out

Frontend

To use a feature flag to import the useFeatureFlagEnabled hook from posthog-js/react.

import { useFeatureFlagEnabled } from 'posthog-js/react';

function MyComponent() {
    const ffIsFunnelViewEnabled = useFeatureFlagEnabled('is_funnel_view_enabled');
    return <div>{ffIsFunnelViewEnabled ? 'Funnel view is enabled' : 'Non feature flag enabled view'}</div>;
}

Backend

To use a feature flag add the property to the FeatureFlags class.

Avoid using the get_feature_flag method directly, instead create a @property that returns the feature flag value.

# utils/feature_flags.py

class FeatureFlags:
    # existing code
    ...

    @property
    def is_funnel_view_enabled(self):
        return self.get_feature_flag('is_funnel_view_enabled')

Using the FeatureFlags class:

# some_other_file.py

feature_flags = FeatureFlags(str(user.id))

if feature_flags.is_funnel_view_enabled:
    # do something

Clean Up

Feature flags aren't meant to be permanent. So when a feature flag is completely rolled out, follow these steps to clean it up:

  1. Make sure the feature flag is completely rolled out
  2. For multi variant flags, check that only one variant is completely rolled out
  3. Delete the flag from the codebase
  4. Delete the property from the FeatureFlags class in the backend
  5. Delete all the references to this feature flag in the codebase
  6. Delete the feature flag from the frontend by searching for useFeatureFlagEnabled(<feature_flag_name>);
  7. Delete the flag from Posthog
  8. Disable the flag in Posthog and check that everything is working as expected
  9. Finally, delete the flag from Posthog