Skip to content

Commit

Permalink
add event origin (#15)
Browse files Browse the repository at this point in the history
Co-authored-by: Wenxi Zeng <[email protected]>
  • Loading branch information
wenxi-zeng and Wenxi Zeng authored May 30, 2024
1 parent 648be58 commit 9dc1f5d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
2 changes: 1 addition & 1 deletion analytics-kotlin-live/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ android {
dependencies {
// Segment
implementation 'com.segment.analytics.kotlin:substrata:1.0.0'
implementation 'com.segment.analytics.kotlin:android:1.16.0'
implementation 'com.segment.analytics.kotlin:android:1.16.3'

implementation 'org.jetbrains.kotlinx:kotlinx-serialization-json:1.5.1'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.1'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ package com.segment.analytics.liveplugins.kotlin
import android.content.Context
import com.segment.analytics.kotlin.android.Analytics
import com.segment.analytics.kotlin.core.Analytics
import com.segment.analytics.kotlin.core.BaseEvent
import com.segment.analytics.kotlin.core.platform.Plugin
import com.segment.analytics.kotlin.core.utilities.putInContext
import com.segment.analytics.substrata.kotlin.JSObject
import com.segment.analytics.substrata.kotlin.JSScope
import com.segment.analytics.substrata.kotlin.JsonElementConverter
import kotlinx.serialization.json.buildJsonObject
import kotlinx.serialization.json.put
import java.lang.ref.WeakReference

object LivePluginsHolder {
Expand Down Expand Up @@ -64,39 +68,57 @@ class JSAnalytics {
}

fun track(event: String) {
analytics.track(event)
analytics.track(event) {
it?.insertEventOrigin()
}
}

fun track(event: String, properties: JSObject) {
analytics.track(event, JsonElementConverter.read(properties))
analytics.track(event, JsonElementConverter.read(properties)) {
it?.insertEventOrigin()
}
}

fun identify(userId: String) {
analytics.identify(userId)
analytics.identify(userId) {
it?.insertEventOrigin()
}
}

fun identify(userId: String, traits: JSObject) {
analytics.identify(userId, JsonElementConverter.read(traits))
analytics.identify(userId, JsonElementConverter.read(traits)) {
it?.insertEventOrigin()
}
}

fun screen(title: String, category: String) {
analytics.screen(title, category)
analytics.screen(title, category) {
it?.insertEventOrigin()
}
}

fun screen(title: String, category: String, properties: JSObject) {
analytics.screen(title, JsonElementConverter.read(properties), category)
analytics.screen(title, JsonElementConverter.read(properties), category) {
it?.insertEventOrigin()
}
}

fun group(groupId: String) {
analytics.group(groupId)
analytics.group(groupId) {
it?.insertEventOrigin()
}
}

fun group(groupId: String, traits: JSObject) {
analytics.group(groupId, JsonElementConverter.read(traits))
analytics.group(groupId, JsonElementConverter.read(traits)) {
it?.insertEventOrigin()
}
}

fun alias(newId: String) {
analytics.alias(newId)
analytics.alias(newId) {
it?.insertEventOrigin()
}
}

fun flush() {
Expand Down Expand Up @@ -126,4 +148,10 @@ class JSAnalytics {
}
return result
}

private fun BaseEvent.insertEventOrigin() : BaseEvent {
return putInContext("__eventOrigin", buildJsonObject {
put("type", "js")
})
}
}

0 comments on commit 9dc1f5d

Please sign in to comment.