Skip to main content

Basic Usage (Android)

This guide covers the most common operations you'll perform with the OpenCDP Android SDK.

Initialization​

Initialize the SDK once, typically in your Application class.

import android.app.Application
import com.opencdp.OpenCDP
import com.opencdp.OpenCDPConfig

class MyApp : Application() {
override fun onCreate() {
super.onCreate()

val config = OpenCDPConfig(
cdpApiKey = "YOUR_API_KEY",
debug = true, // Enable logging in development
autoTrackScreens = true, // Enable automatic Activity tracking
trackApplicationLifecycleEvents = true
)

OpenCDP.initialize(this, config)
}
}

Identifying Users​

Use identify to create or update a user profile.

fun onUserLogin(userId: String, email: String) {
val properties = mapOf(
"email" to email,
"name" to "John Doe",
"plan" to "premium"
)

// Identify the user with a unique ID and optional traits
OpenCDP.identify(userId, properties)
}

Tracking Events​

Use track to record user actions.

fun onPurchaseComplete(orderId: String, amount: Double) {
val properties = mapOf(
"order_id" to orderId,
"amount" to amount,
"currency" to "USD"
)

OpenCDP.track("purchase_completed", properties)
}

Tracking Screens​

If autoTrackScreens is enabled, Activities are tracked automatically. You can also track screens manually:

fun viewProductDetails(productId: String) {
OpenCDP.trackScreenView("Product Details", mapOf("product_id" to productId))
}

Clearing Identity​

When a user logs out, clear their identity to reset the SDK state.

fun logout() {
OpenCDP.clearIdentity()
}