Skip to content

Usage

This guide explains how to integrate and use the KAnalytics library for event tracking in your applications.

Basic Setup

1. Creating Trackers

A tracker sends events to a specific analytics service. Implement the KTracker interface to create a custom tracker:

1
2
3
4
5
6
7
8
9
class FirebaseTracker(private val context: Context) : KTracker {

  override fun send(event: KAnalyticsEvent) {
    FirebaseAnalytics.getInstance(context).logEvent(
      event.eventName,
      event.properties
    )
  }
}

Tip

The tracker class eg. FirebaseTracker is used as the name of the tracker so by convention it's better if you use the name of the analytics service provider in the tracker class name. This helps to know which tracker events have been sent to.

2. Creating Interceptors

Interceptors process events before they reach the trackers. They can modify, or log events. Implement the Interceptor interface to create a custom interceptor:

1
2
3
4
5
6
7
class LogInterceptor : Interceptor {

  override fun intercept(chain: Interceptor.Chain): KAnalyticsEvent {
    Logger.d { "Event: ${chain.event.eventName} is intercepted and being sent to Firebase: ${chain.kTrackerName}" }
    return chain.proceed(chain.event)
  }
}

3. Initializing KAnalytics

Configure KAnalytics with your trackers and interceptors:

1
2
3
4
5
val kAnalytics = KAnalytics.Builder()
  .addTracker(FirebaseTracker(App.applicationContext()))
  .addTracker(AmplitudeTracker())
  .addInterceptor(LogInterceptor())
  .build()

Sending Events

Send to All Trackers

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
val event = KAnalyticsEvent("user_login").apply {
  addParameters(
    mapOf(
      "screen_name" to "LoginScreen",
      "user_type" to "new_user"
    )
  )
}

kAnalytics.send(event)  // Sends to all trackers

Send to Specific Trackers

1
kAnalytics.send(event, FirebaseTracker::class, AmplitudeTracker::class)  // Sends only to Firebase and Amplitude

Debug Tools

KAnalyticsViewer Setup

If you're using KAnalyticsViewer to view the events to be sent and wants to configure its Interceptor:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
// Configure event collection
val collector = KAnalyticsCollector(
  showNotification = true,
  showShortcut = true,
  duration = RetentionPolicyManager.DayDuration(7)  // Keep events for 7 days
)

// Create and add viewer interceptor
val viewerInterceptor = KAnalyticsInterceptor(collector = collector)

val kAnalytics = KAnalytics.Builder()
  .addTracker(FirebaseTracker(App.applicationContext()))
  .addInterceptor(viewerInterceptor)
  .build()