Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

1.3 KB
Raw Blame
import { useMDXComponents } from 'scenes/onboarding/shared/OnboardingDocsContentWrapper'

export const OnFeatureFlagsCallbackSnippet = (): JSX.Element => {
    const { CodeBlock, dedent, Markdown } = useMDXComponents()

    return (
        <>
            <Markdown>
                {dedent`
                    Every time a user loads a page, we send a request in the background to fetch the feature flags that apply to that user. We store those flags in your chosen persistence option (local storage by default).

                    This means that for most pages, the feature flags are available immediately — **except for the first time a user visits**.

                    To handle this, you can use the \`onFeatureFlags\` callback to wait for the feature flag request to finish:
                `}
            </Markdown>
            <CodeBlock
                language="javascript"
                code={dedent`
                    posthog.onFeatureFlags(function (flags, flagVariants, { errorsLoading }) {
                        // feature flags are guaranteed to be available at this point
                        if (posthog.isFeatureEnabled('flag-key')) {
                            // do something
                        }
                    })
                `}
            />
        </>
    )
}