Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add event origin #15

Merged
merged 1 commit into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
})
}
}
Loading