Skip to content

Repository files navigation

PostHog KMP Logo

Kotlin Ktor Platforms Maven Central License

PostHog KMP

Kotlin Multiplatform SDK for PostHog — type-safe analytics, feature flags, and event capture for every platform Kotlin runs on.

Features

  • Type-safe Result monadPostHogResult<T> with map, flatMap, recover — no exceptions leak to callers
  • Optional Kotlin Result bridge — use toKotlinResult() / toPostHogResult() when you prefer stdlib Result
  • Value class IDsDistinctId, ApiKey, FeatureFlagKey, GroupType, GroupKey prevent mixups at compile time
  • Properties DSL — Build event properties with a clean Kotlin DSL instead of raw JSON
  • Event capture with automatic batching — Queue events in memory, flush at threshold or on interval
  • User identification — Associate distinct IDs with user properties via identify and alias
  • Group analytics — Associate users with companies, projects, or any custom group type
  • Feature flags with caching — Fetch flags from /decide, cache in memory, observe via StateFlow
  • Multivariate flags — Boolean, string, and typed JSON payloads with getTypedPayload<T>()
  • Super properties — Register properties that are merged into every subsequent event
  • Opt in/out — Disable all capture with a single call, GDPR-friendly
  • 15 platform targets — Android, iOS, macOS, tvOS, watchOS, JVM, Linux, Windows, and WasmJs
  • Simple setup — Factory-based client creation

Setup

Add the dependencies you need to your build.gradle.kts:

[versions]
posthog-kmp = "0.1.2"

[libraries]
posthog-core = { module = "io.github.androidpoet:posthog-core", version.ref = "posthog-kmp" }
posthog-client = { module = "io.github.androidpoet:posthog-client", version.ref = "posthog-kmp" }
posthog-feature-flags = { module = "io.github.androidpoet:posthog-feature-flags", version.ref = "posthog-kmp" }
kotlin {
    sourceSets {
        commonMain.dependencies {
            implementation(libs.posthog.client)        // includes posthog-core
            implementation(libs.posthog.feature.flags) // optional
        }
    }
}

Usage

Create a Client

val client = PostHog.create("phc_your_project_api_key") {
    host = "https://us.i.posthog.com"  // or "https://eu.i.posthog.com"
    flushAt = 20
    flushIntervalMs = 30_000L
    logging = true
    preloadFeatureFlags = true
}

val client = createPostHogClient("phc_your_project_api_key", config)
val flags = createFeatureFlagsClient(client)

Capture Events

client.capture("page_viewed")

client.capture("button_clicked") {
    "screen" to "home"
    "button_id" to "cta_main"
    "count" to 3
    "is_premium" to true
}

Identify Users

client.identify(
    distinctId = "user_123",
    userProperties = buildJsonObject {
        put("name", "Jane Doe")
        put("email", "jane@example.com")
        put("plan", "pro")
    },
)

Feature Flags

val flags = createFeatureFlagsClient(client)

flags.loadFlags(distinctId = "user_123")

if (flags.isFeatureEnabled("new-dashboard")) {
    showNewDashboard()
}

val variant = flags.getStringFlag("onboarding-flow", defaultValue = "control")

@Serializable
data class DiscountConfig(val percent: Int, val code: String)

val discount = flags.getTypedPayload<DiscountConfig>("summer-sale")

flags.flagsFlow.collect { allFlags ->
    val enabled = allFlags.filter { it.value.isEnabled }
    println("Enabled flags: ${enabled.keys}")
}

Group Analytics

client.group(
    groupType = "company",
    groupKey = "company_abc",
    groupProperties = buildJsonObject {
        put("name", "Acme Corp")
        put("plan", "enterprise")
        put("employee_count", 250)
    },
)

Batch Flush

client.flush()

client.reset()

client.close()

Architecture

┌─────────────────────────────────────────────────────────┐
│                        Your App                          │
├──────────────┬──────────────────┬────────────────────────┤
│  posthog-    │    posthog-      │     posthog-           │
│  feature-    │    client        │     core               │
│  flags       │                  │                        │
│              │  PostHogClient   │  PostHogResult         │
│  /decide     │  HttpTransport   │  Properties DSL        │
│  Cache       │  Event Queue     │  Value IDs             │
│  StateFlow   │  Batch Flush     │  Models                │
│  Typed       │  Super Props     │  Serialization         │
│  Payloads    │  Factory API     │  Error Types           │
│              ├──────────────────┤                        │
│              │  Ktor Engines    │                        │
│              │  OkHttp · Darwin │                        │
│              │  CIO · Js        │                        │
└──────────────┴──────────────────┴────────────────────────┘

Modules

Module Artifact Description
posthog-core io.github.androidpoet:posthog-core Result monad, error types, value class IDs, event/flag models
posthog-client io.github.androidpoet:posthog-client HTTP transport, event capture, batching, identify, groups
posthog-feature-flags io.github.androidpoet:posthog-feature-flags Feature flag evaluation, caching, StateFlow observation, typed payloads

Targets

Platform Target Ktor Engine
Android androidTarget() OkHttp
JVM jvm() OkHttp
iOS iosX64() iosArm64() iosSimulatorArm64() Darwin
macOS macosX64() macosArm64() Darwin
tvOS tvosX64() tvosArm64() tvosSimulatorArm64() Darwin
watchOS watchosX64() watchosArm64() watchosSimulatorArm64() Darwin
Linux linuxX64() CIO
Windows mingwX64() CIO
Web wasmJs() Js

Why posthog-kmp?

posthog-kmp posthog-android (official)
Platforms 15 KMP targets Android only
Error handling PostHogResult<T> monad Thrown exceptions
Type safety Value class IDs String IDs
Feature flags Cached + StateFlow Cached
Properties Kotlin DSL Map builders
Dependencies 3 core Platform-specific
Codebase ~2K LOC ~15K LOC

Tech Stack

Layer Library
Language Kotlin 2.1.10
Networking Ktor 3.1.1
Serialization kotlinx.serialization 1.8.0
Coroutines kotlinx.coroutines 1.10.1
Date/Time kotlinx-datetime 0.6.2
Publishing vanniktech maven-publish 0.30.0

Build

# Compile all targets
./gradlew compileKotlinJvm

# Run tests
./gradlew jvmTest

# Full build (all platforms)
./gradlew build --no-configuration-cache

# Publish to Maven Central (CI only)
./gradlew publishAllPublicationsToMavenCentral --no-configuration-cache

Contributing

Contributions are welcome! If you've found a bug, have an idea for an improvement, or want to contribute new features, please open an issue or submit a pull request.

Find this repository useful? ❤️

Support it by joining stargazers for this repository. ⭐
Also, follow me on GitHub for my next creations! 🤩

License

MIT License

Copyright (c) 2026 Ranbir Singh

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

About

Kotlin Multiplatform SDK for PostHog — type-safe analytics, feature flags, session replay

Topics

Resources

Stars

9 stars

Watchers

0 watching

Forks

Releases

Sponsor this project

Packages

Contributors

Languages