Analytics: Create PostHog client only when user has given their consent
This commit is contained in:
parent
be2637c426
commit
55c7270ef2
|
@ -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 }
|
||||
|
|
Loading…
Reference in New Issue