forked from dsutanto/bChot-android
First Commit
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
plugins {
|
||||
id("io.element.android-compose-library")
|
||||
}
|
||||
|
||||
android {
|
||||
namespace = "io.element.android.services.analytics.api"
|
||||
}
|
||||
|
||||
dependencies {
|
||||
api(projects.services.analyticsproviders.api)
|
||||
api(projects.services.toolbox.api)
|
||||
implementation(libs.coroutines.core)
|
||||
implementation(projects.libraries.core)
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* 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.api
|
||||
|
||||
sealed class AnalyticsLongRunningTransaction(
|
||||
val name: String,
|
||||
val operation: String?,
|
||||
) {
|
||||
data object ColdStartUntilCachedRoomList : AnalyticsLongRunningTransaction("Cold start until cached room list is displayed", null)
|
||||
data object FirstRoomsDisplayed : AnalyticsLongRunningTransaction("First rooms displayed after login or restoration", null)
|
||||
data object ResumeAppUntilNewRoomsReceived : AnalyticsLongRunningTransaction("App was resumed and new room list items arrived", null)
|
||||
data object NotificationTapOpensTimeline : AnalyticsLongRunningTransaction("A notification was tapped and it opened a timeline", null)
|
||||
data object OpenRoom : AnalyticsLongRunningTransaction("Open a room and see loaded items in the timeline", null)
|
||||
data object LoadJoinedRoomFlow : AnalyticsLongRunningTransaction("Load joined room UI", "ui.load")
|
||||
data object LoadMessagesUi : AnalyticsLongRunningTransaction("Load messages UI", "ui.load")
|
||||
data object DisplayFirstTimelineItems : AnalyticsLongRunningTransaction("Get and display first timeline items", null)
|
||||
}
|
||||
+112
@@ -0,0 +1,112 @@
|
||||
/*
|
||||
* 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.api
|
||||
|
||||
import io.element.android.services.analyticsproviders.api.AnalyticsProvider
|
||||
import io.element.android.services.analyticsproviders.api.AnalyticsTransaction
|
||||
import io.element.android.services.analyticsproviders.api.trackers.AnalyticsTracker
|
||||
import io.element.android.services.analyticsproviders.api.trackers.ErrorTracker
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
interface AnalyticsService : AnalyticsTracker, ErrorTracker {
|
||||
/**
|
||||
* Get the available analytics providers.
|
||||
*/
|
||||
fun getAvailableAnalyticsProviders(): Set<AnalyticsProvider>
|
||||
|
||||
/**
|
||||
* A Flow of Boolean, true if the user has given their consent.
|
||||
*/
|
||||
val userConsentFlow: Flow<Boolean>
|
||||
|
||||
/**
|
||||
* Update the user consent value.
|
||||
*/
|
||||
suspend fun setUserConsent(userConsent: Boolean)
|
||||
|
||||
/**
|
||||
* A Flow of Boolean, true if the user has been asked for their consent.
|
||||
*/
|
||||
val didAskUserConsentFlow: Flow<Boolean>
|
||||
|
||||
/**
|
||||
* Store the fact that the user has been asked for their consent.
|
||||
*/
|
||||
suspend fun setDidAskUserConsent()
|
||||
|
||||
/**
|
||||
* A Flow of String, used for analytics Id.
|
||||
*/
|
||||
val analyticsIdFlow: Flow<String>
|
||||
|
||||
/**
|
||||
* Update analyticsId from the AccountData.
|
||||
*/
|
||||
suspend fun setAnalyticsId(analyticsId: String)
|
||||
|
||||
/**
|
||||
* Starts a transaction to measure the performance of an operation.
|
||||
*/
|
||||
fun startTransaction(name: String, operation: String? = null): AnalyticsTransaction
|
||||
|
||||
/**
|
||||
* Starts an [AnalyticsLongRunningTransaction], that can be shared with other components.
|
||||
*/
|
||||
fun startLongRunningTransaction(
|
||||
longRunningTransaction: AnalyticsLongRunningTransaction,
|
||||
parentTransaction: AnalyticsTransaction? = null
|
||||
): AnalyticsTransaction
|
||||
|
||||
/**
|
||||
* Gets an ongoing [AnalyticsLongRunningTransaction], if it exists.
|
||||
*/
|
||||
fun getLongRunningTransaction(longRunningTransaction: AnalyticsLongRunningTransaction): AnalyticsTransaction?
|
||||
|
||||
/**
|
||||
* Removes an ongoing [AnalyticsLongRunningTransaction] so it's no longer shared.
|
||||
*/
|
||||
fun removeLongRunningTransaction(longRunningTransaction: AnalyticsLongRunningTransaction): AnalyticsTransaction?
|
||||
}
|
||||
|
||||
inline fun <T> AnalyticsService.recordTransaction(
|
||||
name: String,
|
||||
operation: String,
|
||||
parentTransaction: AnalyticsTransaction? = null,
|
||||
block: (AnalyticsTransaction) -> T
|
||||
): T {
|
||||
val transaction = parentTransaction?.startChild(name, operation)
|
||||
?: startTransaction(name, operation)
|
||||
try {
|
||||
val result = block(transaction)
|
||||
return result
|
||||
} finally {
|
||||
transaction.finish()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Cancels a long running transaction. It behaves the same as [AnalyticsService.removeLongRunningTransaction],
|
||||
* but it doesn't return the transaction so we can't finish it later.
|
||||
*/
|
||||
fun AnalyticsService.cancelLongRunningTransaction(
|
||||
longRunningTransaction: AnalyticsLongRunningTransaction
|
||||
) = removeLongRunningTransaction(longRunningTransaction)
|
||||
|
||||
/**
|
||||
* Finishes a long running transaction if it exists. Optionally performs an [action] with the transaction before finishing it.
|
||||
*/
|
||||
fun AnalyticsService.finishLongRunningTransaction(
|
||||
longRunningTransaction: AnalyticsLongRunningTransaction,
|
||||
action: (AnalyticsTransaction) -> Unit = {},
|
||||
) {
|
||||
removeLongRunningTransaction(longRunningTransaction)?.let {
|
||||
action(it)
|
||||
it.finish()
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
/*
|
||||
* 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.api
|
||||
|
||||
import io.element.android.services.analyticsproviders.api.AnalyticsTransaction
|
||||
|
||||
object NoopAnalyticsTransaction : AnalyticsTransaction {
|
||||
override fun startChild(operation: String, description: String?): AnalyticsTransaction = NoopAnalyticsTransaction
|
||||
override fun setData(key: String, value: Any) {}
|
||||
override fun isFinished(): Boolean = true
|
||||
override fun finish() {}
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
/*
|
||||
* 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.api
|
||||
|
||||
import androidx.compose.runtime.Composable
|
||||
import im.vector.app.features.analytics.plan.MobileScreen
|
||||
|
||||
interface ScreenTracker {
|
||||
@Composable
|
||||
fun TrackScreen(
|
||||
screen: MobileScreen.ScreenName,
|
||||
)
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
/*
|
||||
* 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.api.watchers
|
||||
|
||||
/**
|
||||
* Adds a performance check transaction measuring the time between a cold start (or, after we read the user consent after a cold start)
|
||||
* until the cached room list is displayed. This check only takes place in a cold app start after the user is authenticated.
|
||||
*/
|
||||
interface AnalyticsColdStartWatcher {
|
||||
fun start()
|
||||
fun whenLoggingIn()
|
||||
fun onRoomListVisible()
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
/*
|
||||
* 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.api.watchers
|
||||
|
||||
/**
|
||||
* This component is used to check how long it takes for the room list to be up to date after opening the app while it's on a 'warm' state:
|
||||
* the app was previously running and we just returned to it.
|
||||
*/
|
||||
interface AnalyticsRoomListStateWatcher {
|
||||
fun start()
|
||||
fun stop()
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
plugins {
|
||||
id("io.element.android-compose-library")
|
||||
}
|
||||
|
||||
android {
|
||||
namespace = "io.element.android.services.analytics.compose"
|
||||
}
|
||||
dependencies {
|
||||
api(projects.services.analytics.api)
|
||||
implementation(projects.services.analytics.noop)
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
/*
|
||||
* 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.compose
|
||||
|
||||
import androidx.compose.runtime.staticCompositionLocalOf
|
||||
import io.element.android.services.analytics.api.AnalyticsService
|
||||
import io.element.android.services.analytics.noop.NoopAnalyticsService
|
||||
|
||||
/**
|
||||
* Global key to access the [AnalyticsService] in the composition tree.
|
||||
*/
|
||||
val LocalAnalyticsService = staticCompositionLocalOf<AnalyticsService> {
|
||||
NoopAnalyticsService()
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
+174
@@ -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)
|
||||
}
|
||||
}
|
||||
+50
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+13
@@ -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")
|
||||
+83
@@ -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()
|
||||
}
|
||||
}
|
||||
}
|
||||
+63
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
+78
@@ -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()
|
||||
}
|
||||
}
|
||||
}
|
||||
+300
@@ -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"
|
||||
}
|
||||
}
|
||||
+62
@@ -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,
|
||||
)
|
||||
+39
@@ -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()
|
||||
}
|
||||
}
|
||||
+107
@@ -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,
|
||||
)
|
||||
}
|
||||
+170
@@ -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(),
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
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")
|
||||
}
|
||||
|
||||
android {
|
||||
namespace = "io.element.android.services.analytics.noop"
|
||||
}
|
||||
|
||||
setupDependencyInjection()
|
||||
|
||||
dependencies {
|
||||
implementation(projects.libraries.architecture)
|
||||
implementation(projects.libraries.di)
|
||||
api(projects.services.analytics.api)
|
||||
testCommonDependencies(libs)
|
||||
}
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* 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.noop
|
||||
|
||||
import dev.zacsweers.metro.AppScope
|
||||
import dev.zacsweers.metro.ContributesBinding
|
||||
import dev.zacsweers.metro.SingleIn
|
||||
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.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.analyticsproviders.api.AnalyticsProvider
|
||||
import io.element.android.services.analyticsproviders.api.AnalyticsTransaction
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.flowOf
|
||||
|
||||
@SingleIn(AppScope::class)
|
||||
@ContributesBinding(AppScope::class)
|
||||
class NoopAnalyticsService : AnalyticsService {
|
||||
override fun getAvailableAnalyticsProviders(): Set<AnalyticsProvider> = emptySet()
|
||||
override val userConsentFlow: Flow<Boolean> = flowOf(false)
|
||||
override suspend fun setUserConsent(userConsent: Boolean) = Unit
|
||||
override val didAskUserConsentFlow: Flow<Boolean> = flowOf(true)
|
||||
override suspend fun setDidAskUserConsent() = Unit
|
||||
override val analyticsIdFlow: Flow<String> = flowOf("")
|
||||
override suspend fun setAnalyticsId(analyticsId: String) = Unit
|
||||
override fun capture(event: VectorAnalyticsEvent) = Unit
|
||||
override fun screen(screen: VectorAnalyticsScreen) = Unit
|
||||
override fun updateUserProperties(userProperties: UserProperties) = Unit
|
||||
override fun trackError(throwable: Throwable) = Unit
|
||||
override fun updateSuperProperties(updatedProperties: SuperProperties) = Unit
|
||||
override fun startTransaction(name: String, operation: String?): AnalyticsTransaction = NoopAnalyticsTransaction
|
||||
override fun startLongRunningTransaction(
|
||||
longRunningTransaction: AnalyticsLongRunningTransaction,
|
||||
parentTransaction: AnalyticsTransaction?,
|
||||
): AnalyticsTransaction = NoopAnalyticsTransaction
|
||||
override fun getLongRunningTransaction(longRunningTransaction: AnalyticsLongRunningTransaction): AnalyticsTransaction? = null
|
||||
override fun removeLongRunningTransaction(longRunningTransaction: AnalyticsLongRunningTransaction) = NoopAnalyticsTransaction
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
* 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.noop
|
||||
|
||||
import androidx.compose.runtime.Composable
|
||||
import dev.zacsweers.metro.AppScope
|
||||
import dev.zacsweers.metro.ContributesBinding
|
||||
import im.vector.app.features.analytics.plan.MobileScreen
|
||||
import io.element.android.services.analytics.api.ScreenTracker
|
||||
|
||||
@ContributesBinding(AppScope::class)
|
||||
class NoopScreenTracker : ScreenTracker {
|
||||
@Composable
|
||||
override fun TrackScreen(screen: MobileScreen.ScreenName) = Unit
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
/*
|
||||
* 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.noop.watchers
|
||||
|
||||
import dev.zacsweers.metro.AppScope
|
||||
import dev.zacsweers.metro.ContributesBinding
|
||||
import io.element.android.services.analytics.api.watchers.AnalyticsColdStartWatcher
|
||||
|
||||
@ContributesBinding(AppScope::class)
|
||||
class NoopAnalyticsColdStartWatcher : AnalyticsColdStartWatcher {
|
||||
override fun start() {}
|
||||
override fun whenLoggingIn() {}
|
||||
override fun onRoomListVisible() {}
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
/*
|
||||
* 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.noop.watchers
|
||||
|
||||
import dev.zacsweers.metro.ContributesBinding
|
||||
import io.element.android.libraries.di.SessionScope
|
||||
import io.element.android.services.analytics.api.watchers.AnalyticsRoomListStateWatcher
|
||||
|
||||
@ContributesBinding(SessionScope::class)
|
||||
class NoopAnalyticsRoomListStateWatcher : AnalyticsRoomListStateWatcher {
|
||||
override fun start() {}
|
||||
override fun stop() {}
|
||||
}
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* 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.noop
|
||||
|
||||
import app.cash.turbine.test
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import im.vector.app.features.analytics.plan.CallStarted
|
||||
import im.vector.app.features.analytics.plan.MobileScreen
|
||||
import im.vector.app.features.analytics.plan.SuperProperties
|
||||
import im.vector.app.features.analytics.plan.UserProperties
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import org.junit.Test
|
||||
|
||||
class NoopAnalyticsServiceTest {
|
||||
@Test
|
||||
fun `getAvailableAnalyticsProviders returns emptySet`() {
|
||||
val sut = NoopAnalyticsService()
|
||||
assertThat(sut.getAvailableAnalyticsProviders()).isEmpty()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `didAskUserConsentFlow emits only true`() = runTest {
|
||||
val sut = NoopAnalyticsService()
|
||||
sut.didAskUserConsentFlow.test {
|
||||
assertThat(awaitItem()).isTrue()
|
||||
awaitComplete()
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `analyticsIdFlow emits only empty string`() = runTest {
|
||||
val sut = NoopAnalyticsService()
|
||||
sut.analyticsIdFlow.test {
|
||||
assertThat(awaitItem()).isEmpty()
|
||||
sut.setAnalyticsId("anId")
|
||||
awaitComplete()
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `userConsentFlow emits only false`() = runTest {
|
||||
val sut = NoopAnalyticsService()
|
||||
sut.userConsentFlow.test {
|
||||
assertThat(awaitItem()).isFalse()
|
||||
awaitComplete()
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test no op methods`() = runTest {
|
||||
val sut = NoopAnalyticsService()
|
||||
sut.setUserConsent(false)
|
||||
sut.setUserConsent(true)
|
||||
sut.setDidAskUserConsent()
|
||||
sut.setAnalyticsId("anId")
|
||||
sut.capture(CallStarted(true, 1, true))
|
||||
sut.screen(MobileScreen(screenName = MobileScreen.ScreenName.RoomMembers))
|
||||
sut.updateUserProperties(UserProperties())
|
||||
sut.trackError(Exception("an_error"))
|
||||
sut.updateSuperProperties(SuperProperties())
|
||||
}
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* 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.noop
|
||||
|
||||
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 kotlinx.coroutines.test.runTest
|
||||
import org.junit.Test
|
||||
|
||||
class NoopScreenTrackerTest {
|
||||
@Test
|
||||
fun `TrackScreen is no op`() = runTest {
|
||||
val sut = NoopScreenTracker()
|
||||
moleculeFlow(RecompositionMode.Immediate) {
|
||||
sut.TrackScreen(MobileScreen.ScreenName.RoomMembers)
|
||||
}.test {
|
||||
assertThat(awaitItem()).isEqualTo(Unit)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
plugins {
|
||||
id("io.element.android-compose-library")
|
||||
}
|
||||
|
||||
android {
|
||||
namespace = "io.element.android.services.analytics.test"
|
||||
}
|
||||
|
||||
dependencies {
|
||||
api(projects.services.analytics.api)
|
||||
implementation(projects.libraries.core)
|
||||
implementation(projects.tests.testutils)
|
||||
implementation(libs.coroutines.core)
|
||||
}
|
||||
+89
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
* 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.test
|
||||
|
||||
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.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.analyticsproviders.api.AnalyticsProvider
|
||||
import io.element.android.services.analyticsproviders.api.AnalyticsTransaction
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
|
||||
class FakeAnalyticsService(
|
||||
isEnabled: Boolean = false,
|
||||
didAskUserConsent: Boolean = false,
|
||||
) : AnalyticsService {
|
||||
private val isEnabledFlow = MutableStateFlow(isEnabled)
|
||||
override val didAskUserConsentFlow = MutableStateFlow(didAskUserConsent)
|
||||
val capturedEvents = mutableListOf<VectorAnalyticsEvent>()
|
||||
val screenEvents = mutableListOf<VectorAnalyticsScreen>()
|
||||
val trackedErrors = mutableListOf<Throwable>()
|
||||
val capturedUserProperties = mutableListOf<UserProperties>()
|
||||
val longRunningTransactions = mutableMapOf<AnalyticsLongRunningTransaction, AnalyticsTransaction>()
|
||||
|
||||
override fun getAvailableAnalyticsProviders(): Set<AnalyticsProvider> = emptySet()
|
||||
|
||||
override val userConsentFlow: Flow<Boolean> = isEnabledFlow.asStateFlow()
|
||||
|
||||
override suspend fun setUserConsent(userConsent: Boolean) {
|
||||
isEnabledFlow.value = userConsent
|
||||
}
|
||||
|
||||
override suspend fun setDidAskUserConsent() {
|
||||
didAskUserConsentFlow.value = true
|
||||
}
|
||||
|
||||
override val analyticsIdFlow: Flow<String> = MutableStateFlow("")
|
||||
|
||||
override suspend fun setAnalyticsId(analyticsId: String) {
|
||||
}
|
||||
|
||||
override fun capture(event: VectorAnalyticsEvent) {
|
||||
capturedEvents += event
|
||||
}
|
||||
|
||||
override fun screen(screen: VectorAnalyticsScreen) {
|
||||
screenEvents += screen
|
||||
}
|
||||
|
||||
override fun updateUserProperties(userProperties: UserProperties) {
|
||||
capturedUserProperties += userProperties
|
||||
}
|
||||
|
||||
override fun trackError(throwable: Throwable) {
|
||||
trackedErrors += throwable
|
||||
}
|
||||
|
||||
override fun updateSuperProperties(updatedProperties: SuperProperties) {
|
||||
// No op
|
||||
}
|
||||
|
||||
override fun startTransaction(name: String, operation: String?): AnalyticsTransaction = NoopAnalyticsTransaction
|
||||
override fun startLongRunningTransaction(
|
||||
longRunningTransaction: AnalyticsLongRunningTransaction,
|
||||
parentTransaction: AnalyticsTransaction?
|
||||
): AnalyticsTransaction {
|
||||
longRunningTransactions[longRunningTransaction] = NoopAnalyticsTransaction
|
||||
return NoopAnalyticsTransaction
|
||||
}
|
||||
|
||||
override fun getLongRunningTransaction(longRunningTransaction: AnalyticsLongRunningTransaction): AnalyticsTransaction? {
|
||||
return longRunningTransactions[longRunningTransaction]
|
||||
}
|
||||
|
||||
override fun removeLongRunningTransaction(longRunningTransaction: AnalyticsLongRunningTransaction): AnalyticsTransaction? {
|
||||
return longRunningTransactions.remove(longRunningTransaction)
|
||||
}
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* 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.test
|
||||
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import im.vector.app.features.analytics.plan.MobileScreen
|
||||
import io.element.android.services.analytics.api.ScreenTracker
|
||||
import io.element.android.tests.testutils.lambda.lambdaError
|
||||
|
||||
class FakeScreenTracker(
|
||||
private val trackScreenLambda: (MobileScreen.ScreenName) -> Unit = { lambdaError() }
|
||||
) : ScreenTracker {
|
||||
@Composable
|
||||
override fun TrackScreen(screen: MobileScreen.ScreenName) {
|
||||
LaunchedEffect(Unit) {
|
||||
trackScreenLambda(screen)
|
||||
}
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
/*
|
||||
* 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.test.watchers
|
||||
|
||||
import io.element.android.services.analytics.api.watchers.AnalyticsColdStartWatcher
|
||||
|
||||
class FakeAnalyticsColdStartWatcher : AnalyticsColdStartWatcher {
|
||||
override fun start() {}
|
||||
override fun whenLoggingIn() {}
|
||||
override fun onRoomListVisible() {}
|
||||
}
|
||||
Reference in New Issue
Block a user