mirror of
https://github.com/element-hq/element-android
synced 2024-11-28 13:38:49 +03:00
Analytics: Fix a crash, cannot create several time a PostHog client
This commit is contained in:
parent
2968be2233
commit
42d987f8ef
2 changed files with 20 additions and 16 deletions
|
@ -39,7 +39,8 @@ class DefaultVectorAnalytics @Inject constructor(
|
||||||
) : VectorAnalytics {
|
) : VectorAnalytics {
|
||||||
private var posthog: PostHog? = null
|
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
|
private var analyticsId: String? = null
|
||||||
|
|
||||||
override fun getUserConsent(): Flow<Boolean> {
|
override fun getUserConsent(): Flow<Boolean> {
|
||||||
|
@ -77,6 +78,7 @@ class DefaultVectorAnalytics @Inject constructor(
|
||||||
override fun init() {
|
override fun init() {
|
||||||
observeUserConsent()
|
observeUserConsent()
|
||||||
observeAnalyticsId()
|
observeAnalyticsId()
|
||||||
|
createAnalyticsClient()
|
||||||
}
|
}
|
||||||
|
|
||||||
@Suppress("EXPERIMENTAL_API_USAGE")
|
@Suppress("EXPERIMENTAL_API_USAGE")
|
||||||
|
@ -107,14 +109,15 @@ class DefaultVectorAnalytics @Inject constructor(
|
||||||
.onEach { consent ->
|
.onEach { consent ->
|
||||||
Timber.tag(analyticsTag.value).d("User consent updated to $consent")
|
Timber.tag(analyticsTag.value).d("User consent updated to $consent")
|
||||||
userConsent = consent
|
userConsent = consent
|
||||||
if (consent) {
|
optOutPostHog()
|
||||||
createAnalyticsClient()
|
|
||||||
}
|
|
||||||
posthog?.optOut(!consent)
|
|
||||||
}
|
}
|
||||||
.launchIn(GlobalScope)
|
.launchIn(GlobalScope)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun optOutPostHog() {
|
||||||
|
userConsent?.let { posthog?.optOut(!it) }
|
||||||
|
}
|
||||||
|
|
||||||
private fun createAnalyticsClient() {
|
private fun createAnalyticsClient() {
|
||||||
Timber.tag(analyticsTag.value).d("createAnalyticsClient()")
|
Timber.tag(analyticsTag.value).d("createAnalyticsClient()")
|
||||||
|
|
||||||
|
@ -142,6 +145,7 @@ class DefaultVectorAnalytics @Inject constructor(
|
||||||
.logLevel(getLogLevel())
|
.logLevel(getLogLevel())
|
||||||
.build()
|
.build()
|
||||||
|
|
||||||
|
optOutPostHog()
|
||||||
identifyPostHog()
|
identifyPostHog()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -156,14 +160,14 @@ class DefaultVectorAnalytics @Inject constructor(
|
||||||
override fun capture(event: String, properties: Map<String, Any>?) {
|
override fun capture(event: String, properties: Map<String, Any>?) {
|
||||||
Timber.tag(analyticsTag.value).d("capture($event)")
|
Timber.tag(analyticsTag.value).d("capture($event)")
|
||||||
posthog
|
posthog
|
||||||
?.takeIf { userConsent }
|
?.takeIf { userConsent == true }
|
||||||
?.capture(event, properties.toPostHogProperties())
|
?.capture(event, properties.toPostHogProperties())
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun screen(name: String, properties: Map<String, Any>?) {
|
override fun screen(name: String, properties: Map<String, Any>?) {
|
||||||
Timber.tag(analyticsTag.value).d("screen($name)")
|
Timber.tag(analyticsTag.value).d("screen($name)")
|
||||||
posthog
|
posthog
|
||||||
?.takeIf { userConsent }
|
?.takeIf { userConsent == true }
|
||||||
?.screen(name, properties.toPostHogProperties())
|
?.screen(name, properties.toPostHogProperties())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -24,6 +24,7 @@ import androidx.datastore.preferences.core.edit
|
||||||
import androidx.datastore.preferences.core.stringPreferencesKey
|
import androidx.datastore.preferences.core.stringPreferencesKey
|
||||||
import androidx.datastore.preferences.preferencesDataStore
|
import androidx.datastore.preferences.preferencesDataStore
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
import kotlinx.coroutines.flow.distinctUntilChanged
|
||||||
import kotlinx.coroutines.flow.map
|
import kotlinx.coroutines.flow.map
|
||||||
import org.matrix.android.sdk.api.extensions.orFalse
|
import org.matrix.android.sdk.api.extensions.orFalse
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
@ -43,17 +44,16 @@ class AnalyticsStore @Inject constructor(
|
||||||
private val didAskUserConsent = booleanPreferencesKey("did_ask_user_consent")
|
private val didAskUserConsent = booleanPreferencesKey("did_ask_user_consent")
|
||||||
private val analyticsId = stringPreferencesKey("analytics_id")
|
private val analyticsId = stringPreferencesKey("analytics_id")
|
||||||
|
|
||||||
val userConsentFlow: Flow<Boolean> = context.dataStore.data.map { preferences ->
|
val userConsentFlow: Flow<Boolean> = context.dataStore.data
|
||||||
preferences[userConsent].orFalse()
|
.map { preferences -> preferences[userConsent].orFalse() }
|
||||||
}
|
.distinctUntilChanged()
|
||||||
|
|
||||||
val didAskUserConsentFlow: Flow<Boolean> = context.dataStore.data.map { preferences ->
|
val didAskUserConsentFlow: Flow<Boolean> = context.dataStore.data
|
||||||
preferences[didAskUserConsent].orFalse()
|
.map { preferences -> preferences[didAskUserConsent].orFalse() }
|
||||||
}
|
|
||||||
|
|
||||||
val analyticsIdFlow: Flow<String> = context.dataStore.data.map { preferences ->
|
val analyticsIdFlow: Flow<String> = context.dataStore.data
|
||||||
preferences[analyticsId].orEmpty()
|
.map { preferences -> preferences[analyticsId].orEmpty() }
|
||||||
}
|
.distinctUntilChanged()
|
||||||
|
|
||||||
suspend fun setUserConsent(newUserConsent: Boolean) {
|
suspend fun setUserConsent(newUserConsent: Boolean) {
|
||||||
context.dataStore.edit { settings ->
|
context.dataStore.edit { settings ->
|
||||||
|
|
Loading…
Reference in a new issue