Kotlin Multiplatform Experiments installation
- 1
Install the dependency
RequiredAdd the PostHog KMP SDK to your shared module's
commonMainsource set:shared/build.gradle.kts0.+resolves to the latest0.xrelease. The SDK is a pre-release, so the API can change between minor versions – pin an exact version from Maven Central if you would rather upgrade deliberately. Kotlin/Wasm support requires0.2.0or higher. - 2
Configure PostHog
RequiredCall
PostHog.setup()once, early in your app's lifecycle. The config is shared across every target – only thePostHogContextdiffers per platform.Android needs the
Applicationinstance, so set PostHog up from yourApplicationclass – not from an Activity, whoseonCreatere-runs on every recreation (for example, on rotation):MyApplication.ktRegister the class in your
AndroidManifest.xml:android/src/main/AndroidManifest.xmlOn iOS, use the no-argument
PostHogContext():MainViewController.ktOn the web (Kotlin/JS and Kotlin/Wasm), use the no-argument
PostHogContext():main.ktOn the JVM – for example, a Compose Multiplatform desktop app – use the no-argument
PostHogContext():main.kt - 3
Implement your experiment
RequiredExperiments run on top of our feature flags. You can define which version of your code runs based on the return value of the feature flag:
- 4
Run your experiment
RequiredOnce you've implemented the feature flag in your code, you'll enable it for a target audience by creating a new experiment in the PostHog dashboard.
- 5
Next steps
RecommendedNow that you're running experiments, continue with the resources below to learn what else Experiments enables within the PostHog platform.
Resource Description Creating an experiment How to create an experiment in PostHog Adding experiment code How to implement experiments for all platforms Statistical significance Understanding when results are meaningful Experiment insights How to analyze your experiment data More tutorials Other real-world examples and use cases