diff --git a/app/src/main/java/org/openedx/app/analytics/AnalyticsUtils.kt b/app/src/main/java/org/openedx/app/analytics/AnalyticsUtils.kt index 6d941eb62..e20e3f5a9 100644 --- a/app/src/main/java/org/openedx/app/analytics/AnalyticsUtils.kt +++ b/app/src/main/java/org/openedx/app/analytics/AnalyticsUtils.kt @@ -22,9 +22,8 @@ object AnalyticsUtils { }.toBundle() } - fun formatFirebaseAnalyticsDataForSegment(`object`: Any): Properties { - val properties = `object` as Properties - val newProperties = buildJsonObject { + fun formatFirebaseAnalyticsDataForSegment(properties: Properties): Properties { + return buildJsonObject { for ((key, value) in properties) { put( makeFirebaseAnalyticsKey(key), @@ -32,6 +31,5 @@ object AnalyticsUtils { ) } } - return newProperties } } diff --git a/app/src/main/java/org/openedx/app/analytics/SegmentAnalytics.kt b/app/src/main/java/org/openedx/app/analytics/SegmentAnalytics.kt index d35d2f470..37a34bc55 100644 --- a/app/src/main/java/org/openedx/app/analytics/SegmentAnalytics.kt +++ b/app/src/main/java/org/openedx/app/analytics/SegmentAnalytics.kt @@ -31,30 +31,24 @@ class SegmentAnalytics(context: Context, config: Config) : Analytics { if (config.getFirebaseConfig().isSegmentAnalyticsSource()) { tracker.add(plugin = FirebaseDestination(context = context)) + // Override the default event plugin to format the event and properties + // according to Firebase Analytics guidelines tracker.find(FirebaseDestination::class)?.add(object : EventPlugin { override lateinit var analytics: SegmentTracker override val type = Plugin.Type.Before - override fun execute(event: BaseEvent): BaseEvent { - when (event) { - is ScreenEvent -> { - event.apply { - name = AnalyticsUtils.makeFirebaseAnalyticsKey(name) - } - } - - is TrackEvent -> { - event.apply { - this.event = AnalyticsUtils.makeFirebaseAnalyticsKey(this.event) - properties = AnalyticsUtils.formatFirebaseAnalyticsDataForSegment(properties) - } - } + override fun track(payload: TrackEvent): BaseEvent { + return payload.apply { + this.event = AnalyticsUtils.makeFirebaseAnalyticsKey(this.event) + properties = + AnalyticsUtils.formatFirebaseAnalyticsDataForSegment(properties) + } + } - else -> { - //do nothing - } + override fun screen(payload: ScreenEvent): BaseEvent { + return payload.apply { + name = AnalyticsUtils.makeFirebaseAnalyticsKey(name) } - return event } }) }