From d82abe53e7d641de320f71b593709c609182e389 Mon Sep 17 00:00:00 2001 From: Omer Habib Date: Fri, 24 Nov 2023 22:25:08 +0500 Subject: [PATCH] feat: Analytics architecture for multiple libraries fix: LEARNER-9667 --- .../java/org/openedx/app/AnalyticsManager.kt | 19 ++++++++---- .../main/java/org/openedx/app/OpenEdXApp.kt | 13 +++++--- .../org/openedx/app/analytics/Analytics.kt | 9 ++++++ .../app/analytics/FirebaseAnalytics.kt | 30 +++++++++++++++++++ .../main/java/org/openedx/app/di/AppModule.kt | 2 +- 5 files changed, 62 insertions(+), 11 deletions(-) create mode 100644 app/src/main/java/org/openedx/app/analytics/Analytics.kt create mode 100644 app/src/main/java/org/openedx/app/analytics/FirebaseAnalytics.kt diff --git a/app/src/main/java/org/openedx/app/AnalyticsManager.kt b/app/src/main/java/org/openedx/app/AnalyticsManager.kt index 0efcb7d54..89ef76b73 100644 --- a/app/src/main/java/org/openedx/app/AnalyticsManager.kt +++ b/app/src/main/java/org/openedx/app/AnalyticsManager.kt @@ -1,9 +1,8 @@ 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 @@ -11,17 +10,25 @@ 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 = 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) { diff --git a/app/src/main/java/org/openedx/app/OpenEdXApp.kt b/app/src/main/java/org/openedx/app/OpenEdXApp.kt index 7cb2b1ad5..0b807b3ba 100644 --- a/app/src/main/java/org/openedx/app/OpenEdXApp.kt +++ b/app/src/main/java/org/openedx/app/OpenEdXApp.kt @@ -4,14 +4,18 @@ 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.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 { @@ -31,7 +35,8 @@ class OpenEdXApp : Application() { .setGcmSenderId(org.openedx.core.BuildConfig.FIREBASE_GCM_SENDER_ID) .build() Firebase.initialize(this, options) + //Add Firebase Analytics for tracking + analytics.addAnalyticsTracker(FirebaseAnalytics(context = baseContext)) } } - -} \ No newline at end of file +} diff --git a/app/src/main/java/org/openedx/app/analytics/Analytics.kt b/app/src/main/java/org/openedx/app/analytics/Analytics.kt new file mode 100644 index 000000000..ed34ec41a --- /dev/null +++ b/app/src/main/java/org/openedx/app/analytics/Analytics.kt @@ -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) +} diff --git a/app/src/main/java/org/openedx/app/analytics/FirebaseAnalytics.kt b/app/src/main/java/org/openedx/app/analytics/FirebaseAnalytics.kt new file mode 100644 index 000000000..6e4db40a0 --- /dev/null +++ b/app/src/main/java/org/openedx/app/analytics/FirebaseAnalytics.kt @@ -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) { + tracker?.logEvent(eventName, bundle) + Log.d("Analytics", "Firebase log Event $eventName: $bundle") + } + + override fun logUserId(userId: Long) { + tracker?.setUserId(userId.toString()) + Log.d("Analytics", "Firebase User Id log Event") + } +} diff --git a/app/src/main/java/org/openedx/app/di/AppModule.kt b/app/src/main/java/org/openedx/app/di/AppModule.kt index 98e09bf08..71b613f20 100644 --- a/app/src/main/java/org/openedx/app/di/AppModule.kt +++ b/app/src/main/java/org/openedx/app/di/AppModule.kt @@ -125,7 +125,7 @@ val appModule = module { single { TranscriptManager(get()) } single { WhatsNewFileManager(get()) } - single { AnalyticsManager(get()) } + single { AnalyticsManager() } single { get() } single { get() } single { get() }