How the Kotlin Multiplatform SDK works
One Kotlin library for Android apps and desktop apps on the JVM, with iOS to follow. It sends the same events, people and sessions as your site's snippet, so an app and a site land in the same reports.
| Setting | Value |
|---|---|
| Package | com.gotmode:metrics-kmp:0.1.1, from https://gotmode.com/sdk/maven |
| Host | Your project's tracking hostname, such as https://m.example.com, from Setup |
| Key | Your project's public ingest key (mpk_), safe to ship inside an app |
| Platforms | Android 5.0 and later, desktop JVM 11 and later (Compose Desktop, Swing, JavaFX). iOS is not published yet |
| Delivery | A queue kept on the device, sent in batches of up to 50, retried with the same event ids so nothing is counted twice |
Install
Add Mode's Maven repository, then the dependency, to your shared module or to an Android or desktop module on its own.
// settings.gradle.kts dependencyResolutionManagement { repositories { google() mavenCentral() exclusiveContent { forRepository { maven("https://gotmode.com/sdk/maven") } filter { includeGroup("com.gotmode") } } } }
// build.gradle.kts kotlin { sourceSets { androidMain.dependencies { implementation("com.gotmode:metrics-kmp:0.1.1") } jvmMain.dependencies { implementation("com.gotmode:metrics-kmp:0.1.1") } } }
Two optional parts for Android are packages of their own, so an app that does not use them carries none of their code. Add each one you use next to the core.
// build.gradle.kts of the Android app dependencies { implementation("com.gotmode:metrics-kmp-navigation:0.1.1") // Jetpack Navigation screens implementation("com.gotmode:metrics-kmp-installreferrer:0.1.1") // the Play Install Referrer }
The repository has the Android and desktop JVM builds. The iOS build is not in it yet, so add the dependency to your Android and desktop source sets, and use the Apple SDK in the iOS app for now.
Mode serves its SDKs itself, not from a public package registry. A published version never changes, and every file and its SHA-256 is listed in SHA256SUMS.
| File | Version | SHA-256 |
|---|---|---|
metrics-kmp-0.1.1.jar | 0.1.1 | 12bd0703692ebf90d7d4fb10e433bab563c78e54d2509147e1390be4b551d93d |
metrics-kmp-jvm-0.1.1.jar | 0.1.1 | 66194add8f577f51eddec7969879750ff1ffcdd8d20d9e4070a254c8b6b37154 |
metrics-kmp-android-0.1.1.aar | 0.1.1 | 61dda33c9b1a92f330a411d0e8f249f20538ff5b77741a5a0a355d5a89dea272 |
metrics-kmp-navigation-0.1.1.aar | 0.1.1 | a6fb183865706c84450499816ff87ca6ea25342302d6f55e0215fa3cdecd877a |
metrics-kmp-installreferrer-0.1.1.aar | 0.1.1 | d7ef3ba42605158cbe2b5482a47fabe220f7b91e4c698b9fac9a335b4a5fe983 |
The SDK asks for no Android permission beyond INTERNET and brings no Firebase or Navigation library. The install referrer package brings Google's Play Install Referrer library, and the navigation package uses your app's own Navigation.
Android
Call setup once in your Application. Install, update, opens, backgrounds, screens, deep links, notification taps and permission, the Firebase token, taps on views and on Jetpack Compose, and crashes are captured from then on.
class App : Application() { override fun onCreate() { super.onCreate() ModeMetrics.setup(this, "YOUR_INGEST_KEY", ModeOptions().apply { apiHost = "https://m.example.com" }) } }
A link that reaches an activity already running arrives in onNewIntent. Pass it on with ModeMetrics.handleIntent(intent). For Jetpack Navigation, in Compose or with fragments, ModeNavigation.track(navController) from metrics-kmp-navigation sends a $screen for every destination.
Desktop
Call setup with your app's id at start. Data is kept in the user's preferences under that id.
ModeMetrics.setup("com.example.notes", "YOUR_INGEST_KEY", ModeOptions().apply { apiHost = "https://m.example.com" appVersion = "1.4.0" }) ModeMetrics.trackWindow(window, "main") // optional, names a window's id
$app_opened is sent at start and $app_quit when the app exits, waiting at most 2 seconds for the send. In Compose Desktop and Swing every window's events and every click on a control are captured with no more code. JavaFX and other toolkits report windows with windowOpened, windowClosed, windowFocused, and windowBlurred. The platform is read from the system as macos, windows, or linux.
iOS
From Swift, through the framework your shared module builds.
Events, people and groups
The same calls on every platform.
ModeMetrics.capture("Order Completed", mapOf("total" to 42.5)) ModeMetrics.screen("Cart") ModeMetrics.identify("user_42", set = mapOf("plan" to "pro")) // after sign in ModeMetrics.group("company", "acme") ModeMetrics.register(mapOf("theme" to "dark")) // on every event ModeMetrics.reset() // on sign out
Also alias, setPersonProperties, unsetPersonProperties, registerOnce, registerForSession and unregister. A different user signing in on the same device becomes a new person, never a merge. Person properties set before sign in are sent with the next identify. An empty id or a placeholder such as null or anonymous is dropped.
Consent and opt out
Start with consent pending, and nothing is written to the device until the person answers.
ModeOptions().apply { consent = ConsentMode.PENDING }
ModeMetrics.setConsent(Grant.GRANTED)
ModeMetrics.setConsent(mapOf("ad_storage" to Grant.DENIED))
ModeMetrics.optOut()| State | What happens |
|---|---|
| Granted (default) | Events are kept on the device until sent |
| Pending | Up to 200 events wait in memory, nothing is stored |
| Denied | Events are dropped and stored ids and the queue are deleted |
| Ad storage denied | Ad click ids and the Mode click id are left out |
| Opted out | Nothing is sent until optIn, and the choice outlives a restart |
Deep links and attribution
A link that opens your app is a landing, like a visit to your site, so app installs and opens are credited to the campaign or Mode link that brought them.
Before anything is kept, sign in tokens, codes, passwords, keys and signatures are removed from the link and an email address becomes [email]. Its utm_ parameters, ad click ids, the Mode click id and sub ids become the first and last touch of the person. On Android, the Play Install Referrer is read on first start when your app includes metrics-kmp-installreferrer, and sent with $app_installed.
Push notifications
Tokens, notification opens, and notification permission are captured automatically when the platform provides them. The SDK never depends on a push library.
On iOS the APNs token, notifications received and tapped, and the permission come from the app delegate and the notification center's delegate. On Android a tap on a Firebase notification is read from the launch. On each return to the app, the SDK checks notification permission and, while analytics is allowed, reads the available Firebase token when your app ships Firebase Messaging with automatic token creation on. A message that arrives while an Android app runs reaches only your messaging service, so pass it on there. Forward refreshed Firebase tokens from onNewToken for immediate updates.
override fun onMessageReceived(m: RemoteMessage) = ModeMetrics.pushReceived(m.data) override fun onNewToken(token: String) = ModeMetrics.registerPushToken(token, "fcm")
Each notification gives only its message id, campaign, category, thread, and channel, never its title or body. A token is sent once until it changes, and the token itself only when you set sendPushToken.
Errors and logs
Crashes are captured on every platform and saved before the app stops, then sent on the next start.
ModeMetrics.captureException(error, mapOf("step" to "pay")) ModeMetrics.log("warn", "payment retried")
Each error carries its type, message, stack, whether it was handled or fatal, the release and the last 20 events before it, and appears in Errors. At most 50 are sent a run. Logs go to the logs store, scrubbed of emails and long numbers. Set console = true to also send what an Android or desktop app prints.
Feature flags
Flags load at start and whenever the person changes, and are cached for the next start.
ModeOptions().apply { flags = true }
if (ModeMetrics.isFeatureEnabled("new-checkout")) showNewCheckout()What is captured automatically
Every family can be turned off with disabledAutocapture, or all of them with autocapture = false, and from your project's settings.
| Event | Android | Desktop | iOS, once published |
|---|---|---|---|
$app_installed, $app_updated | Yes | Yes | Yes |
$app_opened, $app_backgrounded | Yes | Opened at start | Yes |
$app_quit | Yes | ||
$screen | Activities and Navigation | With screen | View controllers, SwiftUI screens, and Compose screens |
$deep_link_opened | Yes | Yes, universal links too | |
$click, $rage_click | Taps on views and Compose | Clicks in Swing and Compose Desktop | Taps on controls, cells, and SwiftUI buttons |
$push_registered, $push_opened, $push_received | Firebase token and opens | Yes | |
$permission_change for notifications | Yes | Yes | |
$window_opened, $window_closed, $window_focus, $window_blur | Every Swing and Compose window | ||
$exception | Yes | Yes | Yes |
The first event of each session carries the device (OS, model, type, pixel ratio, dark mode, text size, reduced motion, build, and languages), never anything typed or stored. The text of a tapped control is scrubbed of emails and long numbers and cut to 80 characters, left out entirely with maskText, and a text or password field never sends its text. A Compose control is described from its semantics, its role, its text or content description, and its test tag.