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...
This commit is contained in:
parent
8e9a364c8d
commit
6a3f5a50d9
|
@ -41,21 +41,25 @@ private val IGNORED_OPTIONS: Options? = null
|
||||||
|
|
||||||
@Singleton
|
@Singleton
|
||||||
class DefaultVectorAnalytics @Inject constructor(
|
class DefaultVectorAnalytics @Inject constructor(
|
||||||
postHogFactory: PostHogFactory,
|
private val postHogFactory: PostHogFactory,
|
||||||
private val sentryAnalytics: SentryAnalytics,
|
private val sentryAnalytics: SentryAnalytics,
|
||||||
analyticsConfig: AnalyticsConfig,
|
private val analyticsConfig: AnalyticsConfig,
|
||||||
private val analyticsStore: AnalyticsStore,
|
private val analyticsStore: AnalyticsStore,
|
||||||
private val lateInitUserPropertiesFactory: LateInitUserPropertiesFactory,
|
private val lateInitUserPropertiesFactory: LateInitUserPropertiesFactory,
|
||||||
@NamedGlobalScope private val globalScope: CoroutineScope
|
@NamedGlobalScope private val globalScope: CoroutineScope
|
||||||
) : VectorAnalytics {
|
) : VectorAnalytics {
|
||||||
|
|
||||||
private val posthog: PostHog? = when {
|
private var posthog: PostHog? = null
|
||||||
|
|
||||||
|
private fun createPosthog(): PostHog? {
|
||||||
|
return when {
|
||||||
analyticsConfig.isEnabled -> postHogFactory.createPosthog()
|
analyticsConfig.isEnabled -> postHogFactory.createPosthog()
|
||||||
else -> {
|
else -> {
|
||||||
Timber.tag(analyticsTag.value).w("Analytics is disabled")
|
Timber.tag(analyticsTag.value).w("Analytics is disabled")
|
||||||
null
|
null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Cache for the store values
|
// Cache for the store values
|
||||||
private var userConsent: Boolean? = null
|
private var userConsent: Boolean? = null
|
||||||
|
@ -150,6 +154,7 @@ class DefaultVectorAnalytics @Inject constructor(
|
||||||
userConsent?.let { _userConsent ->
|
userConsent?.let { _userConsent ->
|
||||||
when (_userConsent) {
|
when (_userConsent) {
|
||||||
true -> {
|
true -> {
|
||||||
|
posthog = createPosthog()
|
||||||
posthog?.optOut(false)
|
posthog?.optOut(false)
|
||||||
identifyPostHog()
|
identifyPostHog()
|
||||||
pendingUserProperties?.let { doUpdateUserProperties(it) }
|
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)
|
// When opting out, ensure that the queue is flushed first, or it will be flushed later (after user has revoked consent)
|
||||||
posthog?.flush()
|
posthog?.flush()
|
||||||
posthog?.optOut(true)
|
posthog?.optOut(true)
|
||||||
|
posthog = null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue