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.

2.0 KB
Raw Blame
import { useValues } from 'kea'

import { CodeSnippet, Language } from 'lib/components/CodeSnippet'
import { FEATURE_FLAGS } from 'lib/constants'
import { featureFlagLogic } from 'lib/logic/featureFlagLogic'
import { apiHostOrigin } from 'lib/utils/apiHost'
import { teamLogic } from 'scenes/teamLogic'

export function JSInstallSnippet(): JSX.Element {
    return (
        <CodeSnippet language={Language.Bash}>
            {['npm install posthog-js', '# OR', 'yarn add posthog-js', '# OR', 'pnpm add posthog-js'].join('\n')}
        </CodeSnippet>
    )
}

export function ReactInstallSnippet(): JSX.Element {
    return (
        <CodeSnippet language={Language.Bash}>
            {[
                'npm install posthog-js @posthog/react',
                '# OR',
                'yarn add posthog-js @posthog/react',
                '# OR',
                'pnpm add posthog-js @posthog/react',
            ].join('\n')}
        </CodeSnippet>
    )
}

export function JSSetupSnippet(): JSX.Element {
    const { currentTeam } = useValues(teamLogic)
    const { featureFlags } = useValues(featureFlagLogic)
    const isPersonProfilesDisabled = featureFlags[FEATURE_FLAGS.PERSONLESS_EVENTS_NOT_SUPPORTED]

    return (
        <CodeSnippet language={Language.JavaScript}>
            {[
                "import posthog from 'posthog-js'",
                '',
                `posthog.init('${currentTeam?.api_token}',`,
                `    {`,
                `        api_host: '${apiHostOrigin()}',`,
                isPersonProfilesDisabled
                    ? ``
                    : `        person_profiles: 'identified_only' // or 'always' to create profiles for anonymous users as well`,
                `    }`,
                `)`,
            ].join('\n')}
        </CodeSnippet>
    )
}

export function SDKInstallJSWebInstructions(): JSX.Element {
    return (
        <>
            <h3>Install</h3>
            <JSInstallSnippet />
            <h3>Initialize</h3>
            <JSSetupSnippet />
        </>
    )
}