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

Update segment implementation #22

Merged
merged 2 commits into from
Apr 17, 2023
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 addon/analytiks-segment/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ android {

dependencies {
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'com.segment.analytics.android:analytics:4.11.0'
implementation 'com.segment.analytics.kotlin:android:1.10.3'
implementation project(':analytiks-core')
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,42 +1,39 @@
package com.analytiks.segment

import android.content.Context
import com.analytiks.core.AnalyticsDataTransmitterExtension
import com.analytiks.core.CoreAddon
import com.analytiks.core.EventsExtension
import com.analytiks.core.UserProfileExtension
import com.analytiks.core.model.Param
import com.analytiks.core.model.UserProperty
import com.analytiks.segment.PropertiesHelper.formatParamsToProperties
import com.segment.analytics.Analytics
import com.segment.analytics.Traits
import java.util.concurrent.TimeUnit
import com.segment.analytics.kotlin.android.Analytics
import com.segment.analytics.kotlin.core.Analytics
import kotlinx.serialization.json.buildJsonObject
import kotlinx.serialization.json.put

class SegmentAnalyticsClient(
private val token: String,
private val recordScreen: Boolean = true,
private val collectDeviceId: Boolean = true,
private val flushIntervalInSeconds: Long? = null,
private val trackApplicationLifecycleEvents: Boolean = false,
private val useLifecycleObserver: Boolean = false,
private val flushIntervalInSeconds: Int? = null,
private val flushAt: Int? = null,
private val tag: String? = null
) : CoreAddon, EventsExtension, UserProfileExtension {
) : CoreAddon, EventsExtension, UserProfileExtension, AnalyticsDataTransmitterExtension {
lateinit var segmentAnalytics: Analytics

override fun initialize(context: Context) {
segmentAnalytics = Analytics.Builder(context, token).apply {
trackApplicationLifecycleEvents()
segmentAnalytics = Analytics(this.token, context) {
this.trackApplicationLifecycleEvents =
this@SegmentAnalyticsClient.trackApplicationLifecycleEvents

if (flushIntervalInSeconds != null) this.flushInterval(
flushIntervalInSeconds,
TimeUnit.SECONDS
)
this.useLifecycleObserver = this@SegmentAnalyticsClient.useLifecycleObserver
this.collectDeviceId = this@SegmentAnalyticsClient.collectDeviceId

if (recordScreen) this.recordScreenViews()
if (collectDeviceId) this.collectDeviceId(true)
tag?.let { this.tag(it) }
}.build().also {
Analytics.setSingletonInstance(it)
this@SegmentAnalyticsClient.flushIntervalInSeconds?.let { this.flushInterval = it }
this@SegmentAnalyticsClient.flushAt?.let { this.flushAt = it }
}

segmentAnalytics = Analytics.with(context)
}

override fun reset() {
Expand All @@ -48,21 +45,28 @@ class SegmentAnalyticsClient(
}

override fun logEvent(name: String, vararg properties: Param) {
val formattedParams = formatParamsToProperties(*properties)
segmentAnalytics.track(name, formattedParams)
segmentAnalytics.track(name, buildJsonObject {
properties.forEach {
put(it.propertyName, it.propertyValue)
}
})
}

override fun identify(userId: String) {
segmentAnalytics.identify(userId)
}

override fun setUserProperty(property: UserProperty) {
segmentAnalytics.identify(
Traits().apply {
putValue(property.propertyName, property.propertyValue)
}
)
if (segmentAnalytics.userId() == null) return

segmentAnalytics.identify(userId = segmentAnalytics.userId()!!, buildJsonObject {
put(property.propertyName, property.propertyValue.toString())
})
}

override fun setUserPropertyOnce(property: UserProperty) = Unit

override fun pushAll() {
segmentAnalytics.flush()
}
}
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ dependencies {
implementation project(':analytiks')
implementation project(path: ':analytiks-core')
implementation project(path: ':addon:analytiks-mixpanel')
implementation project(path: ':addon:analytiks-segment')
implementation project(path: ':addon:analytiks-timber')

testImplementation 'junit:junit:4.13.2'
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/java/com/logitanalyticsapp/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.analytiks.addon.mixpanel.MixpanelAnalyticsClient
import com.analytiks.addon.timber.TimberLocalClient
import com.analytiks.core.CoreAddon
import com.analytiks.core.model.Param
import com.analytiks.segment.SegmentAnalyticsClient
import com.logitanalyticsapp.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {
Expand All @@ -25,6 +26,11 @@ class MainActivity : AppCompatActivity() {
TimberLocalClient(),
MixpanelAnalyticsClient(
token = "YOUR_TOKEN"
),
SegmentAnalyticsClient(
token = "YOUR_TOKEN",
flushIntervalInSeconds = 5,
trackApplicationLifecycleEvents = true,
)
)

Expand Down