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:
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.
Identify
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.
fun identify(userId: String, traits: JsonObject = emptyJsonObject)
// If <T> is annotated with @Serializable you will not need to provide a serializationStrategy
fun <T> identify(userId: String, traits: T, serializationStrategy: KSerializer<T>)
analytics.identify("user-123", buildJsonObject {
put("username", "MisterWhiskers")
put("email", "hello@test.com")
put("plan", "premium")
});
analytics.identify("user-123", Builders.buildJsonObject(o -> {
o.put("username", "MisterWhiskers")
.put("email", "hello@test.com")
.put("plan", "premium");
}));
// or
analytics.identify("user-123", new YourJsonSerializable());
Track
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.
fun track(name: String, properties: JsonObject = emptyJsonObject)
// If <T> is annotated with @Serializable you will not need to provide a serializationStrategy
fun <T> track(name: String, properties: T, serializationStrategy: KSerializer<T>)
analytics.track("View Product", buildJsonObject {
put("productId", 123)
put("productName", "Striped trousers")
});
analytics.track("View Product", Builders.buildJsonObject(o -> {
o.put("productId", 123)
.put("productName", "Striped Trousers")
});
Screen
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.
fun screen(screenTitle: String, properties: JsonObject = emptyJsonObject, category: String = "")
// If <T> is annotated with @Serializable you will not need to provide a serializationStrategy
fun <T> screen(screenTitle: String, properties: T, category: String = "", serializationStrategy: KSerializer<T>)
analytics.screen("ScreenName", buildJsonObject {
put("productSlug", "example-product-123")
});
analytics.screen("ScreenName", Builders.buildJsonObject(o -> {
o.put("productSlug", "example-product-123");
}));
// or
analytics.screen("ScreenName", new YourJsonSerializable());
Add the AndroidRecordScreenPlugin to enable automatic screen tracking.
Group
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.
fun group(groupId: String, traits: JsonObject = emptyJsonObject)
// If <T> is annotated with @Serializable you will not need to provide a serializationStrategy
fun <T> group(groupId: String, traits: T, serializationStrategy: KSerializer<T>)
analytics.group("user-123", buildJsonObject {
put("username", "MisterWhiskers")
put("email", "hello@test.com")
put("plan", "premium")
});
analytics.group("user-123", Builders.buildJsonObject(o -> {
o.put("username", "MisterWhiskers")
.put("email", "hello@test.com")
.put("plan", "premium");
}));
// or
analytics.group("user-123", new YourJsonSerializable());
Utility methods
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.
Add
The Add method lets you add a plugin to the analytics timeline.
fun add(plugin: Plugin): Analytics
val plugin = object: Plugin {
override val type = Plugin.Type.Enrichment
override val name = "SomePlugin"
override var lateinit analytics: Analytics
}
analytics.add(plugin)
Find
The Find method lets you find a registered plugin from the analytics timeline.
fun find(pluginName: String): Plugin
val plugin = analytics.find(SomePlugin::class)
Remove
The Remove methods lets you remove a registered plugin from the analytics timeline.
fun remove(pluginName: String): Analytics
analytics.remove("SomePlugin")
Flush
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()
analytics.flush()
Reset
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()
analytics.reset()
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.
OpenURL
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:
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
// Add a deep-link opened event manually.
// This is necessary when your Activity has a android:launchMode of
// 'singleInstance', 'singleInstancePerTask', 'singleTop', or any other mode
// that will re-use an existing Activity instead of creating a new instance.
// The Analytics SDK automatically identifies when you app is started from
// a deep link if the Activity is created, but not if it is re-used. Therefore
// we have to add this code to manually capture the Deep Link info.
val referrer = "unknown"
analytics.trackDeepLinkOpen(referrer, intent)
}
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.
Changelog
This page was last modified: 13 Aug 2024
Need support?
Questions? Problems? Need more info? Contact Segment Support for assistance!