Skip to content

Commit fbdf95c

Browse files
authored
feat: use logEvent instead of deprecated setCurrentScreen and forward screenView attributes (#106)
1 parent 808dd61 commit fbdf95c

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

src/main/kotlin/com/mparticle/kits/GoogleAnalyticsFirebaseKit.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,16 @@ class GoogleAnalyticsFirebaseKit : KitIntegration(), KitIntegration.EventListene
5757
return listOf(ReportingMessage.fromEvent(this, mpEvent))
5858
}
5959

60-
override fun logScreen(s: String, map: Map<String, String>?): List<ReportingMessage> {
60+
override fun logScreen(
61+
screenName: String,
62+
screenAttributes: Map<String, String>?
63+
): List<ReportingMessage> {
64+
val bundle = toBundle(screenAttributes)
65+
bundle.putString(FirebaseAnalytics.Param.SCREEN_NAME, standardizeName(screenName, true))
6166
val activity = currentActivity.get()
6267
if (activity != null) {
6368
FirebaseAnalytics.getInstance(context)
64-
.setCurrentScreen(activity, standardizeName(s, true), null)
69+
.logEvent(FirebaseAnalytics.Event.SCREEN_VIEW, bundle)
6570
return listOf(
6671
ReportingMessage(
6772
this,

src/test/kotlin/com/mparticle/kits/GoogleAnalyticsFirebaseKitTest.kt

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -784,9 +784,33 @@ class GoogleAnalyticsFirebaseKitTest {
784784
@Test
785785
fun testScreenNameSanitized() {
786786
kitInstance.logScreen("Some long Screen name", emptyMap())
787+
val firebaseScreenViewEvent = firebaseSdk.loggedEvents[0]
787788
TestCase.assertEquals(
788789
"Some_long_Screen_name",
789-
FirebaseAnalytics.getInstance(null)?.currentScreenName
790+
firebaseScreenViewEvent.value.getString("screen_name")
791+
)
792+
}
793+
794+
@Test
795+
fun testScreenNameAttributes() {
796+
val attributes = hashMapOf<String, String>()
797+
attributes["testAttributeKey"] = "testAttributeValue"
798+
kitInstance.logScreen("testScreenName", attributes)
799+
val firebaseScreenViewEvent = firebaseSdk.loggedEvents[0]
800+
// even though we are passing one attribute, it should contain two including the screen_name
801+
TestCase.assertEquals(
802+
2,
803+
firebaseScreenViewEvent.value.size()
804+
)
805+
// make sure the even name is correct with Firebase's constant SCREEN_NAME value
806+
TestCase.assertEquals(
807+
"screen_view",
808+
firebaseScreenViewEvent.key
809+
)
810+
// make sure that the Params include the screenName value
811+
TestCase.assertEquals(
812+
"testScreenName",
813+
firebaseScreenViewEvent.value.getString("screen_name")
790814
)
791815
}
792816

0 commit comments

Comments
 (0)