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.8 KB
Raw Blame
import { useValues } from 'kea'

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

export interface AndroidSetupProps {
    includeReplay?: boolean
}

function AndroidInstallSnippet(): JSX.Element {
    return (
        <CodeSnippet language={Language.Kotlin}>
            {`dependencies {
    implementation("com.posthog:posthog-android:3.+")
}`}
        </CodeSnippet>
    )
}

function AndroidSetupSnippet({ includeReplay }: AndroidSetupProps): JSX.Element {
    const { currentTeam } = useValues(teamLogic)

    return (
        <CodeSnippet language={Language.Kotlin}>
            {`class SampleApp : Application() {

    companion object {
        const val POSTHOG_PROJECT_TOKEN = "${currentTeam?.api_token}"
        const val POSTHOG_HOST = "${apiHostOrigin()}"
    }

    override fun onCreate() {
        super.onCreate()

        // Create a PostHog Config with the given project token and host
        val config = PostHogAndroidConfig(
            apiKey = POSTHOG_PROJECT_TOKEN,
            host = POSTHOG_HOST
        )
        ${
            includeReplay
                ? `
        // check https://posthog.com/docs/session-replay/installation?tab=Android
        // for more config and to learn about how we capture sessions on mobile
        // and what to expect
        config.sessionReplay = true
        // choose whether to mask images or text
        config.sessionReplayConfig.maskAllImages = false
        config.sessionReplayConfig.maskAllTextInputs = true
        // screenshot is disabled by default
        // The screenshot may contain sensitive information, use with caution
        config.sessionReplayConfig.screenshot = true`
                : ''
        }

        // Setup PostHog with the given Context and Config
        PostHogAndroid.setup(this, config)
    }
}`}
        </CodeSnippet>
    )
}

export function SDKInstallAndroidInstructions(props: AndroidSetupProps): JSX.Element {
    return (
        <>
            <h3>Install</h3>
            <AndroidInstallSnippet />
            <h3>Configure</h3>
            <AndroidSetupSnippet {...props} />
        </>
    )
}

export function SDKInstallAndroidTrackScreenInstructions(): JSX.Element {
    return (
        <>
            <p>
                With <code>captureScreenViews = true</code>, PostHog will try to record all screen changes
                automatically.
            </p>
            <p>
                If you want to manually send a new screen capture event, use the <code>screen</code> function.
            </p>
            <CodeSnippet language={Language.Kotlin}>{`import com.posthog.PostHog

PostHog.screen(
    screenTitle = "Dashboard",
    properties = mapOf(
        "background" to "blue",
        "hero" to "superhog"
    )
)`}</CodeSnippet>
        </>
    )
}