First Commit

This commit is contained in:
2025-12-18 16:28:50 +07:00
commit 8c3e4f491f
9974 changed files with 396488 additions and 0 deletions
+44
View File
@@ -0,0 +1,44 @@
import extension.setupDependencyInjection
import extension.testCommonDependencies
/*
* Copyright (c) 2025 Element Creations Ltd.
* Copyright 2023, 2024 New Vector Ltd.
*
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial.
* Please see LICENSE files in the repository root for full details.
*/
plugins {
id("io.element.android-compose-library")
id("kotlin-parcelize")
}
android {
namespace = "io.element.android.services.analytics.impl"
}
setupDependencyInjection()
dependencies {
implementation(projects.libraries.androidutils)
implementation(projects.libraries.core)
implementation(projects.libraries.architecture)
implementation(projects.libraries.designsystem)
implementation(projects.libraries.matrix.api)
implementation(projects.libraries.preferences.api)
implementation(projects.libraries.sessionStorage.api)
implementation(projects.services.appnavstate.api)
api(projects.services.analyticsproviders.api)
api(projects.services.analytics.api)
implementation(libs.androidx.datastore.preferences)
testCommonDependencies(libs)
testImplementation(projects.libraries.matrix.test)
testImplementation(projects.libraries.sessionStorage.test)
testImplementation(projects.services.analytics.test)
testImplementation(projects.services.analyticsproviders.test)
testImplementation(projects.services.appnavstate.test)
testImplementation(projects.services.toolbox.test)
}
@@ -0,0 +1,174 @@
/*
* Copyright (c) 2025 Element Creations Ltd.
* Copyright 2023-2025 New Vector Ltd.
*
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial.
* Please see LICENSE files in the repository root for full details.
*/
package io.element.android.services.analytics.impl
import dev.zacsweers.metro.AppScope
import dev.zacsweers.metro.ContributesBinding
import dev.zacsweers.metro.SingleIn
import dev.zacsweers.metro.binding
import im.vector.app.features.analytics.itf.VectorAnalyticsEvent
import im.vector.app.features.analytics.itf.VectorAnalyticsScreen
import im.vector.app.features.analytics.plan.SuperProperties
import im.vector.app.features.analytics.plan.UserProperties
import io.element.android.libraries.di.annotations.AppCoroutineScope
import io.element.android.libraries.sessionstorage.api.observer.SessionListener
import io.element.android.libraries.sessionstorage.api.observer.SessionObserver
import io.element.android.services.analytics.api.AnalyticsLongRunningTransaction
import io.element.android.services.analytics.api.AnalyticsService
import io.element.android.services.analytics.api.NoopAnalyticsTransaction
import io.element.android.services.analytics.impl.log.analyticsTag
import io.element.android.services.analytics.impl.store.AnalyticsStore
import io.element.android.services.analyticsproviders.api.AnalyticsProvider
import io.element.android.services.analyticsproviders.api.AnalyticsTransaction
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import timber.log.Timber
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.atomic.AtomicBoolean
@SingleIn(AppScope::class)
@ContributesBinding(AppScope::class, binding = binding<AnalyticsService>())
class DefaultAnalyticsService(
private val analyticsProviders: Set<@JvmSuppressWildcards AnalyticsProvider>,
private val analyticsStore: AnalyticsStore,
// private val lateInitUserPropertiesFactory: LateInitUserPropertiesFactory,
@AppCoroutineScope
private val coroutineScope: CoroutineScope,
private val sessionObserver: SessionObserver,
) : AnalyticsService, SessionListener {
private val pendingLongRunningTransactions = ConcurrentHashMap<AnalyticsLongRunningTransaction, AnalyticsTransaction>()
// Cache for the store values
private val userConsent = AtomicBoolean(false)
// Cache for the properties to send
private var pendingUserProperties: UserProperties? = null
override val userConsentFlow: Flow<Boolean> = analyticsStore.userConsentFlow
override val didAskUserConsentFlow: Flow<Boolean> = analyticsStore.didAskUserConsentFlow
override val analyticsIdFlow: Flow<String> = analyticsStore.analyticsIdFlow
init {
observeUserConsent()
observeSessions()
}
override fun getAvailableAnalyticsProviders(): Set<AnalyticsProvider> {
return analyticsProviders
}
override suspend fun setUserConsent(userConsent: Boolean) {
Timber.tag(analyticsTag.value).d("setUserConsent($userConsent)")
analyticsStore.setUserConsent(userConsent)
}
override suspend fun setDidAskUserConsent() {
Timber.tag(analyticsTag.value).d("setDidAskUserConsent()")
analyticsStore.setDidAskUserConsent()
}
override suspend fun setAnalyticsId(analyticsId: String) {
Timber.tag(analyticsTag.value).d("setAnalyticsId($analyticsId)")
analyticsStore.setAnalyticsId(analyticsId)
}
override suspend fun onSessionDeleted(userId: String, wasLastSession: Boolean) {
// Delete the store when the last session is deleted
if (wasLastSession) {
analyticsStore.reset()
}
}
private fun observeUserConsent() {
userConsentFlow
.onEach { consent ->
Timber.tag(analyticsTag.value).d("User consent updated to $consent")
userConsent.set(consent)
initOrStop()
}
.launchIn(coroutineScope)
}
private fun observeSessions() {
sessionObserver.addListener(this)
}
private fun initOrStop() {
if (userConsent.get()) {
analyticsProviders.onEach { it.init() }
pendingUserProperties?.let {
analyticsProviders.onEach { provider -> provider.updateUserProperties(it) }
pendingUserProperties = null
}
} else {
analyticsProviders.onEach { it.stop() }
}
}
override fun capture(event: VectorAnalyticsEvent) {
Timber.tag(analyticsTag.value).d("capture($event)")
if (userConsent.get()) {
analyticsProviders.onEach { it.capture(event) }
}
}
override fun screen(screen: VectorAnalyticsScreen) {
Timber.tag(analyticsTag.value).d("screen($screen)")
if (userConsent.get()) {
analyticsProviders.onEach { it.screen(screen) }
}
}
override fun updateUserProperties(userProperties: UserProperties) {
if (userConsent.get()) {
analyticsProviders.onEach { it.updateUserProperties(userProperties) }
} else {
pendingUserProperties = userProperties
}
}
override fun updateSuperProperties(updatedProperties: SuperProperties) {
this.analyticsProviders.onEach { it.updateSuperProperties(updatedProperties) }
}
override fun trackError(throwable: Throwable) {
if (userConsent.get()) {
analyticsProviders.onEach { it.trackError(throwable) }
}
}
override fun startTransaction(name: String, operation: String?): AnalyticsTransaction {
return if (userConsent.get()) {
analyticsProviders.firstNotNullOfOrNull { it.startTransaction(name, operation) }
} else {
null
} ?: NoopAnalyticsTransaction
}
override fun startLongRunningTransaction(
longRunningTransaction: AnalyticsLongRunningTransaction,
parentTransaction: AnalyticsTransaction?,
): AnalyticsTransaction {
val transaction = parentTransaction?.startChild(longRunningTransaction.name, longRunningTransaction.operation)
?: startTransaction(longRunningTransaction.name, longRunningTransaction.operation)
pendingLongRunningTransactions[longRunningTransaction] = transaction
return transaction
}
override fun getLongRunningTransaction(longRunningTransaction: AnalyticsLongRunningTransaction): AnalyticsTransaction? {
return pendingLongRunningTransactions[longRunningTransaction]
}
override fun removeLongRunningTransaction(longRunningTransaction: AnalyticsLongRunningTransaction): AnalyticsTransaction? {
return pendingLongRunningTransactions.remove(longRunningTransaction)
}
}
@@ -0,0 +1,50 @@
/*
* Copyright (c) 2025 Element Creations Ltd.
* Copyright 2024, 2025 New Vector Ltd.
*
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial.
* Please see LICENSE files in the repository root for full details.
*/
package io.element.android.services.analytics.impl
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableLongStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.lifecycle.Lifecycle
import dev.zacsweers.metro.AppScope
import dev.zacsweers.metro.ContributesBinding
import im.vector.app.features.analytics.plan.MobileScreen
import io.element.android.libraries.designsystem.utils.OnLifecycleEvent
import io.element.android.services.analytics.api.AnalyticsService
import io.element.android.services.analytics.api.ScreenTracker
import io.element.android.services.toolbox.api.systemclock.SystemClock
@ContributesBinding(AppScope::class)
class DefaultScreenTracker(
private val analyticsService: AnalyticsService,
private val systemClock: SystemClock,
) : ScreenTracker {
@Composable
override fun TrackScreen(
screen: MobileScreen.ScreenName,
) {
var startTime by remember { mutableLongStateOf(0L) }
OnLifecycleEvent { _, event ->
when (event) {
Lifecycle.Event.ON_RESUME -> {
startTime = systemClock.epochMillis()
}
Lifecycle.Event.ON_PAUSE -> analyticsService.screen(
screen = MobileScreen(
durationMs = (systemClock.epochMillis() - startTime).toInt(),
screenName = screen
)
)
else -> Unit
}
}
}
}
@@ -0,0 +1,13 @@
/*
* Copyright (c) 2025 Element Creations Ltd.
* Copyright 2021-2025 New Vector Ltd.
*
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial.
* Please see LICENSE files in the repository root for full details.
*/
package io.element.android.services.analytics.impl.log
import io.element.android.libraries.core.log.logger.LoggerTag
val analyticsTag = LoggerTag("Analytics")
@@ -0,0 +1,83 @@
/*
* Copyright (c) 2025 Element Creations Ltd.
* Copyright 2021-2025 New Vector Ltd.
*
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial.
* Please see LICENSE files in the repository root for full details.
*/
package io.element.android.services.analytics.impl.store
import androidx.datastore.preferences.core.booleanPreferencesKey
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.stringPreferencesKey
import dev.zacsweers.metro.AppScope
import dev.zacsweers.metro.ContributesBinding
import io.element.android.libraries.core.bool.orFalse
import io.element.android.libraries.preferences.api.store.PreferenceDataStoreFactory
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.map
/**
* Local storage for:
* - user consent (Boolean);
* - did ask user consent (Boolean);
* - analytics Id (String).
*/
interface AnalyticsStore {
val userConsentFlow: Flow<Boolean>
val didAskUserConsentFlow: Flow<Boolean>
val analyticsIdFlow: Flow<String>
suspend fun setUserConsent(newUserConsent: Boolean)
suspend fun setDidAskUserConsent(newValue: Boolean = true)
suspend fun setAnalyticsId(newAnalyticsId: String)
suspend fun reset()
}
@ContributesBinding(AppScope::class)
class DefaultAnalyticsStore(
preferenceDataStoreFactory: PreferenceDataStoreFactory,
) : AnalyticsStore {
private val userConsent = booleanPreferencesKey("user_consent")
private val didAskUserConsent = booleanPreferencesKey("did_ask_user_consent")
private val analyticsId = stringPreferencesKey("analytics_id")
private val dataStore = preferenceDataStoreFactory.create("vector_analytics")
override val userConsentFlow: Flow<Boolean> = dataStore.data
.map { preferences -> preferences[userConsent].orFalse() }
.distinctUntilChanged()
override val didAskUserConsentFlow: Flow<Boolean> = dataStore.data
.map { preferences -> preferences[didAskUserConsent].orFalse() }
.distinctUntilChanged()
override val analyticsIdFlow: Flow<String> = dataStore.data
.map { preferences -> preferences[analyticsId].orEmpty() }
.distinctUntilChanged()
override suspend fun setUserConsent(newUserConsent: Boolean) {
dataStore.edit { settings ->
settings[userConsent] = newUserConsent
}
}
override suspend fun setDidAskUserConsent(newValue: Boolean) {
dataStore.edit { settings ->
settings[didAskUserConsent] = newValue
}
}
override suspend fun setAnalyticsId(newAnalyticsId: String) {
dataStore.edit { settings ->
settings[analyticsId] = newAnalyticsId
}
}
override suspend fun reset() {
dataStore.edit {
it.clear()
}
}
}
@@ -0,0 +1,63 @@
/*
* Copyright (c) 2025 Element Creations Ltd.
*
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial.
* Please see LICENSE files in the repository root for full details.
*/
package io.element.android.services.analytics.impl.watchers
import dev.zacsweers.metro.AppScope
import dev.zacsweers.metro.ContributesBinding
import dev.zacsweers.metro.SingleIn
import io.element.android.libraries.di.annotations.AppCoroutineScope
import io.element.android.services.analytics.api.AnalyticsLongRunningTransaction
import io.element.android.services.analytics.api.AnalyticsService
import io.element.android.services.analytics.api.cancelLongRunningTransaction
import io.element.android.services.analytics.api.finishLongRunningTransaction
import io.element.android.services.analytics.api.watchers.AnalyticsColdStartWatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import timber.log.Timber
import java.util.concurrent.atomic.AtomicBoolean
@SingleIn(AppScope::class)
@ContributesBinding(AppScope::class)
class DefaultAnalyticsColdStartWatcher(
private val analyticsService: AnalyticsService,
@AppCoroutineScope private val appCoroutineScope: CoroutineScope,
) : AnalyticsColdStartWatcher {
private val isColdStart = AtomicBoolean(true)
override fun start() {
analyticsService.userConsentFlow
.onEach { hasConsent ->
if (hasConsent) {
if (isColdStart.get()) {
Timber.d("Starting cold start check")
analyticsService.startLongRunningTransaction(AnalyticsLongRunningTransaction.ColdStartUntilCachedRoomList)
} else {
error("The app is no longer in a cold start state")
}
}
}
.catch { Timber.w(it.message) }
.launchIn(appCoroutineScope)
}
override fun whenLoggingIn() {
if (isColdStart.getAndSet(false)) {
analyticsService.cancelLongRunningTransaction(AnalyticsLongRunningTransaction.ColdStartUntilCachedRoomList)
Timber.d("Canceled cold start check: user is logging in")
}
}
override fun onRoomListVisible() {
if (isColdStart.getAndSet(false)) {
Timber.d("Room list is visible, finishing cold start check")
analyticsService.finishLongRunningTransaction(AnalyticsLongRunningTransaction.ColdStartUntilCachedRoomList)
}
}
}
@@ -0,0 +1,78 @@
/*
* Copyright (c) 2025 Element Creations Ltd.
*
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial.
* Please see LICENSE files in the repository root for full details.
*/
package io.element.android.services.analytics.impl.watchers
import dev.zacsweers.metro.ContributesBinding
import io.element.android.libraries.core.coroutine.CoroutineDispatchers
import io.element.android.libraries.core.coroutine.childScope
import io.element.android.libraries.core.coroutine.withPreviousValue
import io.element.android.libraries.di.SessionScope
import io.element.android.libraries.di.annotations.SessionCoroutineScope
import io.element.android.libraries.matrix.api.roomlist.RoomListService
import io.element.android.services.analytics.api.AnalyticsLongRunningTransaction
import io.element.android.services.analytics.api.AnalyticsService
import io.element.android.services.analytics.api.finishLongRunningTransaction
import io.element.android.services.analytics.api.watchers.AnalyticsRoomListStateWatcher
import io.element.android.services.appnavstate.api.AppNavigationStateService
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.cancelChildren
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onEach
import timber.log.Timber
import java.util.concurrent.atomic.AtomicBoolean
@ContributesBinding(SessionScope::class)
class DefaultAnalyticsRoomListStateWatcher(
private val appNavigationStateService: AppNavigationStateService,
private val roomListService: RoomListService,
private val analyticsService: AnalyticsService,
@SessionCoroutineScope sessionCoroutineScope: CoroutineScope,
dispatchers: CoroutineDispatchers,
) : AnalyticsRoomListStateWatcher {
private val coroutineScope: CoroutineScope = sessionCoroutineScope.childScope(dispatchers.computation, "AnalyticsRoomListStateWatcher")
private val isStarted = AtomicBoolean(false)
private val isWarmState = AtomicBoolean(false)
override fun start() {
if (isStarted.getAndSet(true)) {
Timber.w("Can't start RoomListStateWatcher, it's already running.")
return
}
appNavigationStateService.appNavigationState
.map { it.isInForeground }
.distinctUntilChanged()
.withPreviousValue()
.onEach { (wasInForeground, isInForeground) ->
if (isInForeground && roomListService.state.value != RoomListService.State.Running) {
analyticsService.startLongRunningTransaction(AnalyticsLongRunningTransaction.ResumeAppUntilNewRoomsReceived)
}
if (wasInForeground == false && isInForeground) {
isWarmState.set(true)
}
}
.launchIn(coroutineScope)
roomListService.state
.onEach { state ->
if (state == RoomListService.State.Running && isWarmState.get()) {
analyticsService.finishLongRunningTransaction(AnalyticsLongRunningTransaction.ResumeAppUntilNewRoomsReceived)
}
}
.launchIn(coroutineScope)
}
override fun stop() {
if (isStarted.getAndSet(false)) {
coroutineScope.coroutineContext.cancelChildren()
}
}
}
@@ -0,0 +1,300 @@
/*
* Copyright (c) 2025 Element Creations Ltd.
* Copyright 2024, 2025 New Vector Ltd.
*
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial.
* Please see LICENSE files in the repository root for full details.
*/
@file:OptIn(ExperimentalCoroutinesApi::class)
package io.element.android.services.analytics.impl
import com.google.common.truth.Truth.assertThat
import im.vector.app.features.analytics.itf.VectorAnalyticsEvent
import im.vector.app.features.analytics.itf.VectorAnalyticsScreen
import im.vector.app.features.analytics.plan.MobileScreen
import im.vector.app.features.analytics.plan.PollEnd
import im.vector.app.features.analytics.plan.SuperProperties
import im.vector.app.features.analytics.plan.UserProperties
import io.element.android.libraries.sessionstorage.api.observer.SessionObserver
import io.element.android.libraries.sessionstorage.test.observer.NoOpSessionObserver
import io.element.android.services.analytics.impl.store.AnalyticsStore
import io.element.android.services.analytics.impl.store.FakeAnalyticsStore
import io.element.android.services.analyticsproviders.api.AnalyticsProvider
import io.element.android.services.analyticsproviders.test.FakeAnalyticsProvider
import io.element.android.tests.testutils.lambda.lambdaRecorder
import io.element.android.tests.testutils.lambda.value
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.cancel
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
import kotlinx.coroutines.test.runTest
import org.junit.Test
class DefaultAnalyticsServiceTest {
@Test
fun `getAvailableAnalyticsProviders return the set of provider`() = runTest {
val providers = setOf(
FakeAnalyticsProvider(name = "provider1", stopLambda = { }),
FakeAnalyticsProvider(name = "provider2", stopLambda = { }),
)
val sut = createDefaultAnalyticsService(
coroutineScope = backgroundScope,
analyticsProviders = providers
)
val result = sut.getAvailableAnalyticsProviders()
assertThat(result).isEqualTo(providers)
}
@Test
fun `when consent is not provided, capture is no op`() = runTest {
val sut = createDefaultAnalyticsService(backgroundScope)
sut.capture(anEvent)
}
@Test
fun `when consent is provided, capture is sent to the AnalyticsProvider`() = runTest {
val initLambda = lambdaRecorder<Unit> { }
val captureLambda = lambdaRecorder<VectorAnalyticsEvent, Unit> { _ -> }
val sut = createDefaultAnalyticsService(
coroutineScope = backgroundScope,
analyticsStore = FakeAnalyticsStore(defaultUserConsent = true),
analyticsProviders = setOf(
FakeAnalyticsProvider(
initLambda = initLambda,
captureLambda = captureLambda,
)
)
)
initLambda.assertions().isCalledOnce()
sut.capture(anEvent)
captureLambda.assertions().isCalledOnce().with(value(anEvent))
}
@Test
fun `when consent is not provided, screen is no op`() = runTest {
val sut = createDefaultAnalyticsService(backgroundScope)
sut.screen(aScreen)
}
@Test
fun `when consent is provided, screen is sent to the AnalyticsProvider`() = runTest {
val initLambda = lambdaRecorder<Unit> { }
val screenLambda = lambdaRecorder<VectorAnalyticsScreen, Unit> { _ -> }
val sut = createDefaultAnalyticsService(
coroutineScope = backgroundScope,
analyticsStore = FakeAnalyticsStore(defaultUserConsent = true),
analyticsProviders = setOf(
FakeAnalyticsProvider(
initLambda = initLambda,
screenLambda = screenLambda,
)
)
)
initLambda.assertions().isCalledOnce()
sut.screen(aScreen)
screenLambda.assertions().isCalledOnce().with(value(aScreen))
}
@Test
fun `when consent is not provided, trackError is no op`() = runTest {
val sut = createDefaultAnalyticsService(backgroundScope)
sut.trackError(anError)
}
@Test
fun `when consent is provided, trackError is sent to the AnalyticsProvider`() = runTest {
val initLambda = lambdaRecorder<Unit> { }
val trackErrorLambda = lambdaRecorder<Throwable, Unit> { _ -> }
val sut = createDefaultAnalyticsService(
coroutineScope = backgroundScope,
analyticsStore = FakeAnalyticsStore(defaultUserConsent = true),
analyticsProviders = setOf(
FakeAnalyticsProvider(
initLambda = initLambda,
trackErrorLambda = trackErrorLambda,
)
)
)
initLambda.assertions().isCalledOnce()
sut.trackError(anError)
trackErrorLambda.assertions().isCalledOnce().with(value(anError))
}
@Test
fun `setUserConsent is sent to the store`() = runTest {
val store = FakeAnalyticsStore()
val sut = createDefaultAnalyticsService(
coroutineScope = backgroundScope,
analyticsStore = store,
)
assertThat(store.userConsentFlow.first()).isFalse()
assertThat(sut.userConsentFlow.first()).isFalse()
sut.setUserConsent(true)
assertThat(store.userConsentFlow.first()).isTrue()
assertThat(sut.userConsentFlow.first()).isTrue()
}
@Test
fun `setAnalyticsId is sent to the store`() = runTest {
val store = FakeAnalyticsStore()
val sut = createDefaultAnalyticsService(
coroutineScope = backgroundScope,
analyticsStore = store,
)
assertThat(store.analyticsIdFlow.first()).isEqualTo("")
assertThat(sut.analyticsIdFlow.first()).isEqualTo("")
sut.setAnalyticsId(AN_ID)
assertThat(store.analyticsIdFlow.first()).isEqualTo(AN_ID)
assertThat(sut.analyticsIdFlow.first()).isEqualTo(AN_ID)
}
@Test
fun `setDidAskUserConsent is sent to the store`() = runTest {
val store = FakeAnalyticsStore()
val sut = createDefaultAnalyticsService(
coroutineScope = backgroundScope,
analyticsStore = store,
)
assertThat(store.didAskUserConsentFlow.first()).isFalse()
assertThat(sut.didAskUserConsentFlow.first()).isFalse()
sut.setDidAskUserConsent()
assertThat(store.didAskUserConsentFlow.first()).isTrue()
assertThat(sut.didAskUserConsentFlow.first()).isTrue()
}
@Test
fun `when the last session is deleted, the store is reset`() = runTest {
val resetLambda = lambdaRecorder<Unit> { }
val store = FakeAnalyticsStore(
resetLambda = resetLambda,
)
val sut = createDefaultAnalyticsService(
coroutineScope = backgroundScope,
analyticsStore = store,
)
sut.onSessionDeleted("userId", true)
resetLambda.assertions().isCalledOnce()
}
@Test
fun `when a session is deleted, the store is not reset if it was not the last session`() = runTest {
val resetLambda = lambdaRecorder<Unit> { }
val store = FakeAnalyticsStore(
resetLambda = resetLambda,
)
val sut = createDefaultAnalyticsService(
coroutineScope = backgroundScope,
analyticsStore = store,
)
sut.onSessionDeleted("userId", false)
resetLambda.assertions().isNeverCalled()
}
@Test
fun `when a session is added, nothing happen`() = runTest {
val sut = createDefaultAnalyticsService(
coroutineScope = backgroundScope,
)
sut.onSessionCreated("userId")
}
@Test
fun `when consent is not provided, updateUserProperties is stored for future use`() = runTest {
val completable = CompletableDeferred<Unit>()
val updateUserPropertiesLambda = lambdaRecorder<UserProperties, Unit> { _ ->
completable.complete(Unit)
}
launch {
val sut = createDefaultAnalyticsService(
coroutineScope = this,
analyticsProviders = setOf(
FakeAnalyticsProvider(
initLambda = { },
stopLambda = { },
updateUserPropertiesLambda = updateUserPropertiesLambda,
)
)
)
sut.updateUserProperties(aUserProperty)
updateUserPropertiesLambda.assertions().isNeverCalled()
// Give user consent
sut.setUserConsent(true)
completable.await()
updateUserPropertiesLambda.assertions().isCalledOnce().with(value(aUserProperty))
cancel()
}
}
@Test
fun `when consent is provided, updateUserProperties is sent to the provider`() = runTest {
val updateUserPropertiesLambda = lambdaRecorder<UserProperties, Unit> { _ -> }
val sut = createDefaultAnalyticsService(
coroutineScope = backgroundScope,
analyticsProviders = setOf(
FakeAnalyticsProvider(
initLambda = { },
updateUserPropertiesLambda = updateUserPropertiesLambda,
)
),
analyticsStore = FakeAnalyticsStore(defaultUserConsent = true),
)
sut.updateUserProperties(aUserProperty)
updateUserPropertiesLambda.assertions().isCalledOnce().with(value(aUserProperty))
}
@Test
fun `when super properties are updated, updateSuperProperties is sent to the provider`() = runTest {
val updateSuperPropertiesLambda = lambdaRecorder<SuperProperties, Unit> { _ -> }
val sut = createDefaultAnalyticsService(
coroutineScope = backgroundScope,
analyticsProviders = setOf(
FakeAnalyticsProvider(
initLambda = { },
updateSuperPropertiesLambda = updateSuperPropertiesLambda,
)
),
analyticsStore = FakeAnalyticsStore(defaultUserConsent = true),
)
sut.updateSuperProperties(aSuperProperty)
updateSuperPropertiesLambda.assertions().isCalledOnce().with(value(aSuperProperty))
}
private suspend fun createDefaultAnalyticsService(
coroutineScope: CoroutineScope,
analyticsProviders: Set<@JvmSuppressWildcards AnalyticsProvider> = setOf(
FakeAnalyticsProvider(
stopLambda = { },
)
),
analyticsStore: AnalyticsStore = FakeAnalyticsStore(),
sessionObserver: SessionObserver = NoOpSessionObserver(),
) = DefaultAnalyticsService(
analyticsProviders = analyticsProviders,
analyticsStore = analyticsStore,
coroutineScope = coroutineScope,
sessionObserver = sessionObserver,
).also {
// Wait for the service to be ready
delay(1)
}
private companion object {
private val anEvent = PollEnd()
private val aScreen = MobileScreen(screenName = MobileScreen.ScreenName.User)
private val aUserProperty = UserProperties(
ftueUseCaseSelection = UserProperties.FtueUseCaseSelection.WorkMessaging,
)
private val aSuperProperty = SuperProperties(
appPlatform = SuperProperties.AppPlatform.EXA,
cryptoSDK = SuperProperties.CryptoSDK.Rust,
cryptoSDKVersion = "0.0"
)
private val anError = Exception("a reason")
private const val AN_ID = "anId"
}
}
@@ -0,0 +1,62 @@
/*
* Copyright (c) 2025 Element Creations Ltd.
* Copyright 2025 New Vector Ltd.
*
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial.
* Please see LICENSE files in the repository root for full details.
*/
package io.element.android.services.analytics.impl
import androidx.lifecycle.Lifecycle
import app.cash.molecule.RecompositionMode
import app.cash.molecule.moleculeFlow
import app.cash.turbine.test
import com.google.common.truth.Truth.assertThat
import im.vector.app.features.analytics.plan.MobileScreen
import io.element.android.services.analytics.api.AnalyticsService
import io.element.android.services.analytics.test.FakeAnalyticsService
import io.element.android.services.toolbox.api.systemclock.SystemClock
import io.element.android.services.toolbox.test.systemclock.FakeSystemClock
import io.element.android.tests.testutils.FakeLifecycleOwner
import io.element.android.tests.testutils.withFakeLifecycleOwner
import kotlinx.coroutines.test.runTest
import org.junit.Test
class DefaultScreenTrackerTest {
@Test
fun `TrackScreen is working as expected`() = runTest {
val analyticsService = FakeAnalyticsService()
val systemClock = FakeSystemClock(150)
val lifecycleOwner = FakeLifecycleOwner()
val sut = createDefaultScreenTracker(
analyticsService = analyticsService,
systemClock = systemClock,
)
moleculeFlow(RecompositionMode.Immediate) {
withFakeLifecycleOwner(lifecycleOwner) {
sut.TrackScreen(MobileScreen.ScreenName.RoomMembers)
}
}.test {
// Screen resumes
lifecycleOwner.givenState(Lifecycle.State.RESUMED)
assertThat(awaitItem()).isEqualTo(Unit)
systemClock.epochMillisResult = 450
lifecycleOwner.givenState(Lifecycle.State.DESTROYED)
}
assertThat(analyticsService.screenEvents).containsExactly(
MobileScreen(
screenName = MobileScreen.ScreenName.RoomMembers,
durationMs = 300,
)
)
}
}
private fun createDefaultScreenTracker(
analyticsService: AnalyticsService = FakeAnalyticsService(),
systemClock: SystemClock = FakeSystemClock(),
) = DefaultScreenTracker(
analyticsService = analyticsService,
systemClock = systemClock,
)
@@ -0,0 +1,39 @@
/*
* Copyright (c) 2025 Element Creations Ltd.
* Copyright 2024, 2025 New Vector Ltd.
*
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial.
* Please see LICENSE files in the repository root for full details.
*/
package io.element.android.services.analytics.impl.store
import io.element.android.tests.testutils.lambda.lambdaError
import kotlinx.coroutines.flow.MutableStateFlow
internal class FakeAnalyticsStore(
defaultUserConsent: Boolean = false,
defaultDidAskUserConsent: Boolean = false,
defaultAnalyticsId: String = "",
private val resetLambda: () -> Unit = { lambdaError() },
) : AnalyticsStore {
override val userConsentFlow = MutableStateFlow(defaultUserConsent)
override val didAskUserConsentFlow = MutableStateFlow(defaultDidAskUserConsent)
override val analyticsIdFlow = MutableStateFlow(defaultAnalyticsId)
override suspend fun setUserConsent(newUserConsent: Boolean) {
userConsentFlow.emit(newUserConsent)
}
override suspend fun setDidAskUserConsent(newValue: Boolean) {
didAskUserConsentFlow.emit(newValue)
}
override suspend fun setAnalyticsId(newAnalyticsId: String) {
analyticsIdFlow.emit(newAnalyticsId)
}
override suspend fun reset() {
resetLambda()
}
}
@@ -0,0 +1,107 @@
/*
* Copyright (c) 2025 Element Creations Ltd.
*
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial.
* Please see LICENSE files in the repository root for full details.
*/
package io.element.android.services.analytics.impl.watchers
import com.google.common.truth.Truth.assertThat
import io.element.android.services.analytics.api.AnalyticsLongRunningTransaction.ColdStartUntilCachedRoomList
import io.element.android.services.analytics.test.FakeAnalyticsService
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.TestScope
import kotlinx.coroutines.test.runCurrent
import kotlinx.coroutines.test.runTest
import org.junit.Test
@OptIn(ExperimentalCoroutinesApi::class)
class DefaultAnalyticsColdStartWatcherTest {
@Test
fun `watch - until room list is visible`() = runTest {
val analyticsService = FakeAnalyticsService()
val watcher = createAnalyticsColdStartWatcher(analyticsService)
// Start watching
watcher.start()
// The user has given analytics consent, we can start tracking the cold start
analyticsService.setUserConsent(true)
runCurrent()
// The transaction is running
assertThat(analyticsService.getLongRunningTransaction(ColdStartUntilCachedRoomList)).isNotNull()
// As soon as the room list is visible
watcher.onRoomListVisible()
runCurrent()
// The transaction is now finished
assertThat(analyticsService.getLongRunningTransaction(ColdStartUntilCachedRoomList)).isNull()
}
@Test
fun `watch - user is logging in, transaction is cancelled since it's not a cold start`() = runTest {
val analyticsService = FakeAnalyticsService()
val watcher = createAnalyticsColdStartWatcher(analyticsService)
// Start watching
watcher.start()
// The user has given analytics consent, we can start tracking the cold start
analyticsService.setUserConsent(true)
runCurrent()
// The transaction is running
assertThat(analyticsService.getLongRunningTransaction(ColdStartUntilCachedRoomList)).isNotNull()
// If the user starts a login flow
watcher.whenLoggingIn()
runCurrent()
// The transaction is gone
assertThat(analyticsService.getLongRunningTransaction(ColdStartUntilCachedRoomList)).isNull()
}
@Test
fun `watch - user was logging in, transaction is never started since it's not a cold start`() = runTest {
val analyticsService = FakeAnalyticsService()
val watcher = createAnalyticsColdStartWatcher(analyticsService)
// Start watching
watcher.start()
// If the user starts a login flow
watcher.whenLoggingIn()
// The user has given analytics consent, we can start tracking the cold start
analyticsService.setUserConsent(true)
runCurrent()
// The transaction never starts
assertThat(analyticsService.getLongRunningTransaction(ColdStartUntilCachedRoomList)).isNull()
}
@Test
fun `watch - never gets consent so it does nothing`() = runTest {
val analyticsService = FakeAnalyticsService()
val watcher = createAnalyticsColdStartWatcher(analyticsService)
// Start watching
watcher.start()
// The user never gets the analytics consent, so we do nothing
runCurrent()
// The transaction is not running in that case
assertThat(analyticsService.getLongRunningTransaction(ColdStartUntilCachedRoomList)).isNull()
}
private fun TestScope.createAnalyticsColdStartWatcher(
analyticsService: FakeAnalyticsService = FakeAnalyticsService(),
) = DefaultAnalyticsColdStartWatcher(
analyticsService = analyticsService,
appCoroutineScope = backgroundScope,
)
}
@@ -0,0 +1,170 @@
/*
* Copyright (c) 2025 Element Creations Ltd.
*
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial.
* Please see LICENSE files in the repository root for full details.
*/
package io.element.android.services.analytics.impl.watchers
import com.google.common.truth.Truth.assertThat
import io.element.android.libraries.matrix.api.roomlist.RoomListService
import io.element.android.libraries.matrix.test.roomlist.FakeRoomListService
import io.element.android.services.analytics.api.AnalyticsLongRunningTransaction.ResumeAppUntilNewRoomsReceived
import io.element.android.services.analytics.test.FakeAnalyticsService
import io.element.android.services.appnavstate.api.AppNavigationState
import io.element.android.services.appnavstate.api.NavigationState
import io.element.android.services.appnavstate.test.FakeAppNavigationStateService
import io.element.android.tests.testutils.testCoroutineDispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.TestScope
import kotlinx.coroutines.test.runCurrent
import kotlinx.coroutines.test.runTest
import org.junit.Test
@OptIn(ExperimentalCoroutinesApi::class)
class DefaultAnalyticsRoomListStateWatcherTest {
@Test
fun `Opening the app in a warm state tracks the time until the room list is synced`() = runTest {
val navigationStateService = FakeAppNavigationStateService()
val roomListService = FakeRoomListService().apply {
postState(RoomListService.State.Idle)
}
val analyticsService = FakeAnalyticsService()
val watcher = createAnalyticsRoomListStateWatcher(
appNavigationStateService = navigationStateService,
roomListService = roomListService,
analyticsService = analyticsService,
)
watcher.start()
// Give some time to load the initial state
runCurrent()
// Make sure it's warm by changing its internal state
navigationStateService.appNavigationState.emit(AppNavigationState(navigationState = NavigationState.Root, isInForeground = false))
runCurrent()
navigationStateService.appNavigationState.emit(AppNavigationState(navigationState = NavigationState.Root, isInForeground = true))
runCurrent()
// The transaction should be present now
assertThat(analyticsService.getLongRunningTransaction(ResumeAppUntilNewRoomsReceived)).isNotNull()
// And now the room list service running
roomListService.postState(RoomListService.State.Running)
runCurrent()
// And the transaction should now be gone
assertThat(analyticsService.getLongRunningTransaction(ResumeAppUntilNewRoomsReceived)).isNull()
watcher.stop()
}
@Test
fun `Opening the app in a cold state does nothing`() = runTest {
val navigationStateService = FakeAppNavigationStateService().apply {
appNavigationState.emit(AppNavigationState(NavigationState.Root, false))
}
val roomListService = FakeRoomListService().apply {
postState(RoomListService.State.Idle)
}
val analyticsService = FakeAnalyticsService()
val watcher = createAnalyticsRoomListStateWatcher(
appNavigationStateService = navigationStateService,
roomListService = roomListService,
analyticsService = analyticsService,
)
watcher.start()
// Give some time to load the initial state
runCurrent()
// The room list service running
roomListService.postState(RoomListService.State.Running)
runCurrent()
// The transaction was never present
assertThat(analyticsService.getLongRunningTransaction(ResumeAppUntilNewRoomsReceived)).isNull()
watcher.stop()
}
@Test
fun `The transaction won't be finished until the room list is synchronised`() = runTest {
val navigationStateService = FakeAppNavigationStateService()
val roomListService = FakeRoomListService().apply {
postState(RoomListService.State.Idle)
}
val analyticsService = FakeAnalyticsService()
val watcher = createAnalyticsRoomListStateWatcher(
appNavigationStateService = navigationStateService,
roomListService = roomListService,
analyticsService = analyticsService,
)
watcher.start()
// Give some time to load the initial state
runCurrent()
// Make sure it's warm by changing its internal state
navigationStateService.appNavigationState.emit(AppNavigationState(navigationState = NavigationState.Root, isInForeground = false))
runCurrent()
navigationStateService.appNavigationState.emit(AppNavigationState(navigationState = NavigationState.Root, isInForeground = true))
runCurrent()
// The transaction should be present now
assertThat(analyticsService.getLongRunningTransaction(ResumeAppUntilNewRoomsReceived)).isNotNull()
runCurrent()
// But without the room list syncing, it never finishes
assertThat(analyticsService.getLongRunningTransaction(ResumeAppUntilNewRoomsReceived)).isNotNull()
watcher.stop()
}
@Test
fun `Opening the app when the room list state was already Running does nothing`() = runTest {
val navigationStateService = FakeAppNavigationStateService()
val roomListService = FakeRoomListService().apply {
postState(RoomListService.State.Running)
}
val analyticsService = FakeAnalyticsService()
val watcher = createAnalyticsRoomListStateWatcher(
appNavigationStateService = navigationStateService,
roomListService = roomListService,
analyticsService = analyticsService,
)
watcher.start()
// Give some time to load the initial state
runCurrent()
// Make sure it's warm by changing its internal state
navigationStateService.appNavigationState.emit(AppNavigationState(navigationState = NavigationState.Root, isInForeground = false))
runCurrent()
navigationStateService.appNavigationState.emit(AppNavigationState(navigationState = NavigationState.Root, isInForeground = true))
runCurrent()
// The transaction was never added
assertThat(analyticsService.getLongRunningTransaction(ResumeAppUntilNewRoomsReceived)).isNull()
watcher.stop()
}
private fun TestScope.createAnalyticsRoomListStateWatcher(
appNavigationStateService: FakeAppNavigationStateService = FakeAppNavigationStateService(),
roomListService: FakeRoomListService = FakeRoomListService(),
analyticsService: FakeAnalyticsService = FakeAnalyticsService(),
) = DefaultAnalyticsRoomListStateWatcher(
appNavigationStateService = appNavigationStateService,
roomListService = roomListService,
analyticsService = analyticsService,
sessionCoroutineScope = backgroundScope,
dispatchers = testCoroutineDispatchers(),
)
}