Events in KMP apps

Event Tracking is part of the Analytics SDK which provides insights on user interactions with your app. The Apptics KMP SDK offers seamless in-app event tracking capabilities for Kotlin Multiplatform applications, allowing you to track user behaviors, business events, and custom analytics across both Android and iOS platforms using a unified API.

Key features:

  • Unified API - Single interface for both iOS and Android platform
  • Custom properties - Attach custom data to events with HashMap support (Android)
  • Event grouping - Organize events by categories for better analytics
  • Platform native - Leverages native Apptics SDKs on each platform
  • Type safe - Kotlin-first design with type safety
  • Easy integration - Simple one-line event tracking

Prerequisites

Before you begin, make sure that Apptics is integrated with your project. If not, you can follow the steps mentioned in the guides below:

Installation

Add the analytics SDK to your app

Declare the KMP event dependency in your shared module's build.gradle.kts.

Copiedkotlin {
    sourceSets {
        commonMain.dependencies {
            implementation("com.zoho.apptics.kmp:analytics:0.1-beta")
        }
    }
}

For platform-specific dependencies

Copiedkotlin {
    sourceSets {
        androidMain.dependencies {
            implementation("com.zoho.apptics.kmp:analytics:0.1-beta")
        }
        iosMain.dependencies {
            implementation("com.zoho.apptics.kmp:analytics:0.1-beta")
        }
    }
}

Quickstart

Basic usage

Track events from your shared Kotlin code.

Copiedkotlin
import com.zoho.apptics.kmp.analytics.AppticsKMPEvent

// Simple event tracking
AppticsKMPEvent.addEvent(
    eventName = "button_clicked",
    groupName = "user_interaction"
)

// Event with custom properties (Android supported)
val properties = hashMapOf<String, Any>(
    "button_id" to "login_button",
    "screen" to "home_screen",
    "user_type" to "premium"
)

AppticsKMPEvent.addEvent(
    eventName = "login_attempt",
    groupName = "authentication",
    customProperty = properties
)

API reference

Core object - 'AppticsKMPEvent'

The main interface for event tracking in Kotlin multiplatform application.

CopiedaddEvent(eventName: String, groupName: String, customProperty: HashMap<String, Any>? = null)

Tracks a custom event with optional properties.

Parameters:

eventName: String - Unique identifier for the event (e.g., "purchase_completed", "video_watched")
groupName: String - Category or group for the event (e.g., "ecommerce", "media", "navigation")
customProperty: HashMap<String, Any>? - Optional custom data associated with the event

Example

Copiedkotlin
AppticsKMPEvent.addEvent("purchase_completed", "ecommerce")

Usage example

Copiedkotlin
// Product viewed
AppticsKMPEvent.addEvent(
    eventName = "product_viewed",
    groupName = "ecommerce",
    customProperty = hashMapOf(
        "product_id" to "12345",
        "product_name" to "Wireless Headphones",
        "category" to "Electronics",
        "price" to 99.99
    )
)
Copied// Add to cart
AppticsKMPEvent.addEvent(
    eventName = "add_to_cart",
    groupName = "ecommerce",
    customProperty = hashMapOf(
        "product_id" to "12345",
        "quantity" to 2,
        "total_value" to 199.98
    )
)
Copied// Purchase completed
AppticsKMPEvent.addEvent(
    eventName = "purchase_completed",
    groupName = "ecommerce",
    customProperty = hashMapOf(
        "order_id" to "ORD-2024-001",
        "total_amount" to 199.98,
        "payment_method" to "credit_card",
        "items_count" to 2
    )
)