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_enabledinstead offunnel_view) - Use positive language for boolean feature flags (e.g.
is_funnel_view_enabledinstead ofis_funnel_view_disabledoris_not_funnel_view_enabled) - The naming convention for the feature flags in the frontend is prepending
ffto 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:
- Create the feature flag in Posthog
- Set a minimal percentage of users to rollout to
- When testing a feature flag in development, filter the users by your
emailor test users cohort - When code is ready to be rolled out, either set the percentage of users or filter by a cohort of users
- Enable the feature flag in Posthog
- Gradually increase the percentage of users to rollout to
- If you filtered by a cohort, remember to modify the filters to include more users until all users are included
- 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:
- Make sure the feature flag is completely rolled out
- For multi variant flags, check that only one variant is completely rolled out
- Delete the flag from the codebase
- Delete the property from the
FeatureFlagsclass in the backend - Delete all the references to this feature flag in the codebase
- Delete the feature flag from the frontend by searching for
useFeatureFlagEnabled(<feature_flag_name>); - Delete the flag from Posthog
- Disable the flag in Posthog and check that everything is working as expected
- Finally, delete the flag from Posthog