Kotlin Multiplatform Session Replay installation

  1. Install the dependency

    Required

    Add the PostHog KMP SDK to your shared module's commonMain source set:

    shared/build.gradle.kts
    kotlin {
    sourceSets {
    commonMain.dependencies {
    implementation("com.posthog:posthog-kmp:0.+")
    }
    }
    }

    0.+ resolves to the latest 0.x release. 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.

    Supported targets

    Session replay is captured on Android, iOS and the web. It is ignored on the JVM (desktop) target.

  2. Enable session recordings in project settings

    Required

    Go to your PostHog Project Settings and enable Record user sessions. Session recordings will not work without this setting enabled.

  3. Configure PostHog with session replay

    Required

    Pass a SessionRecordingConfig to PostHogConfig from shared code. Here are all the available options:

    Kotlin
    import com.posthog.kmp.PostHog
    import com.posthog.kmp.PostHogConfig
    import com.posthog.kmp.PostHogContext
    import com.posthog.kmp.SessionRecordingConfig
    val config = PostHogConfig(
    apiKey = "<ph_project_token>",
    host = "https://us.i.posthog.com",
    // Enable session recording. Requires enabling in your project settings as well.
    sessionRecording = SessionRecordingConfig(
    enabled = true,
    // Whether text input values are masked. Default is true.
    maskAllTextInputs = true,
    // Whether images are masked. Default is true.
    maskAllImages = true,
    // Whether network requests are included in the recording. Default is true.
    captureNetworkTelemetry = true,
    // Capture console and system logs (iOS and web). Default is false.
    captureLogs = false,
    // Whether replays are created using high quality screenshots instead of
    // wireframes (experimental, Android and iOS). Default is false.
    // Screenshots may contain sensitive information, so use with caution.
    screenshot = false,
    // Capture Android logcat output (Android only). Default is true.
    captureLogcat = true,
    // Touch event debounce delay in milliseconds (Android only). Default is 1000.
    debouncerDelayMs = 1000L,
    ),
    )

    Pass that config to PostHog.setup(). Android needs the Application instance, so it takes PostHogContext(application) – every other target takes the no-argument PostHogContext().

    MyApplication.kt
    import android.app.Application
    class MyApplication : Application() {
    override fun onCreate() {
    super.onCreate()
    PostHog.setup(config = config, context = PostHogContext(this))
    }
    }

    For more configuration options, see the Kotlin Multiplatform session replay docs.

  4. Watch session recordings

    Recommended

    Visit your site or app and interact with it for at least 10 seconds to generate a recording. Navigate between pages, click buttons, and fill out forms to capture meaningful interactions.

    Watch your first recording →

  5. Next steps

    Recommended

    Now that you're recording sessions, continue with the resources below to learn what else Session Replay enables within the PostHog platform.

    ResourceDescription
    Watching recordingsHow to find and watch session recordings
    Privacy controlsHow to mask sensitive data in recordings
    Network recordingHow to capture network requests in recordings
    Console log recordingHow to capture console logs in recordings
    More tutorialsOther real-world examples and use cases

Was this page useful?