From 6a3f5a50d91e1fe6f01a9eb1e68d24917ed25ff2 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Mon, 13 Mar 2023 10:33:18 +0100 Subject: [PATCH 1/2] Create Posthog instance only whe user consent is given, to avoid pinging Posthog server at startup when consent is not given. Note that feature flag will not work, but for now they are not used. All the `?.takeIf { userConsent == true }` could be removed with this change, but let's keep them for safety... --- .../analytics/impl/DefaultVectorAnalytics.kt | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/vector/src/main/java/im/vector/app/features/analytics/impl/DefaultVectorAnalytics.kt b/vector/src/main/java/im/vector/app/features/analytics/impl/DefaultVectorAnalytics.kt index 917c3468c6..42193b9d1e 100644 --- a/vector/src/main/java/im/vector/app/features/analytics/impl/DefaultVectorAnalytics.kt +++ b/vector/src/main/java/im/vector/app/features/analytics/impl/DefaultVectorAnalytics.kt @@ -41,19 +41,23 @@ private val IGNORED_OPTIONS: Options? = null @Singleton class DefaultVectorAnalytics @Inject constructor( - postHogFactory: PostHogFactory, + private val postHogFactory: PostHogFactory, private val sentryAnalytics: SentryAnalytics, - analyticsConfig: AnalyticsConfig, + private val analyticsConfig: AnalyticsConfig, private val analyticsStore: AnalyticsStore, private val lateInitUserPropertiesFactory: LateInitUserPropertiesFactory, @NamedGlobalScope private val globalScope: CoroutineScope ) : VectorAnalytics { - private val posthog: PostHog? = when { - analyticsConfig.isEnabled -> postHogFactory.createPosthog() - else -> { - Timber.tag(analyticsTag.value).w("Analytics is disabled") - null + private var posthog: PostHog? = null + + private fun createPosthog(): PostHog? { + return when { + analyticsConfig.isEnabled -> postHogFactory.createPosthog() + else -> { + Timber.tag(analyticsTag.value).w("Analytics is disabled") + null + } } } @@ -150,6 +154,7 @@ class DefaultVectorAnalytics @Inject constructor( userConsent?.let { _userConsent -> when (_userConsent) { true -> { + posthog = createPosthog() posthog?.optOut(false) identifyPostHog() pendingUserProperties?.let { doUpdateUserProperties(it) } @@ -159,6 +164,7 @@ class DefaultVectorAnalytics @Inject constructor( // When opting out, ensure that the queue is flushed first, or it will be flushed later (after user has revoked consent) posthog?.flush() posthog?.optOut(true) + posthog = null } } } From 7857339f54595dc5121895c072c57e12088a5434 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Thu, 16 Mar 2023 09:47:45 +0100 Subject: [PATCH 2/2] Fix test --- .../features/analytics/impl/DefaultVectorAnalyticsTest.kt | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/vector/src/test/java/im/vector/app/features/analytics/impl/DefaultVectorAnalyticsTest.kt b/vector/src/test/java/im/vector/app/features/analytics/impl/DefaultVectorAnalyticsTest.kt index 084ee2410e..226a2d8ecf 100644 --- a/vector/src/test/java/im/vector/app/features/analytics/impl/DefaultVectorAnalyticsTest.kt +++ b/vector/src/test/java/im/vector/app/features/analytics/impl/DefaultVectorAnalyticsTest.kt @@ -80,6 +80,12 @@ class DefaultVectorAnalyticsTest { @Test fun `when revoking consent to analytics then updates posthog opt out to true and closes Sentry`() = runTest { + // For opt-out to have effect on Posthog, it has to be used first, so it has to be opt-in first + fakeAnalyticsStore.givenUserContent(consent = true) + fakePostHog.verifyOptOutStatus(optedOut = false) + fakeSentryAnalytics.verifySentryInit() + + // Then test opt-out fakeAnalyticsStore.givenUserContent(consent = false) fakePostHog.verifyOptOutStatus(optedOut = true)