Kotlin SDK Quickstart Alpha

Install and use the RudderStack Kotlin SDK on your Android apps.

This guide will walk you through setting up the SDK and sending your first events to RudderStack.

Prerequisites

info
In the RudderStack dashboard, the Setup tab in the source page has an SDK installation snippet containing both the write key and the data plane URL. You can use it to integrate the Kotlin SDK into your project.

Step 1: Install Kotlin SDK

Github Badge

The steps covered in the below sections will help you integrate the RudderStack Kotlin SDK into your Android project:

Add dependencies

Configure Maven Central

warning
Make sure that your app has the necessary permissions to send network requests.

Step 2: Initialize Kotlin SDK

Before tracking any events, initialize the Kotlin SDK in your Application class, as shown:

import android.app.Application
import com.rudderstack.sdk.kotlin.android.Analytics
import com.rudderstack.sdk.kotlin.android.Configuration

class MyApplication : Application() {
    lateinit var analytics: Analytics

    override fun onCreate() {
        super.onCreate()
        analytics = Analytics(
            configuration = Configuration(
                writeKey = "WRITE_KEY",
                application = this,
                dataPlaneUrl = "DATA_PLANE_URL",
            )
        )
    }
}

Replace the WRITE_KEY and DATA_PLANE_URL parameters with the Kotlin source write key and the data plane URL obtained in Prerequisites.

Step 3: Identify users

You can use the identify event to identify a user and associate them with their actions. It also enables you to record any traits about them like their name, email, etc.

You can use the identify method as follows:

import kotlinx.serialization.json.buildJsonObject
import kotlinx.serialization.json.put

analytics.identify(
    userId = "1hKOmRA4el9Zt1WSfVJIVo4GRlm",
    traits = buildJsonObject {
        put("name", "Alex Keener")
        put("email", "alex@example.com")
    }
)

Step 4: Track user actions

Once the Kotlin SDK is initialized, you can send track user actions and send them as events.

A sample track event triggered once the order is completed successfully is shown:

import kotlinx.serialization.json.buildJsonObject
import kotlinx.serialization.json.put
analytics.track(
    name = "Order Completed",
    properties = buildJsonObject {
        put("revenue", 30)
        put("currency", "USD")
    }
)

In the above snippet, the track method logs an event called Order Completed along with two event properties revenue and currency that provide additional context to the event.



Questions? Contact us by email or on Slack