Analytics: Fix a crash, cannot create several time a PostHog client

This commit is contained in:
Benoit Marty 2021-11-24 17:36:03 +01:00 committed by Benoit Marty
parent 2968be2233
commit 42d987f8ef
2 changed files with 20 additions and 16 deletions

View file

@ -39,7 +39,8 @@ class DefaultVectorAnalytics @Inject constructor(
) : VectorAnalytics {
private var posthog: PostHog? = null
private var userConsent: Boolean = false
// Cache for the store values
private var userConsent: Boolean? = null
private var analyticsId: String? = null
override fun getUserConsent(): Flow<Boolean> {
@ -77,6 +78,7 @@ class DefaultVectorAnalytics @Inject constructor(
override fun init() {
observeUserConsent()
observeAnalyticsId()
createAnalyticsClient()
}
@Suppress("EXPERIMENTAL_API_USAGE")
@ -107,14 +109,15 @@ class DefaultVectorAnalytics @Inject constructor(
.onEach { consent ->
Timber.tag(analyticsTag.value).d("User consent updated to $consent")
userConsent = consent
if (consent) {
createAnalyticsClient()
}
posthog?.optOut(!consent)
optOutPostHog()
}
.launchIn(GlobalScope)
}
private fun optOutPostHog() {
userConsent?.let { posthog?.optOut(!it) }
}
private fun createAnalyticsClient() {
Timber.tag(analyticsTag.value).d("createAnalyticsClient()")
@ -142,6 +145,7 @@ class DefaultVectorAnalytics @Inject constructor(
.logLevel(getLogLevel())
.build()
optOutPostHog()
identifyPostHog()
}
@ -156,14 +160,14 @@ class DefaultVectorAnalytics @Inject constructor(
override fun capture(event: String, properties: Map<String, Any>?) {
Timber.tag(analyticsTag.value).d("capture($event)")
posthog
?.takeIf { userConsent }
?.takeIf { userConsent == true }
?.capture(event, properties.toPostHogProperties())
}
override fun screen(name: String, properties: Map<String, Any>?) {
Timber.tag(analyticsTag.value).d("screen($name)")
posthog
?.takeIf { userConsent }
?.takeIf { userConsent == true }
?.screen(name, properties.toPostHogProperties())
}

View file

@ -24,6 +24,7 @@ import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.stringPreferencesKey
import androidx.datastore.preferences.preferencesDataStore
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.map
import org.matrix.android.sdk.api.extensions.orFalse
import javax.inject.Inject
@ -43,17 +44,16 @@ class AnalyticsStore @Inject constructor(
private val didAskUserConsent = booleanPreferencesKey("did_ask_user_consent")
private val analyticsId = stringPreferencesKey("analytics_id")
val userConsentFlow: Flow<Boolean> = context.dataStore.data.map { preferences ->
preferences[userConsent].orFalse()
}
val userConsentFlow: Flow<Boolean> = context.dataStore.data
.map { preferences -> preferences[userConsent].orFalse() }
.distinctUntilChanged()
val didAskUserConsentFlow: Flow<Boolean> = context.dataStore.data.map { preferences ->
preferences[didAskUserConsent].orFalse()
}
val didAskUserConsentFlow: Flow<Boolean> = context.dataStore.data
.map { preferences -> preferences[didAskUserConsent].orFalse() }
val analyticsIdFlow: Flow<String> = context.dataStore.data.map { preferences ->
preferences[analyticsId].orEmpty()
}
val analyticsIdFlow: Flow<String> = context.dataStore.data
.map { preferences -> preferences[analyticsId].orEmpty() }
.distinctUntilChanged()
suspend fun setUserConsent(newUserConsent: Boolean) {
context.dataStore.edit { settings ->