Kotlin Multiplatform web analytics 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
Track screen views
RecommendedDespite the name, the web analytics dashboard can be used to track screen views in mobile apps, too. Open your app and view some screens to generate some events.
Screen views are off by default. Set
captureScreenViews = trueto capture them automatically – screen views on Android and iOS, pageviews on the web:KotlinYou can also capture a screen view yourself from shared code:
Kotlin - 4
Next steps
RecommendedAfter installing PostHog and ensuring autocapture is enabled, head to your web analytics dashboard to see your data. And then check out our start here guide.
PostHog tip: Web analytics works with anonymous events. This means if you are primarily using PostHog for web analytics, it can be significantly cheaper for you.