Analytics: Create PostHog client only when user has given their consent

This commit is contained in:
Benoit Marty 2021-11-24 17:05:19 +01:00 committed by Benoit Marty
parent be2637c426
commit 55c7270ef2

View file

@ -70,6 +70,37 @@ class DefaultVectorAnalytics @Inject constructor(
}
override fun init() {
observeUserConsent()
observeAnalyticsId()
}
@Suppress("EXPERIMENTAL_API_USAGE")
private fun observeAnalyticsId() {
getAnalyticsId()
.onEach { id ->
if (id.isEmpty()) {
posthog?.reset()
} else {
posthog?.identify(id)
}
}
.launchIn(GlobalScope)
}
@Suppress("EXPERIMENTAL_API_USAGE")
private fun observeUserConsent() {
getUserConsent()
.onEach { consent ->
userConsent = consent
if (consent) {
createAnalyticsClient()
}
posthog?.optOut(!consent)
}
.launchIn(GlobalScope)
}
private fun createAnalyticsClient() {
val config: AnalyticsConfig = AnalyticsConfig.getConfig()
?: return Unit.also { Timber.w("Analytics is disabled") }
@ -93,9 +124,6 @@ class DefaultVectorAnalytics @Inject constructor(
.collectDeviceId(false)
.logLevel(getLogLevel())
.build()
observeUserConsent()
observeAnalyticsId()
}
private fun getLogLevel(): PostHog.LogLevel {
@ -106,29 +134,6 @@ class DefaultVectorAnalytics @Inject constructor(
}
}
@Suppress("EXPERIMENTAL_API_USAGE")
private fun observeAnalyticsId() {
getAnalyticsId()
.onEach { id ->
if (id.isEmpty()) {
posthog?.reset()
} else {
posthog?.identify(id)
}
}
.launchIn(GlobalScope)
}
@Suppress("EXPERIMENTAL_API_USAGE")
private fun observeUserConsent() {
getUserConsent()
.onEach { consent ->
userConsent = consent
posthog?.optOut(!consent)
}
.launchIn(GlobalScope)
}
override fun capture(event: String, properties: Map<String, Any>?) {
posthog
?.takeIf { userConsent }