Analytics: Fix a race condition

This commit is contained in:
Benoit Marty 2021-11-24 17:24:08 +01:00 committed by Benoit Marty
parent 24a6080090
commit 2968be2233
1 changed files with 16 additions and 7 deletions

View File

@ -40,6 +40,7 @@ class DefaultVectorAnalytics @Inject constructor(
private var posthog: PostHog? = null private var posthog: PostHog? = null
private var userConsent: Boolean = false private var userConsent: Boolean = false
private var analyticsId: String? = null
override fun getUserConsent(): Flow<Boolean> { override fun getUserConsent(): Flow<Boolean> {
return analyticsStore.userConsentFlow return analyticsStore.userConsentFlow
@ -83,6 +84,14 @@ class DefaultVectorAnalytics @Inject constructor(
getAnalyticsId() getAnalyticsId()
.onEach { id -> .onEach { id ->
Timber.tag(analyticsTag.value).d("Analytics Id updated to '$id'") Timber.tag(analyticsTag.value).d("Analytics Id updated to '$id'")
analyticsId = id
identifyPostHog()
}
.launchIn(GlobalScope)
}
private fun identifyPostHog() {
val id = analyticsId ?: return
if (id.isEmpty()) { if (id.isEmpty()) {
Timber.tag(analyticsTag.value).d("reset") Timber.tag(analyticsTag.value).d("reset")
posthog?.reset() posthog?.reset()
@ -91,8 +100,6 @@ class DefaultVectorAnalytics @Inject constructor(
posthog?.identify(id) posthog?.identify(id)
} }
} }
.launchIn(GlobalScope)
}
@Suppress("EXPERIMENTAL_API_USAGE") @Suppress("EXPERIMENTAL_API_USAGE")
private fun observeUserConsent() { private fun observeUserConsent() {
@ -134,6 +141,8 @@ class DefaultVectorAnalytics @Inject constructor(
.collectDeviceId(false) .collectDeviceId(false)
.logLevel(getLogLevel()) .logLevel(getLogLevel())
.build() .build()
identifyPostHog()
} }
private fun getLogLevel(): PostHog.LogLevel { private fun getLogLevel(): PostHog.LogLevel {