From 9dc1f5d5c85050c2acc433d1c183cd286b19f511 Mon Sep 17 00:00:00 2001 From: Wenxi Zeng Date: Thu, 30 May 2024 12:22:26 -0500 Subject: [PATCH] add event origin (#15) Co-authored-by: Wenxi Zeng --- analytics-kotlin-live/build.gradle | 2 +- .../liveplugins/kotlin/JSAnalytics.kt | 46 +++++++++++++++---- 2 files changed, 38 insertions(+), 10 deletions(-) diff --git a/analytics-kotlin-live/build.gradle b/analytics-kotlin-live/build.gradle index 5e37f80..9db47d8 100644 --- a/analytics-kotlin-live/build.gradle +++ b/analytics-kotlin-live/build.gradle @@ -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' diff --git a/analytics-kotlin-live/src/main/java/com/segment/analytics/liveplugins/kotlin/JSAnalytics.kt b/analytics-kotlin-live/src/main/java/com/segment/analytics/liveplugins/kotlin/JSAnalytics.kt index 9b40d1c..7cda024 100644 --- a/analytics-kotlin-live/src/main/java/com/segment/analytics/liveplugins/kotlin/JSAnalytics.kt +++ b/analytics-kotlin-live/src/main/java/com/segment/analytics/liveplugins/kotlin/JSAnalytics.kt @@ -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 { @@ -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() { @@ -126,4 +148,10 @@ class JSAnalytics { } return result } + + private fun BaseEvent.insertEventOrigin() : BaseEvent { + return putInContext("__eventOrigin", buildJsonObject { + put("type", "js") + }) + } } \ No newline at end of file