Skip to content

Commit

Permalink
feat: Analytics architecture for multiple libraries (openedx#91)
Browse files Browse the repository at this point in the history
- Introduced a way to implement multiple analytics libraries with only a little effort
- Write a base class to manage analytics with limited methods
- Also added the support to add analytics using feature flag implementation

fix: LEARNER-9667
  • Loading branch information
omerhabib26 authored and k1rill committed Dec 4, 2023
1 parent 01b5960 commit c7fa5d3
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 4 deletions.
25 changes: 21 additions & 4 deletions app/src/main/java/org/openedx/app/AnalyticsManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ 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.app.analytics.FirebaseAnalytics
import org.openedx.auth.presentation.AuthAnalytics
import org.openedx.core.BuildConfig
import org.openedx.course.presentation.CourseAnalytics
import org.openedx.dashboard.presentation.DashboardAnalytics
import org.openedx.discovery.presentation.DiscoveryAnalytics
Expand All @@ -14,14 +16,29 @@ import org.openedx.profile.presentation.ProfileAnalytics
class AnalyticsManager(context: Context) : DashboardAnalytics, AuthAnalytics, AppAnalytics,
DiscoveryAnalytics, ProfileAnalytics, CourseAnalytics, DiscussionAnalytics {

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

init {
// Initialise all the analytics libraries here
if (BuildConfig.FIREBASE_PROJECT_ID.isNotEmpty()) {
addAnalyticsTracker(FirebaseAnalytics(context = context))
}
}

private 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
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) {
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")
}
}

0 comments on commit c7fa5d3

Please sign in to comment.