From 24a6080090ccb4c6652a70fd1272fe20cf119dc3 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Wed, 24 Nov 2021 17:16:51 +0100 Subject: [PATCH] Analytics: Improve logs --- .../app/features/analytics/AnalyticsConfig.kt | 9 ++++++-- .../AnalyticsAccountDataViewModel.kt | 5 +++-- .../analytics/impl/DefaultVectorAnalytics.kt | 14 ++++++++++++- .../analytics/log/AnalyticsLoggerTag.kt | 21 +++++++++++++++++++ 4 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 vector/src/main/java/im/vector/app/features/analytics/log/AnalyticsLoggerTag.kt diff --git a/vector/src/main/java/im/vector/app/features/analytics/AnalyticsConfig.kt b/vector/src/main/java/im/vector/app/features/analytics/AnalyticsConfig.kt index 9a8d2e627d..a9045dc325 100644 --- a/vector/src/main/java/im/vector/app/features/analytics/AnalyticsConfig.kt +++ b/vector/src/main/java/im/vector/app/features/analytics/AnalyticsConfig.kt @@ -17,6 +17,7 @@ package im.vector.app.features.analytics import im.vector.app.BuildConfig +import im.vector.app.features.analytics.log.analyticsTag import timber.log.Timber data class AnalyticsConfig( @@ -29,9 +30,13 @@ data class AnalyticsConfig( */ fun getConfig(): AnalyticsConfig? { 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() } - ?: 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( postHogHost = postHogHost, diff --git a/vector/src/main/java/im/vector/app/features/analytics/accountdata/AnalyticsAccountDataViewModel.kt b/vector/src/main/java/im/vector/app/features/analytics/accountdata/AnalyticsAccountDataViewModel.kt index 665a26ce22..89b12cd8c2 100644 --- a/vector/src/main/java/im/vector/app/features/analytics/accountdata/AnalyticsAccountDataViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/analytics/accountdata/AnalyticsAccountDataViewModel.kt @@ -28,6 +28,7 @@ import im.vector.app.core.platform.EmptyAction import im.vector.app.core.platform.EmptyViewEvents import im.vector.app.core.platform.VectorViewModel import im.vector.app.features.analytics.VectorAnalytics +import im.vector.app.features.analytics.log.analyticsTag import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.mapNotNull @@ -93,9 +94,9 @@ class AnalyticsAccountDataViewModel @AssistedInject constructor( if (analyticsAccountDataContent.id.isNullOrEmpty()) { // Probably consent revoked from Element Web // Ignore here - Timber.d("Consent revoked from Element Web?") + Timber.tag(analyticsTag.value).d("Consent revoked from Element Web?") } else { - Timber.d("AnalyticsId has been retrieved") + Timber.tag(analyticsTag.value).d("AnalyticsId has been retrieved") analytics.setAnalyticsId(analyticsAccountDataContent.id) } } 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 a4f2ce2ca3..3ebe65099c 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 @@ -22,6 +22,7 @@ import com.posthog.android.Properties import im.vector.app.BuildConfig import im.vector.app.features.analytics.AnalyticsConfig import im.vector.app.features.analytics.VectorAnalytics +import im.vector.app.features.analytics.log.analyticsTag import im.vector.app.features.analytics.store.AnalyticsStore import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.flow.Flow @@ -45,6 +46,7 @@ class DefaultVectorAnalytics @Inject constructor( } override suspend fun setUserConsent(userConsent: Boolean) { + Timber.tag(analyticsTag.value).d("setUserConsent($userConsent)") analyticsStore.setUserConsent(userConsent) } @@ -53,6 +55,7 @@ class DefaultVectorAnalytics @Inject constructor( } override suspend fun setDidAskUserConsent() { + Timber.tag(analyticsTag.value).d("setDidAskUserConsent()") analyticsStore.setDidAskUserConsent() } @@ -61,6 +64,7 @@ class DefaultVectorAnalytics @Inject constructor( } override suspend fun setAnalyticsId(analyticsId: String) { + Timber.tag(analyticsTag.value).d("setAnalyticsId($analyticsId)") analyticsStore.setAnalyticsId(analyticsId) } @@ -78,9 +82,12 @@ class DefaultVectorAnalytics @Inject constructor( private fun observeAnalyticsId() { getAnalyticsId() .onEach { id -> + Timber.tag(analyticsTag.value).d("Analytics Id updated to '$id'") if (id.isEmpty()) { + Timber.tag(analyticsTag.value).d("reset") posthog?.reset() } else { + Timber.tag(analyticsTag.value).d("identify") posthog?.identify(id) } } @@ -91,6 +98,7 @@ class DefaultVectorAnalytics @Inject constructor( private fun observeUserConsent() { getUserConsent() .onEach { consent -> + Timber.tag(analyticsTag.value).d("User consent updated to $consent") userConsent = consent if (consent) { createAnalyticsClient() @@ -101,8 +109,10 @@ class DefaultVectorAnalytics @Inject constructor( } private fun createAnalyticsClient() { + Timber.tag(analyticsTag.value).d("createAnalyticsClient()") + 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) // Record certain application events automatically! (off/false by default) @@ -135,12 +145,14 @@ class DefaultVectorAnalytics @Inject constructor( } override fun capture(event: String, properties: Map?) { + Timber.tag(analyticsTag.value).d("capture($event)") posthog ?.takeIf { userConsent } ?.capture(event, properties.toPostHogProperties()) } override fun screen(name: String, properties: Map?) { + Timber.tag(analyticsTag.value).d("screen($name)") posthog ?.takeIf { userConsent } ?.screen(name, properties.toPostHogProperties()) diff --git a/vector/src/main/java/im/vector/app/features/analytics/log/AnalyticsLoggerTag.kt b/vector/src/main/java/im/vector/app/features/analytics/log/AnalyticsLoggerTag.kt new file mode 100644 index 0000000000..360740b9ce --- /dev/null +++ b/vector/src/main/java/im/vector/app/features/analytics/log/AnalyticsLoggerTag.kt @@ -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")