mirror of
https://github.com/element-hq/element-android
synced 2024-11-24 18:35:40 +03:00
Analytics: Create PostHog client only when user has given their consent
This commit is contained in:
parent
be2637c426
commit
55c7270ef2
1 changed files with 31 additions and 26 deletions
|
@ -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 a new issue