Skip to content

Commit

Permalink
feat: Analytics architecture for multiple libraries
Browse files Browse the repository at this point in the history
fix: LEARNER-9667
  • Loading branch information
omerhabib26 committed Nov 13, 2023
1 parent a6fd21c commit a06939f
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 11 deletions.
19 changes: 13 additions & 6 deletions app/src/main/java/org/openedx/app/AnalyticsManager.kt
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
package org.openedx.app

import android.content.Context
import android.os.Bundle
import androidx.core.os.bundleOf
import com.google.firebase.analytics.FirebaseAnalytics
import org.openedx.app.analytics.Analytics
import org.openedx.auth.presentation.AuthAnalytics
import org.openedx.course.presentation.CourseAnalytics
import org.openedx.dashboard.presentation.DashboardAnalytics
import org.openedx.discovery.presentation.DiscoveryAnalytics
import org.openedx.discussion.presentation.DiscussionAnalytics
import org.openedx.profile.presentation.ProfileAnalytics

class AnalyticsManager(context: Context) : DashboardAnalytics, AuthAnalytics, AppAnalytics,
class AnalyticsManager : DashboardAnalytics, AuthAnalytics, AppAnalytics,
DiscoveryAnalytics, ProfileAnalytics, CourseAnalytics, DiscussionAnalytics {

private val analytics = FirebaseAnalytics.getInstance(context)
private val services: ArrayList<Analytics> = arrayListOf()

fun addAnalyticsTracker(analytic: Analytics) {
services.add(analytic)
}

private fun logEvent(event: Event, params: Bundle = bundleOf()) {
analytics.logEvent(event.eventName, params)
services.forEach { analytics ->
analytics.logEvent(event.eventName, params)
}
}

private fun setUserId(userId: Long) {
analytics.setUserId(userId.toString())
services.forEach { analytics ->
analytics.logUserId(userId)
}
}

override fun dashboardCourseClickedEvent(courseId: String, courseName: String) {
Expand Down
16 changes: 12 additions & 4 deletions app/src/main/java/org/openedx/app/OpenEdXApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@ import android.app.Application
import com.google.firebase.FirebaseOptions
import com.google.firebase.ktx.Firebase
import com.google.firebase.ktx.initialize
import org.koin.android.ext.android.inject
import org.koin.android.ext.koin.androidContext
import org.koin.core.context.startKoin
import org.openedx.app.analytics.FirebaseAnalytics
import org.openedx.app.analytics.SegmentAnalytics
import org.openedx.app.di.appModule
import org.openedx.app.di.networkingModule
import org.openedx.app.di.screenModule
import org.koin.android.ext.koin.androidContext
import org.koin.core.context.startKoin

class OpenEdXApp : Application() {

private val analytics: AnalyticsManager by inject()

override fun onCreate() {
super.onCreate()
startKoin {
Expand All @@ -32,6 +37,9 @@ class OpenEdXApp : Application() {
.build()
Firebase.initialize(this, options)
}
//Add Firebase Analytics for tracking
analytics.addAnalyticsTracker(FirebaseAnalytics(context = baseContext))
//Add Segment Analytics for tracking based on Config Flag
analytics.addAnalyticsTracker(SegmentAnalytics(context = baseContext))
}

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

import android.os.Bundle

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

import android.content.Context
import android.os.Bundle
import android.util.Log
import com.google.firebase.analytics.FirebaseAnalytics

class FirebaseAnalytics(context: Context) : Analytics {

private var tracker: FirebaseAnalytics? = null

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

override fun logScreenEvent(screenName: String, bundle: Bundle) {
Log.d("Analytics", "Firebase log Screen Event: $screenName + $bundle")
}

override fun logEvent(eventName: String, bundle: Bundle) {
Log.d("Analytics", "Firebase log Event $eventName: $bundle")
tracker?.logEvent(eventName, bundle)
}

override fun logUserId(userId: Long) {
tracker?.setUserId(userId.toString())
Log.d("Analytics", "Firebase User Id log Event")
}
}
26 changes: 26 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,26 @@
package org.openedx.app.analytics

import android.content.Context
import android.os.Bundle
import android.util.Log

class SegmentAnalytics(context: Context) : Analytics {

// private lateinit var tracker: com.segment.analytics.Analytics

init {
Log.d("Analytics", "Segment Builder Initialised")
}

override fun logScreenEvent(screenName: String, bundle: Bundle) {
Log.d("Analytics", "Segment log Screen Event: $screenName + $bundle")
}

override fun logEvent(eventName: String, bundle: Bundle) {
Log.d("Analytics", "Segment log Event $eventName: $bundle")
}

override fun logUserId(userId: Long) {
Log.d("Analytics", "Segment User Id log Event")
}
}
2 changes: 1 addition & 1 deletion app/src/main/java/org/openedx/app/di/AppModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ val appModule = module {
single { TranscriptManager(get()) }
single { WhatsNewFileManager(get()) }

single { AnalyticsManager(get()) }
single { AnalyticsManager() }
single<DashboardAnalytics> { get<AnalyticsManager>() }
single<AuthAnalytics> { get<AnalyticsManager>() }
single<AppAnalytics> { get<AnalyticsManager>() }
Expand Down

0 comments on commit a06939f

Please sign in to comment.