Analytics-Kotlin Implementation Guide
Once you've installed the mobile or server Analytics Kotlin library, you can start collecting data through Segment's tracking methods:
Info
For any of the different methods described, you can replace the properties and traits in the code samples with variables that represent the data collected.
The Identify method lets you tie a user to their actions and record traits about them. This includes a unique user ID and any optional traits you know about them like their email, name, or address. The traits option can include any information you want to tie to the user. When using any of the reserved traits, be sure the information reflects the name of the trait. For example, email should always be a string of the user's email address.
1fun identify(userId: String, traits: JsonObject = emptyJsonObject)23// If <T> is annotated with @Serializable you will not need to provide a serializationStrategy4fun <T> identify(userId: String, traits: T, serializationStrategy: KSerializer<T>)
The Track method lets you record the actions your users perform. Every action triggers an event, which also has associated properties that the track method records.
1fun track(name: String, properties: JsonObject = emptyJsonObject)23// If <T> is annotated with @Serializable you will not need to provide a serializationStrategy4fun <T> track(name: String, properties: T, serializationStrategy: KSerializer<T>)
The Screen method lets you record whenever a user sees a screen in your mobile app, along with optional extra information about the page being viewed.
You'll want to record a screen event whenever the user opens a screen in your app. This could be a view, fragment, dialog, or activity depending on your app.
Not all integrations support screen, so when it's not supported explicitly, the screen method tracks as an event with the same parameters.
1fun screen(screenTitle: String, properties: JsonObject = emptyJsonObject, category: String = "")23// If <T> is annotated with @Serializable you will not need to provide a serializationStrategy4fun <T> screen(screenTitle: String, properties: T, category: String = "", serializationStrategy: KSerializer<T>)
Info
Add the AndroidRecordScreenPlugin to enable automatic screen tracking.
The Group method lets you associate an individual user with a group— whether it's a company, organization, account, project, or team. This includes a unique group identifier and any additional group traits you may have, like company name, industry, number of employees. You can include any information you want to associate with the group in the traits option. When using any of the reserved group traits, be sure the information reflects the name of the trait. For example, email should always be a string of the user's email address.
1fun group(groupId: String, traits: JsonObject = emptyJsonObject)23// If <T> is annotated with @Serializable you will not need to provide a serializationStrategy4fun <T> group(groupId: String, traits: T, serializationStrategy: KSerializer<T>)
The Analytics Kotlin utility methods help you work with plugins from the analytics timeline. They include:
There's also the Flush method to help you manage the current queue of events.
The Add method lets you add a plugin to the analytics timeline.
fun add(plugin: Plugin): Analytics
The Find method lets you find a registered plugin from the analytics timeline.
fun find(pluginName: String): Plugin
The Remove methods lets you remove a registered plugin from the analytics timeline.
fun remove(pluginName: String): Analytics
The Flush method lets you force flush the current queue of events regardless of what the flushAt and flushInterval is set to.
public fun flush()
The reset method clears the SDK's internal stores for the current user and group. This is useful for apps where users log in and out with different identities on the same device over time.
fun reset()
Info
The reset method doesn't clear the userId from connected client-side integrations. If you want to clear the userId from connected client-side destination plugins, you'll need to call the equivalent reset method for that library.
While Analytics Kotlin will automatically track deep links that open your app when the trackDeepLinks Configuration property is set to true. There are some situations when the app is already open that could cause a deep link open event to be missed.
The openUrl function allows you to manually track that a deep link has opened your app while your app was already open:
1override fun onNewIntent(intent: Intent?) {2super.onNewIntent(intent)34// Add a deep-link opened event manually.5// This is necessary when your Activity has a android:launchMode of6// 'singleInstance', 'singleInstancePerTask', 'singleTop', or any other mode7// that will re-use an existing Activity instead of creating a new instance.8// The Analytics SDK automatically identifies when you app is started from9// a deep link if the Activity is created, but not if it is re-used. Therefore10// we have to add this code to manually capture the Deep Link info.1112val referrer = "unknown"13analytics.trackDeepLinkOpen(referrer, intent)14}
Info
Due to the way deep links are handled in Android, we can not know the referrer when a deep link causes onNewIntent() to be fired instead of onCreate().
For a sample implementation see our Kotlin Sample App.