From b33cddf0e366ae58146871bd9a5449c9fbaabc14 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Tue, 23 Nov 2021 15:34:04 +0100 Subject: [PATCH] Analytics: add PostHog library --- vector/build.gradle | 3 ++ .../src/main/assets/open_source_licenses.html | 9 +++++ .../java/im/vector/app/VectorApplication.kt | 3 ++ .../app/features/analytics/VectorAnalytics.kt | 5 +++ .../analytics/impl/DefaultVectorAnalytics.kt | 34 +++++++++++++++++++ 5 files changed, 54 insertions(+) diff --git a/vector/build.gradle b/vector/build.gradle index 1b6e81848d..201be09cf6 100644 --- a/vector/build.gradle +++ b/vector/build.gradle @@ -462,6 +462,9 @@ dependencies { implementation libs.dagger.hilt kapt libs.dagger.hiltCompiler + // Analytics + implementation 'com.posthog.android:posthog:1.1.2' + // gplay flavor only gplayImplementation('com.google.firebase:firebase-messaging:23.0.0') { exclude group: 'com.google.firebase', module: 'firebase-core' diff --git a/vector/src/main/assets/open_source_licenses.html b/vector/src/main/assets/open_source_licenses.html index 529b7da2f1..29537fc40f 100755 --- a/vector/src/main/assets/open_source_licenses.html +++ b/vector/src/main/assets/open_source_licenses.html @@ -262,6 +262,15 @@ SOFTWARE. + +

Apache License
diff --git a/vector/src/main/java/im/vector/app/VectorApplication.kt b/vector/src/main/java/im/vector/app/VectorApplication.kt index 46f155a8d9..5401c23bf1 100644 --- a/vector/src/main/java/im/vector/app/VectorApplication.kt +++ b/vector/src/main/java/im/vector/app/VectorApplication.kt @@ -42,6 +42,7 @@ import dagger.hilt.android.HiltAndroidApp import im.vector.app.core.di.ActiveSessionHolder import im.vector.app.core.extensions.configureAndStart import im.vector.app.core.extensions.startSyncing +import im.vector.app.features.analytics.VectorAnalytics import im.vector.app.features.call.webrtc.WebRtcCallManager import im.vector.app.features.configuration.VectorConfiguration import im.vector.app.features.disclaimer.doNotShowDisclaimerDialog @@ -96,6 +97,7 @@ class VectorApplication : @Inject lateinit var callManager: WebRtcCallManager @Inject lateinit var invitesAcceptor: InvitesAcceptor @Inject lateinit var vectorFileLogger: VectorFileLogger + @Inject lateinit var vectorAnalytics: VectorAnalytics // font thread handler private var fontThreadHandler: Handler? = null @@ -113,6 +115,7 @@ class VectorApplication : enableStrictModeIfNeeded() super.onCreate() appContext = this + vectorAnalytics.init() invitesAcceptor.initialize() vectorUncaughtExceptionHandler.activate(this) diff --git a/vector/src/main/java/im/vector/app/features/analytics/VectorAnalytics.kt b/vector/src/main/java/im/vector/app/features/analytics/VectorAnalytics.kt index 55e6147461..538508b0de 100644 --- a/vector/src/main/java/im/vector/app/features/analytics/VectorAnalytics.kt +++ b/vector/src/main/java/im/vector/app/features/analytics/VectorAnalytics.kt @@ -53,4 +53,9 @@ interface VectorAnalytics { * To be called when a session is destroyed */ suspend fun onSignOut() + + /** + * To be called when application is started + */ + fun init() } 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 7eca77c74f..1d3447efbb 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 @@ -16,14 +16,23 @@ package im.vector.app.features.analytics.impl +import android.content.Context +import com.posthog.android.PostHog +import im.vector.app.features.analytics.AnalyticsConfig import im.vector.app.features.analytics.VectorAnalytics import im.vector.app.features.analytics.store.AnalyticsStore import kotlinx.coroutines.flow.Flow +import timber.log.Timber import javax.inject.Inject +import javax.inject.Singleton +@Singleton class DefaultVectorAnalytics @Inject constructor( + private val context: Context, private val analyticsStore: AnalyticsStore ) : VectorAnalytics { + private var posthog: PostHog? = null + override fun getUserConsent(): Flow { return analyticsStore.userConsentFlow } @@ -52,4 +61,29 @@ class DefaultVectorAnalytics @Inject constructor( // reset the analyticsId setAnalyticsId("") } + + override fun init() { + val config: AnalyticsConfig = AnalyticsConfig.getConfig() + ?: return Unit.also { Timber.w("Analytics is disabled") } + + posthog = PostHog.Builder(context, config.postHogApiKey, config.postHogHost) + // Record certain application events automatically! (off/false by default) + // .captureApplicationLifecycleEvents() + + // Record screen views automatically! (off/false by default) + // .recordScreenViews() + + // Capture deep links as part of the screen call. (off by default) + // .captureDeepLinks() + + // Maximum number of events to keep in queue before flushing (20) + // .flushQueueSize(20) + + // Max delay before flushing the queue (30 seconds) + // .flushInterval(30, TimeUnit.SECONDS) + + // Enable or disable collection of ANDROID_ID (true) + .collectDeviceId(false) + .build() + } }