Skip to content

Commit 22ed9f1

Browse files
Merge pull request #240 from PostHog/feat/kmp-support
feat: add Kotlin Multiplatform (KMP) integration skill
2 parents d25e35e + 68c198f commit 22ed9f1

3 files changed

Lines changed: 45 additions & 0 deletions

File tree

context/commandments.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,3 +278,12 @@ commandments:
278278
- Call `PostHogAndroid.setup()` only once in the Application class's `onCreate()` method, so it's initialized as early as possible and only once.
279279
- Initialize PostHog in the Application class's `onCreate()` method
280280
- Ensure every activity has a `android:label` to accurately track screen views.
281+
282+
kmp:
283+
- com.posthog:posthog-kmp is the Kotlin Multiplatform SDK package name; add it to the shared module's commonMain.dependencies in build.gradle.kts, not to androidMain, iosMain, or any platform-specific source set
284+
- All PostHog APIs live in the com.posthog.kmp package — import com.posthog.kmp.PostHog, com.posthog.kmp.PostHogConfig, and com.posthog.kmp.PostHogContext
285+
- Call PostHog.setup(config, context) exactly once, early in the app lifecycle, from shared code; do not also add posthog-android or posthog-js directly
286+
- PostHogContext is platform-specific — on Android pass PostHogContext(application); on iOS and web use the no-argument PostHogContext()
287+
- posthog-kmp is a thin wrapper that delegates to the native posthog-android, posthog-ios, and posthog-js SDKs, so session replay and autocapture availability follow the underlying native platform
288+
- Use PostHogConfig.HOST_US or PostHogConfig.HOST_EU for the host, and read the project token from generated build config or environment values rather than hardcoding it
289+
- The SDK is 0.x pre-release; check the latest posthog-kmp version on Maven Central (https://central.sonatype.com/artifact/com.posthog/posthog-kmp) before pinning instead of hardcoding a stale version

context/skills/integration/config.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,14 @@ variants:
235235
tags: [flutter, dart, mobile]
236236
docs_urls:
237237
- https://posthog.com/docs/libraries/flutter.md
238+
239+
- id: kmp
240+
template: description-kmp-docs-only.md
241+
display_name: Kotlin Multiplatform
242+
description: PostHog integration for Kotlin Multiplatform (KMP) shared code targeting Android, iOS, and web using the posthog-kmp SDK
243+
tags: [kmp, kotlin, multiplatform, mobile]
244+
docs_urls:
245+
- https://posthog.com/docs/libraries/kmp.md
238246

239247
- id: react-native
240248
example_paths: example-apps/react-native
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# PostHog integration for {display_name}
2+
3+
This skill helps you add PostHog analytics to {display_name} applications using the official PostHog Kotlin Multiplatform SDK documentation.
4+
5+
## Instructions
6+
7+
1. Detect the existing Kotlin Multiplatform app structure. Check `settings.gradle.kts`, the shared module's `build.gradle.kts` (look for the `kotlin("multiplatform")` plugin and a `commonMain` source set), and the per-platform entry points (Android `Application`/`Activity`, iOS `MainViewController`, web `main`).
8+
2. Read the reference files below before changing code. They are the source of truth for SDK installation, initialization, event capture, identification, feature flags, group analytics, session replay, and error tracking.
9+
3. Install the SDK by adding `implementation("com.posthog:posthog-kmp:<version>")` to the shared module's `commonMain` dependencies in `build.gradle.kts` — not to a platform-specific source set. Check the latest version on Maven Central (https://central.sonatype.com/artifact/com.posthog/posthog-kmp) rather than hardcoding a stale one.
10+
4. Initialize PostHog once, early in the app lifecycle, from shared code: `PostHog.setup(config = PostHogConfig(apiKey = ..., host = ...), context = PostHogContext())`. Use environment or generated build-config values for the project token and host. Never hardcode secrets.
11+
5. Build `PostHogContext` per platform: on Android pass `PostHogContext(application)`; on iOS and web use the no-argument `PostHogContext()`. All PostHog APIs live in the `com.posthog.kmp` package.
12+
6. Add `PostHog.identify(...)` at login/signup boundaries and `PostHog.reset()` on logout.
13+
7. Verify with the project's normal Gradle commands, such as `./gradlew build`, or the repository's existing checks.
14+
15+
## Reference files
16+
17+
{references}
18+
19+
## Key principles
20+
21+
- **Environment/configuration values**: Always use environment variables or generated build config for PostHog keys. Never hardcode them.
22+
- **Minimal changes**: Add PostHog code alongside existing integrations. Don't replace or restructure existing code.
23+
- **Match the docs**: Follow the Kotlin Multiplatform reference's initialization and capture patterns exactly.
24+
- **Analytics contract**: Treat event names, property names, and feature flag keys as part of an analytics contract. Reuse existing names and patterns found in the project. When introducing new ones, make them clear, descriptive, and consistent with existing conventions.
25+
26+
## Framework guidelines
27+
28+
{commandments}

0 commit comments

Comments
 (0)