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

feat: Segment SDK implementation and destinations #227

Merged
Merged
Show file tree
Hide file tree
Changes from 4 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
337 changes: 130 additions & 207 deletions app/src/main/java/org/openedx/app/AnalyticsManager.kt

Large diffs are not rendered by default.

6 changes: 2 additions & 4 deletions app/src/main/java/org/openedx/app/analytics/Analytics.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package org.openedx.app.analytics

import android.os.Bundle

interface Analytics {
fun logScreenEvent(screenName: String, bundle: Bundle)
fun logEvent(eventName: String, bundle: Bundle)
fun logScreenEvent(screenName: String, params: Map<String, Any?>)
fun logEvent(eventName: String, params: Map<String, Any?>)
fun logUserId(userId: Long)
}
23 changes: 12 additions & 11 deletions app/src/main/java/org/openedx/app/analytics/FirebaseAnalytics.kt
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
package org.openedx.app.analytics

import android.content.Context
import android.os.Bundle
import android.util.Log
import com.google.firebase.analytics.FirebaseAnalytics
import org.openedx.core.extension.toBundle
import org.openedx.core.utils.Logger

class FirebaseAnalytics(context: Context) : Analytics {

private var tracker: FirebaseAnalytics? = null
private val logger = Logger(this.javaClass.name)
k1rill marked this conversation as resolved.
Show resolved Hide resolved
private var tracker: FirebaseAnalytics

init {
tracker = FirebaseAnalytics.getInstance(context)
Log.d("Analytics", "Firebase Builder Initialised")
logger.d { "Firebase Analytics Builder Initialised" }
}

override fun logScreenEvent(screenName: String, bundle: Bundle) {
Log.d("Analytics", "Firebase log Screen Event: $screenName + $bundle")
override fun logScreenEvent(screenName: String, params: Map<String, Any?>) {
logger.d { "Firebase Analytics log Screen Event: $screenName + $params" }
}

override fun logEvent(eventName: String, bundle: Bundle) {
tracker?.logEvent(eventName, bundle)
Log.d("Analytics", "Firebase log Event $eventName: $bundle")
override fun logEvent(eventName: String, params: Map<String, Any?>) {
tracker.logEvent(eventName, params.toBundle())
logger.d { "Firebase Analytics log Event $eventName: $params" }
}

override fun logUserId(userId: Long) {
tracker?.setUserId(userId.toString())
Log.d("Analytics", "Firebase User Id log Event")
tracker.setUserId(userId.toString())
logger.d { "Firebase Analytics User Id log Event" }
}
}
52 changes: 52 additions & 0 deletions app/src/main/java/org/openedx/app/analytics/SegmentAnalytics.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.openedx.app.analytics

import android.content.Context
import com.segment.analytics.kotlin.destinations.braze.BrazeDestination
import com.segment.analytics.kotlin.destinations.firebase.FirebaseDestination
import org.openedx.app.BuildConfig
import org.openedx.core.config.Config
import org.openedx.core.utils.Logger
import com.segment.analytics.kotlin.android.Analytics as SegmentAnalyticsBuilder
import com.segment.analytics.kotlin.core.Analytics as SegmentTracker

class SegmentAnalytics(context: Context, config: Config) : Analytics {

private val logger = Logger(this.javaClass.name)
private var tracker: SegmentTracker

init {
// Create an analytics client with the given application context and Segment write key.
tracker = SegmentAnalyticsBuilder(config.getSegmentConfig().segmentWriteKey, context) {
// Automatically track Lifecycle events
trackApplicationLifecycleEvents = true
flushAt = 20
flushInterval = 30
}
if (config.getFirebaseConfig().isSegmentAnalyticsSource()) {
tracker.add(plugin = FirebaseDestination(context = context))
}

if (config.getFirebaseConfig()
.isSegmentAnalyticsSource() && config.getBrazeConfig().isEnabled
) {
tracker.add(plugin = BrazeDestination(context))
}
SegmentTracker.debugLogsEnabled = BuildConfig.DEBUG
logger.d { "Segment Analytics Builder Initialised" }
}

override fun logScreenEvent(screenName: String, params: Map<String, Any?>) {
logger.d { "Segment Analytics log Screen Event: $screenName + $params" }
tracker.screen(screenName, params)
}

override fun logEvent(eventName: String, params: Map<String, Any?>) {
logger.d { "Segment Analytics log Event $eventName: $params" }
tracker.track(eventName, params)
}

override fun logUserId(userId: Long) {
logger.d { "Segment Analytics User Id log Event: $userId" }
tracker.identify(userId.toString())
}
}
7 changes: 7 additions & 0 deletions core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,13 @@ dependencies {
//Play In-App Review
api "com.google.android.play:review-ktx:$in_app_review"

// Segment Library
api "com.segment.analytics.kotlin:android:1.14.2"
farhan-arshad-dev marked this conversation as resolved.
Show resolved Hide resolved
// Segment's Firebase integration
api 'com.segment.analytics.kotlin.destinations:firebase:1.5.2'
// Braze SDK Integration
api "com.braze:braze-segment-kotlin:1.4.2"
volodymyr-chekyrta marked this conversation as resolved.
Show resolved Hide resolved

testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
Expand Down
9 changes: 9 additions & 0 deletions core/src/main/java/org/openedx/core/config/AnalyticsSource.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.openedx.core.config

import com.google.gson.annotations.SerializedName

enum class AnalyticsSource {
@SerializedName("segment")
SEGMENT,
NONE,
}
11 changes: 11 additions & 0 deletions core/src/main/java/org/openedx/core/config/BrazeConfig.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.openedx.core.config

import com.google.gson.annotations.SerializedName

data class BrazeConfig(
@SerializedName("ENABLED")
val isEnabled: Boolean = false,

@SerializedName("PUSH_NOTIFICATIONS_ENABLED")
val isPushNotificationsEnabled: Boolean = false
)
10 changes: 10 additions & 0 deletions core/src/main/java/org/openedx/core/config/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ class Config(context: Context) {
return getObjectOrNewInstance(FIREBASE, FirebaseConfig::class.java)
}

fun getSegmentConfig(): SegmentConfig {
return getObjectOrNewInstance(SEGMENT_IO, SegmentConfig::class.java)
}

fun getBrazeConfig(): BrazeConfig {
return getObjectOrNewInstance(BRAZE, BrazeConfig::class.java)
}

fun getFacebookConfig(): FacebookConfig {
return getObjectOrNewInstance(FACEBOOK, FacebookConfig::class.java)
}
Expand Down Expand Up @@ -148,6 +156,8 @@ class Config(context: Context) {
private const val WHATS_NEW_ENABLED = "WHATS_NEW_ENABLED"
private const val SOCIAL_AUTH_ENABLED = "SOCIAL_AUTH_ENABLED"
private const val FIREBASE = "FIREBASE"
private const val SEGMENT_IO = "SEGMENT_IO"
private const val BRAZE = "BRAZE"
private const val FACEBOOK = "FACEBOOK"
private const val GOOGLE = "GOOGLE"
private const val MICROSOFT = "MICROSOFT"
Expand Down
9 changes: 8 additions & 1 deletion core/src/main/java/org/openedx/core/config/FirebaseConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ data class FirebaseConfig(
@SerializedName("ENABLED")
val enabled: Boolean = false,

@SerializedName("ANALYTICS_SOURCE")
val analyticsSource: AnalyticsSource = AnalyticsSource.NONE,

@SerializedName("PROJECT_ID")
val projectId: String = "",

Expand All @@ -17,4 +20,8 @@ data class FirebaseConfig(

@SerializedName("GCM_SENDER_ID")
val gcmSenderId: String = "",
)
) {
fun isSegmentAnalyticsSource(): Boolean {
return enabled && analyticsSource == AnalyticsSource.SEGMENT
}
}
11 changes: 11 additions & 0 deletions core/src/main/java/org/openedx/core/config/SegmentConfig.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.openedx.core.config

import com.google.gson.annotations.SerializedName

data class SegmentConfig(
@SerializedName("ENABLED")
val enabled: Boolean = false,

@SerializedName("SEGMENT_IO_WRITE_KEY")
val segmentWriteKey: String = "",
)
13 changes: 13 additions & 0 deletions core/src/main/java/org/openedx/core/extension/MapExt.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.openedx.core.extension

import android.os.Bundle

fun Map<String, Any?>.toBundle(): Bundle {
val bundle = Bundle()
for ((key, value) in this.entries) {
value?.let {
bundle.putString(key, it.toString())
}
}
return bundle
}
Loading