From 2968be22334d631fc98ce97cb68901c6a607972e Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Wed, 24 Nov 2021 17:24:08 +0100 Subject: [PATCH] Analytics: Fix a race condition --- .../analytics/impl/DefaultVectorAnalytics.kt | 23 +++++++++++++------ 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/vector/src/main/java/im/vector/app/features/analytics/impl/DefaultVectorAnalytics.kt b/vector/src/main/java/im/vector/app/features/analytics/impl/DefaultVectorAnalytics.kt index 3ebe65099c..4c14432906 100644 --- a/vector/src/main/java/im/vector/app/features/analytics/impl/DefaultVectorAnalytics.kt +++ b/vector/src/main/java/im/vector/app/features/analytics/impl/DefaultVectorAnalytics.kt @@ -40,6 +40,7 @@ class DefaultVectorAnalytics @Inject constructor( private var posthog: PostHog? = null private var userConsent: Boolean = false + private var analyticsId: String? = null override fun getUserConsent(): Flow { return analyticsStore.userConsentFlow @@ -83,17 +84,23 @@ class DefaultVectorAnalytics @Inject constructor( getAnalyticsId() .onEach { id -> Timber.tag(analyticsTag.value).d("Analytics Id updated to '$id'") - if (id.isEmpty()) { - Timber.tag(analyticsTag.value).d("reset") - posthog?.reset() - } else { - Timber.tag(analyticsTag.value).d("identify") - posthog?.identify(id) - } + analyticsId = id + identifyPostHog() } .launchIn(GlobalScope) } + private fun identifyPostHog() { + val id = analyticsId ?: return + if (id.isEmpty()) { + Timber.tag(analyticsTag.value).d("reset") + posthog?.reset() + } else { + Timber.tag(analyticsTag.value).d("identify") + posthog?.identify(id) + } + } + @Suppress("EXPERIMENTAL_API_USAGE") private fun observeUserConsent() { getUserConsent() @@ -134,6 +141,8 @@ class DefaultVectorAnalytics @Inject constructor( .collectDeviceId(false) .logLevel(getLogLevel()) .build() + + identifyPostHog() } private fun getLogLevel(): PostHog.LogLevel {