Analytics: add PostHog library

This commit is contained in:
Benoit Marty 2021-11-23 15:34:04 +01:00 committed by Benoit Marty
parent 8608230fa0
commit b33cddf0e3
5 changed files with 54 additions and 0 deletions

View File

@ -462,6 +462,9 @@ dependencies {
implementation libs.dagger.hilt implementation libs.dagger.hilt
kapt libs.dagger.hiltCompiler kapt libs.dagger.hiltCompiler
// Analytics
implementation 'com.posthog.android:posthog:1.1.2'
// gplay flavor only // gplay flavor only
gplayImplementation('com.google.firebase:firebase-messaging:23.0.0') { gplayImplementation('com.google.firebase:firebase-messaging:23.0.0') {
exclude group: 'com.google.firebase', module: 'firebase-core' exclude group: 'com.google.firebase', module: 'firebase-core'

View File

@ -262,6 +262,15 @@ SOFTWARE.
</li> </li>
</ul> </ul>
<ul>
<li>
<b>posthog-android</b>
<br/>
https://github.com/PostHog/posthog-android
PostHog Android integration is licensed under the MIT License
</li>
</ul>
<h3> <h3>
Apache License Apache License
<br/> <br/>

View File

@ -42,6 +42,7 @@ import dagger.hilt.android.HiltAndroidApp
import im.vector.app.core.di.ActiveSessionHolder import im.vector.app.core.di.ActiveSessionHolder
import im.vector.app.core.extensions.configureAndStart import im.vector.app.core.extensions.configureAndStart
import im.vector.app.core.extensions.startSyncing 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.call.webrtc.WebRtcCallManager
import im.vector.app.features.configuration.VectorConfiguration import im.vector.app.features.configuration.VectorConfiguration
import im.vector.app.features.disclaimer.doNotShowDisclaimerDialog import im.vector.app.features.disclaimer.doNotShowDisclaimerDialog
@ -96,6 +97,7 @@ class VectorApplication :
@Inject lateinit var callManager: WebRtcCallManager @Inject lateinit var callManager: WebRtcCallManager
@Inject lateinit var invitesAcceptor: InvitesAcceptor @Inject lateinit var invitesAcceptor: InvitesAcceptor
@Inject lateinit var vectorFileLogger: VectorFileLogger @Inject lateinit var vectorFileLogger: VectorFileLogger
@Inject lateinit var vectorAnalytics: VectorAnalytics
// font thread handler // font thread handler
private var fontThreadHandler: Handler? = null private var fontThreadHandler: Handler? = null
@ -113,6 +115,7 @@ class VectorApplication :
enableStrictModeIfNeeded() enableStrictModeIfNeeded()
super.onCreate() super.onCreate()
appContext = this appContext = this
vectorAnalytics.init()
invitesAcceptor.initialize() invitesAcceptor.initialize()
vectorUncaughtExceptionHandler.activate(this) vectorUncaughtExceptionHandler.activate(this)

View File

@ -53,4 +53,9 @@ interface VectorAnalytics {
* To be called when a session is destroyed * To be called when a session is destroyed
*/ */
suspend fun onSignOut() suspend fun onSignOut()
/**
* To be called when application is started
*/
fun init()
} }

View File

@ -16,14 +16,23 @@
package im.vector.app.features.analytics.impl 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.VectorAnalytics
import im.vector.app.features.analytics.store.AnalyticsStore import im.vector.app.features.analytics.store.AnalyticsStore
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import timber.log.Timber
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton
@Singleton
class DefaultVectorAnalytics @Inject constructor( class DefaultVectorAnalytics @Inject constructor(
private val context: Context,
private val analyticsStore: AnalyticsStore private val analyticsStore: AnalyticsStore
) : VectorAnalytics { ) : VectorAnalytics {
private var posthog: PostHog? = null
override fun getUserConsent(): Flow<Boolean> { override fun getUserConsent(): Flow<Boolean> {
return analyticsStore.userConsentFlow return analyticsStore.userConsentFlow
} }
@ -52,4 +61,29 @@ class DefaultVectorAnalytics @Inject constructor(
// reset the analyticsId // reset the analyticsId
setAnalyticsId("") 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()
}
} }