Android experiments installation

  1. Install PostHog Android SDK

    Required

    The best way to install the PostHog Android library is with a build system like Gradle. This ensures you can easily upgrade to the latest versions.

    All you need to do is add the posthog-android module to your App's build.gradle or build.gradle.kts:

    app/build.gradle
    dependencies {
    implementation("com.posthog:posthog-android:3.+")
    }

    Configuration

    The best place to initialize the client is in your Application subclass.

    Kotlin
    import android.app.Application
    import com.posthog.android.PostHogAndroid
    import com.posthog.android.PostHogAndroidConfig
    class SampleApp : Application() {
    companion object {
    const val POSTHOG_API_KEY = "<ph_project_api_key>"
    // usually 'https://us.i.posthog.com' or 'https://eu.i.posthog.com'
    const val POSTHOG_HOST = "https://us.i.posthog.com"
    }
    override fun onCreate() {
    super.onCreate()
    val config = PostHogAndroidConfig(
    apiKey = POSTHOG_API_KEY,
    host = POSTHOG_HOST
    )
    PostHogAndroid.setup(this, config)
    }
    }

  2. Capture conversion event

    Required

    Once PostHog is initialized, you should be able to capture events. For your experiment to be meaningful, we need to capture an event that we want to measure, such as a conversion event.

    For this tutorial, we can track a simple button click on a CTA as a conversion event.

    Kotlin
    // other imports
    import com.posthog.PostHog
    @Composable
    fun SomeView() {
    Button(
    onClick = {
    PostHog.capture("cta clicked")
    }
    ) {
    Text("Call to action text")
    }
    }
  3. Validate PostHog events

    Checkpoint
    Confirm events are being sent to PostHog

    Before proceeding, let's make sure events are being captured and sent to PostHog. You should see cta clicked events appear in the Activity feed.

    Check for events in PostHog

  4. Create an experiment

    Required

    Go to the Experiments tab in the PostHog app and click on the New experiment button in the top right.

    Create experiment

    For this tutorial, let's create a new experiment using simplified test values:

    • Name: "Test experiment"
    • Description: "This is a test experiment"
    • Feature flag key: "test-experiment-ff-key"
    • Experiment type: "Feature flag"
    • Variants: "control" and "test"
    • Participant type: "Users"

    Then click Save as draft.

  5. Add primary metric and launch

    Required

    Scroll down to the Primary metrics section and click + Add primary metric.

    Choose Single-use and select Type > Mean.

    Then search for the event cta clicked under Metric and click Save.

    Add primary metric

    By default, experiments are exposed to 100% of users. You can customize release conditions to expose the experiment to a subset of users.

    For this tutorial, we'll ship the experiment to all users and click Launch in the top right.

  6. Call feature flag

    Required

    Use the PostHog SDK to call the experiment flag and update how your app renders based on the assigned variant.

    Kotlin
    // other imports
    import com.posthog.PostHog
    @Composable
    fun SomeView() {
    if (PostHog.getFeatureFlag("experiment-feature-flag-key") == "test") {
    Button(
    onClick = {
    PostHog.capture("variant_button_clicked")
    }
    ) {
    Text("Variant Button")
    }
    } else {
    Button(
    onClick = {
    PostHog.capture("control_button_clicked")
    }
    ) {
    Text("Control Button")
    }
    }
    }

    Now when a user triggers a cta clicked event, PostHog automatically assigns the user to a variant and records an experiment exposure.

    By default, users are split equally between variants. If you want to assign specific users to a specific variant, see more about distribution and release conditions.

  7. Validate feature flag calls

    Checkpoint

    Make sure exposures and feature flag calls are being sent to PostHog. You should see $feature_flag_called events appear in the Activity feed.

    Check for events in PostHog

  8. Evaluate experiment results

    Recommended

    As you capture more cta clicked events, more exposures will populate the primary metrics in your experiment.

    Evaluate experiment metrics

    With enough data, you can analyze the experiment and its variants by:

    • Conversion rates
    • Statistical significance
    • Credible intervals
    • Chance to win %
    • Minimum detectable effect
    • And more

Community questions

Was this page useful?

Questions about this page? or post a community question.