From 7949af8aecee43fbd0e730fac84de5c1d4d7f0fd Mon Sep 17 00:00:00 2001 From: Daniel Frett Date: Thu, 12 Oct 2023 14:40:15 -0600 Subject: [PATCH] record the authenticated account type as a firebase user property --- gradle/libs.versions.toml | 2 +- .../account/GodToolsAccountManager.kt | 4 ++++ .../firebase/FirebaseAnalyticsService.kt | 19 +++++++++++++++---- .../firebase/FirebaseAnalyticsServiceTest.kt | 2 +- 4 files changed, 21 insertions(+), 6 deletions(-) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index df46db9127..c2e2a04f6e 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -16,7 +16,7 @@ facebook = "16.2.0" facebook-flipper = "0.226.0" firebase-crashlytics = "18.4.3" firebase-perf = "20.4.1" -godtoolsShared = "0.9.1" +godtoolsShared = "0.9.2-SNAPSHOT" google-auto-value = "1.10.4" gtoSupport = "4.2.0-SNAPSHOT" kotlin = "1.9.10" diff --git a/library/account/src/main/kotlin/org/cru/godtools/account/GodToolsAccountManager.kt b/library/account/src/main/kotlin/org/cru/godtools/account/GodToolsAccountManager.kt index a70699a016..20477a6d59 100644 --- a/library/account/src/main/kotlin/org/cru/godtools/account/GodToolsAccountManager.kt +++ b/library/account/src/main/kotlin/org/cru/godtools/account/GodToolsAccountManager.kt @@ -48,6 +48,10 @@ class GodToolsAccountManager @VisibleForTesting internal constructor( .flatMapLatest { it?.isAuthenticatedFlow() ?: flowOf(false) } .shareIn(coroutineScope, SharingStarted.WhileSubscribed(), replay = 1) .distinctUntilChanged() + val authenticatedAccountTypeFlow = activeProviderFlow + .map { it?.type } + .shareIn(coroutineScope, SharingStarted.WhileSubscribed(), replay = 1) + .distinctUntilChanged() val userIdFlow = activeProviderFlow .flatMapLatest { it?.userIdFlow() ?: flowOf(null) } .shareIn(coroutineScope, SharingStarted.WhileSubscribed(), replay = 1) diff --git a/library/analytics/src/main/kotlin/org/cru/godtools/analytics/firebase/FirebaseAnalyticsService.kt b/library/analytics/src/main/kotlin/org/cru/godtools/analytics/firebase/FirebaseAnalyticsService.kt index 905675e1de..f2a42c086d 100644 --- a/library/analytics/src/main/kotlin/org/cru/godtools/analytics/firebase/FirebaseAnalyticsService.kt +++ b/library/analytics/src/main/kotlin/org/cru/godtools/analytics/firebase/FirebaseAnalyticsService.kt @@ -13,12 +13,14 @@ import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.onEach +import org.cru.godtools.account.AccountType import org.cru.godtools.account.GodToolsAccountManager import org.cru.godtools.analytics.BuildConfig import org.cru.godtools.analytics.model.AnalyticsActionEvent import org.cru.godtools.analytics.model.AnalyticsBaseEvent import org.cru.godtools.analytics.model.AnalyticsScreenEvent import org.cru.godtools.analytics.model.AnalyticsSystem +import org.cru.godtools.shared.analytics.AnalyticsUserProperties import org.cru.godtools.user.data.UserManager import org.greenrobot.eventbus.EventBus import org.greenrobot.eventbus.Subscribe @@ -33,7 +35,6 @@ private const val VALUE_APP_TYPE_INSTANT = "instant" private const val VALUE_APP_TYPE_INSTALLED = "installed" private const val USER_PROP_DEBUG = "debug" -private const val USER_PROP_LOGGED_IN_STATUS = "cru_loggedinstatus" private const val USER_PROP_SSO_GUID = "cru_ssoguid" private const val USER_PROP_GR_MASTER_PERSON_ID = "cru_grmasterpersonid" @@ -49,7 +50,7 @@ class FirebaseAnalyticsService @VisibleForTesting internal constructor( eventBus: EventBus, userManager: UserManager, private val firebase: FirebaseAnalytics, - coroutineScope: CoroutineScope = CoroutineScope(Dispatchers.Default) + coroutineScope: CoroutineScope = CoroutineScope(Dispatchers.Default), ) { @Inject @MainThread @@ -108,8 +109,18 @@ class FirebaseAnalyticsService @VisibleForTesting internal constructor( } .launchIn(coroutineScope) - accountManager.isAuthenticatedFlow - .onEach { firebase.setUserProperty(USER_PROP_LOGGED_IN_STATUS, "$it") } + accountManager.authenticatedAccountTypeFlow + .onEach { + firebase.setUserProperty(AnalyticsUserProperties.LOGGED_IN_STATUS, "${it != null}") + firebase.setUserProperty( + AnalyticsUserProperties.LOGIN_PROVIDER, + when (it) { + AccountType.FACEBOOK -> AnalyticsUserProperties.LOGIN_PROVIDER_FACEBOOK + AccountType.GOOGLE -> AnalyticsUserProperties.LOGIN_PROVIDER_GOOGLE + null -> null + } + ) + } .launchIn(coroutineScope) firebase.setUserProperty(USER_PROP_APP_NAME, VALUE_APP_NAME_GODTOOLS) diff --git a/library/analytics/src/test/kotlin/org/cru/godtools/analytics/firebase/FirebaseAnalyticsServiceTest.kt b/library/analytics/src/test/kotlin/org/cru/godtools/analytics/firebase/FirebaseAnalyticsServiceTest.kt index 0db33063a5..67ba16fc8f 100644 --- a/library/analytics/src/test/kotlin/org/cru/godtools/analytics/firebase/FirebaseAnalyticsServiceTest.kt +++ b/library/analytics/src/test/kotlin/org/cru/godtools/analytics/firebase/FirebaseAnalyticsServiceTest.kt @@ -27,7 +27,7 @@ class FirebaseAnalyticsServiceTest { private val userFlow = MutableSharedFlow() private val accountManager: GodToolsAccountManager = mockk { - every { isAuthenticatedFlow } returns flowOf(false) + every { authenticatedAccountTypeFlow } returns flowOf(null) } private val eventBus: EventBus = mockk(relaxUnitFun = true) private val firebase: FirebaseAnalytics = mockk(relaxUnitFun = true)