Analytics: Improve logs

This commit is contained in:
Benoit Marty 2021-11-24 17:16:51 +01:00 committed by Benoit Marty
parent 55c7270ef2
commit 24a6080090
4 changed files with 44 additions and 5 deletions

View File

@ -17,6 +17,7 @@
package im.vector.app.features.analytics package im.vector.app.features.analytics
import im.vector.app.BuildConfig import im.vector.app.BuildConfig
import im.vector.app.features.analytics.log.analyticsTag
import timber.log.Timber import timber.log.Timber
data class AnalyticsConfig( data class AnalyticsConfig(
@ -29,9 +30,13 @@ data class AnalyticsConfig(
*/ */
fun getConfig(): AnalyticsConfig? { fun getConfig(): AnalyticsConfig? {
val postHogHost = BuildConfig.ANALYTICS_POSTHOG_HOST.takeIf { it.isNotEmpty() } val postHogHost = BuildConfig.ANALYTICS_POSTHOG_HOST.takeIf { it.isNotEmpty() }
?: return null.also { Timber.w("Analytics is disabled, ANALYTICS_POSTHOG_HOST is empty") } ?: return null.also {
Timber.tag(analyticsTag.value).w("Analytics is disabled, ANALYTICS_POSTHOG_HOST is empty")
}
val postHogApiKey = BuildConfig.ANALYTICS_POSTHOG_API_KEY.takeIf { it.isNotEmpty() } val postHogApiKey = BuildConfig.ANALYTICS_POSTHOG_API_KEY.takeIf { it.isNotEmpty() }
?: return null.also { Timber.w("Analytics is disabled, ANALYTICS_POSTHOG_API_KEY is empty") } ?: return null.also {
Timber.tag(analyticsTag.value).w("Analytics is disabled, ANALYTICS_POSTHOG_API_KEY is empty")
}
return AnalyticsConfig( return AnalyticsConfig(
postHogHost = postHogHost, postHogHost = postHogHost,

View File

@ -28,6 +28,7 @@ import im.vector.app.core.platform.EmptyAction
import im.vector.app.core.platform.EmptyViewEvents import im.vector.app.core.platform.EmptyViewEvents
import im.vector.app.core.platform.VectorViewModel import im.vector.app.core.platform.VectorViewModel
import im.vector.app.features.analytics.VectorAnalytics import im.vector.app.features.analytics.VectorAnalytics
import im.vector.app.features.analytics.log.analyticsTag
import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.mapNotNull import kotlinx.coroutines.flow.mapNotNull
@ -93,9 +94,9 @@ class AnalyticsAccountDataViewModel @AssistedInject constructor(
if (analyticsAccountDataContent.id.isNullOrEmpty()) { if (analyticsAccountDataContent.id.isNullOrEmpty()) {
// Probably consent revoked from Element Web // Probably consent revoked from Element Web
// Ignore here // Ignore here
Timber.d("Consent revoked from Element Web?") Timber.tag(analyticsTag.value).d("Consent revoked from Element Web?")
} else { } else {
Timber.d("AnalyticsId has been retrieved") Timber.tag(analyticsTag.value).d("AnalyticsId has been retrieved")
analytics.setAnalyticsId(analyticsAccountDataContent.id) analytics.setAnalyticsId(analyticsAccountDataContent.id)
} }
} }

View File

@ -22,6 +22,7 @@ import com.posthog.android.Properties
import im.vector.app.BuildConfig import im.vector.app.BuildConfig
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.log.analyticsTag
import im.vector.app.features.analytics.store.AnalyticsStore import im.vector.app.features.analytics.store.AnalyticsStore
import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
@ -45,6 +46,7 @@ class DefaultVectorAnalytics @Inject constructor(
} }
override suspend fun setUserConsent(userConsent: Boolean) { override suspend fun setUserConsent(userConsent: Boolean) {
Timber.tag(analyticsTag.value).d("setUserConsent($userConsent)")
analyticsStore.setUserConsent(userConsent) analyticsStore.setUserConsent(userConsent)
} }
@ -53,6 +55,7 @@ class DefaultVectorAnalytics @Inject constructor(
} }
override suspend fun setDidAskUserConsent() { override suspend fun setDidAskUserConsent() {
Timber.tag(analyticsTag.value).d("setDidAskUserConsent()")
analyticsStore.setDidAskUserConsent() analyticsStore.setDidAskUserConsent()
} }
@ -61,6 +64,7 @@ class DefaultVectorAnalytics @Inject constructor(
} }
override suspend fun setAnalyticsId(analyticsId: String) { override suspend fun setAnalyticsId(analyticsId: String) {
Timber.tag(analyticsTag.value).d("setAnalyticsId($analyticsId)")
analyticsStore.setAnalyticsId(analyticsId) analyticsStore.setAnalyticsId(analyticsId)
} }
@ -78,9 +82,12 @@ class DefaultVectorAnalytics @Inject constructor(
private fun observeAnalyticsId() { private fun observeAnalyticsId() {
getAnalyticsId() getAnalyticsId()
.onEach { id -> .onEach { id ->
Timber.tag(analyticsTag.value).d("Analytics Id updated to '$id'")
if (id.isEmpty()) { if (id.isEmpty()) {
Timber.tag(analyticsTag.value).d("reset")
posthog?.reset() posthog?.reset()
} else { } else {
Timber.tag(analyticsTag.value).d("identify")
posthog?.identify(id) posthog?.identify(id)
} }
} }
@ -91,6 +98,7 @@ class DefaultVectorAnalytics @Inject constructor(
private fun observeUserConsent() { private fun observeUserConsent() {
getUserConsent() getUserConsent()
.onEach { consent -> .onEach { consent ->
Timber.tag(analyticsTag.value).d("User consent updated to $consent")
userConsent = consent userConsent = consent
if (consent) { if (consent) {
createAnalyticsClient() createAnalyticsClient()
@ -101,8 +109,10 @@ class DefaultVectorAnalytics @Inject constructor(
} }
private fun createAnalyticsClient() { private fun createAnalyticsClient() {
Timber.tag(analyticsTag.value).d("createAnalyticsClient()")
val config: AnalyticsConfig = AnalyticsConfig.getConfig() val config: AnalyticsConfig = AnalyticsConfig.getConfig()
?: return Unit.also { Timber.w("Analytics is disabled") } ?: return Unit.also { Timber.tag(analyticsTag.value).w("Analytics is disabled") }
posthog = PostHog.Builder(context, config.postHogApiKey, config.postHogHost) posthog = PostHog.Builder(context, config.postHogApiKey, config.postHogHost)
// Record certain application events automatically! (off/false by default) // Record certain application events automatically! (off/false by default)
@ -135,12 +145,14 @@ class DefaultVectorAnalytics @Inject constructor(
} }
override fun capture(event: String, properties: Map<String, Any>?) { override fun capture(event: String, properties: Map<String, Any>?) {
Timber.tag(analyticsTag.value).d("capture($event)")
posthog posthog
?.takeIf { userConsent } ?.takeIf { userConsent }
?.capture(event, properties.toPostHogProperties()) ?.capture(event, properties.toPostHogProperties())
} }
override fun screen(name: String, properties: Map<String, Any>?) { override fun screen(name: String, properties: Map<String, Any>?) {
Timber.tag(analyticsTag.value).d("screen($name)")
posthog posthog
?.takeIf { userConsent } ?.takeIf { userConsent }
?.screen(name, properties.toPostHogProperties()) ?.screen(name, properties.toPostHogProperties())

View File

@ -0,0 +1,21 @@
/*
* Copyright (c) 2021 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package im.vector.app.features.analytics.log
import org.matrix.android.sdk.api.logger.LoggerTag
val analyticsTag = LoggerTag("Analytics")