Merge pull request #666 from vector-im/yostyle/enable_analytics

This commit is contained in:
Yoan Pintas 2023-06-23 16:35:58 +02:00 committed by GitHub
commit 1c1e65c70d
3 changed files with 20 additions and 21 deletions

View file

@ -33,6 +33,7 @@ import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.flow.onEach
import timber.log.Timber import timber.log.Timber
import java.util.concurrent.atomic.AtomicBoolean
import javax.inject.Inject import javax.inject.Inject
@SingleIn(AppScope::class) @SingleIn(AppScope::class)
@ -45,7 +46,7 @@ class DefaultAnalyticsService @Inject constructor(
private val sessionObserver: SessionObserver, private val sessionObserver: SessionObserver,
) : AnalyticsService, SessionListener { ) : AnalyticsService, SessionListener {
// Cache for the store values // Cache for the store values
private var userConsent: Boolean? = null private val userConsent = AtomicBoolean(false)
// Cache for the properties to send // Cache for the properties to send
private var pendingUserProperties: UserProperties? = null private var pendingUserProperties: UserProperties? = null
@ -104,7 +105,7 @@ class DefaultAnalyticsService @Inject constructor(
getUserConsent() getUserConsent()
.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.set(consent)
initOrStop() initOrStop()
} }
.launchIn(coroutineScope) .launchIn(coroutineScope)
@ -115,35 +116,33 @@ class DefaultAnalyticsService @Inject constructor(
} }
private fun initOrStop() { private fun initOrStop() {
userConsent?.let { _userConsent -> if (userConsent.get()) {
when (_userConsent) { analyticsProviders.onEach { it.init() }
true -> { pendingUserProperties?.let {
pendingUserProperties?.let { analyticsProviders.onEach { provider -> provider.updateUserProperties(it) }
analyticsProviders.onEach { provider -> provider.updateUserProperties(it) } pendingUserProperties = null
pendingUserProperties = null
}
}
false -> {}
} }
} else {
analyticsProviders.onEach { it.stop() }
} }
} }
override fun capture(event: VectorAnalyticsEvent) { override fun capture(event: VectorAnalyticsEvent) {
Timber.tag(analyticsTag.value).d("capture($event)") Timber.tag(analyticsTag.value).d("capture($event)")
if (userConsent == true) { if (userConsent.get()) {
analyticsProviders.onEach { it.capture(event) } analyticsProviders.onEach { it.capture(event) }
} }
} }
override fun screen(screen: VectorAnalyticsScreen) { override fun screen(screen: VectorAnalyticsScreen) {
Timber.tag(analyticsTag.value).d("screen($screen)") Timber.tag(analyticsTag.value).d("screen($screen)")
if (userConsent == true) { if (userConsent.get()) {
analyticsProviders.onEach { it.screen(screen) } analyticsProviders.onEach { it.screen(screen) }
} }
} }
override fun updateUserProperties(userProperties: UserProperties) { override fun updateUserProperties(userProperties: UserProperties) {
if (userConsent == true) { if (userConsent.get()) {
analyticsProviders.onEach { it.updateUserProperties(userProperties) } analyticsProviders.onEach { it.updateUserProperties(userProperties) }
} else { } else {
pendingUserProperties = userProperties pendingUserProperties = userProperties
@ -151,7 +150,7 @@ class DefaultAnalyticsService @Inject constructor(
} }
override fun trackError(throwable: Throwable) { override fun trackError(throwable: Throwable) {
if (userConsent == true) { if (userConsent.get()) {
analyticsProviders.onEach { it.trackError(throwable) } analyticsProviders.onEach { it.trackError(throwable) }
} }
} }

View file

@ -30,7 +30,7 @@ interface AnalyticsProvider: AnalyticsTracker, ErrorTracker {
*/ */
val name: String val name: String
suspend fun init() fun init()
fun stop() fun stop()
} }

View file

@ -42,7 +42,7 @@ class PosthogAnalyticsProvider @Inject constructor(
private var posthog: PostHog? = null private var posthog: PostHog? = null
private var analyticsId: String? = null private var analyticsId: String? = null
override suspend fun init() { override fun init() {
posthog = createPosthog() posthog = createPosthog()
posthog?.optOut(false) posthog?.optOut(false)
identifyPostHog() identifyPostHog()
@ -66,10 +66,10 @@ class PosthogAnalyticsProvider @Inject constructor(
} }
override fun updateUserProperties(userProperties: UserProperties) { override fun updateUserProperties(userProperties: UserProperties) {
posthog?.identify( // posthog?.identify(
REUSE_EXISTING_ID, userProperties.getProperties()?.toPostHogUserProperties(), // REUSE_EXISTING_ID, userProperties.getProperties()?.toPostHogUserProperties(),
IGNORED_OPTIONS // IGNORED_OPTIONS
) // )
} }
override fun trackError(throwable: Throwable) { override fun trackError(throwable: Throwable) {