Analytics: observe the store and react
This commit is contained in:
parent
995e1e3d49
commit
8752fe1e69
@ -22,7 +22,10 @@ import com.posthog.android.Properties
|
|||||||
import im.vector.app.features.analytics.AnalyticsConfig
|
import im.vector.app.features.analytics.AnalyticsConfig
|
||||||
import im.vector.app.features.analytics.VectorAnalytics
|
import im.vector.app.features.analytics.VectorAnalytics
|
||||||
import im.vector.app.features.analytics.store.AnalyticsStore
|
import im.vector.app.features.analytics.store.AnalyticsStore
|
||||||
|
import kotlinx.coroutines.GlobalScope
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
import kotlinx.coroutines.flow.onEach
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
import javax.inject.Singleton
|
import javax.inject.Singleton
|
||||||
@ -34,6 +37,8 @@ class DefaultVectorAnalytics @Inject constructor(
|
|||||||
) : VectorAnalytics {
|
) : VectorAnalytics {
|
||||||
private var posthog: PostHog? = null
|
private var posthog: PostHog? = null
|
||||||
|
|
||||||
|
private var userConsent: Boolean = false
|
||||||
|
|
||||||
override fun getUserConsent(): Flow<Boolean> {
|
override fun getUserConsent(): Flow<Boolean> {
|
||||||
return analyticsStore.userConsentFlow
|
return analyticsStore.userConsentFlow
|
||||||
}
|
}
|
||||||
@ -61,8 +66,6 @@ class DefaultVectorAnalytics @Inject constructor(
|
|||||||
override suspend fun onSignOut() {
|
override suspend fun onSignOut() {
|
||||||
// reset the analyticsId
|
// reset the analyticsId
|
||||||
setAnalyticsId("")
|
setAnalyticsId("")
|
||||||
// reset the library
|
|
||||||
posthog?.reset()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun init() {
|
override fun init() {
|
||||||
@ -88,20 +91,43 @@ class DefaultVectorAnalytics @Inject constructor(
|
|||||||
// Enable or disable collection of ANDROID_ID (true)
|
// Enable or disable collection of ANDROID_ID (true)
|
||||||
.collectDeviceId(false)
|
.collectDeviceId(false)
|
||||||
.build()
|
.build()
|
||||||
|
|
||||||
|
observeUserConsent()
|
||||||
|
observeAnalyticsId()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Suppress("EXPERIMENTAL_API_USAGE")
|
||||||
|
private fun observeAnalyticsId() {
|
||||||
|
GlobalScope.launch {
|
||||||
|
getAnalyticsId().onEach {
|
||||||
|
if (it.isEmpty()) {
|
||||||
|
posthog?.reset()
|
||||||
|
} else {
|
||||||
|
posthog?.identify(it)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Suppress("EXPERIMENTAL_API_USAGE")
|
||||||
|
private fun observeUserConsent() {
|
||||||
|
GlobalScope.launch {
|
||||||
|
getUserConsent().onEach {
|
||||||
|
userConsent = it
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun capture(event: String, properties: Map<String, Any>?) {
|
override fun capture(event: String, properties: Map<String, Any>?) {
|
||||||
posthog?.capture(
|
posthog
|
||||||
event,
|
?.takeIf { userConsent }
|
||||||
properties.toPostHogProperties()
|
?.capture(event, properties.toPostHogProperties())
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun screen(name: String, properties: Map<String, Any>?) {
|
override fun screen(name: String, properties: Map<String, Any>?) {
|
||||||
posthog?.screen(
|
posthog
|
||||||
name,
|
?.takeIf { userConsent }
|
||||||
properties.toPostHogProperties()
|
?.screen(name, properties.toPostHogProperties())
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun Map<String, Any>?.toPostHogProperties(): Properties? {
|
private fun Map<String, Any>?.toPostHogProperties(): Properties? {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user