Updated on 2026-08-14

This commit is contained in:
Tangem 2025-01-24 15:55:32 +02:00
parent 57b2825e71
commit 74f6e748d5
5 changed files with 62 additions and 3 deletions

View file

@ -8,6 +8,7 @@ plugins {
alias(deps.plugins.google.services) alias(deps.plugins.google.services)
alias(deps.plugins.hilt.android) alias(deps.plugins.hilt.android)
alias(deps.plugins.firebase.crashlytics) alias(deps.plugins.firebase.crashlytics)
alias(deps.plugins.firebase.perf)
id("configuration") id("configuration")
} }
@ -212,7 +213,10 @@ dependencies {
implementation(deps.firebase.analytics) implementation(deps.firebase.analytics)
implementation(deps.firebase.crashlytics) implementation(deps.firebase.crashlytics)
implementation(deps.firebase.messaging) implementation(deps.firebase.messaging)
implementation(deps.firebase.perf) {
exclude(group = "com.google.firebase", module = "protolite-well-known-types")
exclude(group = "com.google.protobuf", module = "protobuf-javalite")
}
/** Tangem libraries */ /** Tangem libraries */
implementation(deps.tangem.blockchain) { implementation(deps.tangem.blockchain) {
exclude(module = "joda-time") exclude(module = "joda-time")

View file

@ -10,6 +10,7 @@ plugins {
alias(deps.plugins.hilt.android) apply false alias(deps.plugins.hilt.android) apply false
alias(deps.plugins.google.services) apply false alias(deps.plugins.google.services) apply false
alias(deps.plugins.firebase.crashlytics) apply false alias(deps.plugins.firebase.crashlytics) apply false
alias(deps.plugins.firebase.perf) apply false
alias(deps.plugins.room) apply false alias(deps.plugins.room) apply false
} }

View file

@ -40,6 +40,10 @@ dependencies {
implementation(deps.tangem.card.core) implementation(deps.tangem.card.core)
implementation(deps.tangem.blockchain) implementation(deps.tangem.blockchain)
implementation(deps.timber) implementation(deps.timber)
implementation(deps.firebase.perf) {
exclude(group = "com.google.firebase", module = "protolite-well-known-types")
exclude(group = "com.google.protobuf", module = "protobuf-javalite")
}
/** DI */ /** DI */
implementation(deps.hilt.android) implementation(deps.hilt.android)

View file

@ -1,6 +1,8 @@
package com.tangem.feature.wallet.presentation.wallet.analytics.utils package com.tangem.feature.wallet.presentation.wallet.analytics.utils
import arrow.core.getOrElse import arrow.core.getOrElse
import com.google.firebase.perf.FirebasePerformance
import com.google.firebase.perf.metrics.Trace
import com.tangem.blockchain.common.Blockchain import com.tangem.blockchain.common.Blockchain
import com.tangem.blockchainsdk.utils.fromNetworkId import com.tangem.blockchainsdk.utils.fromNetworkId
import com.tangem.common.extensions.isZero import com.tangem.common.extensions.isZero
@ -12,6 +14,7 @@ import com.tangem.domain.tokens.model.CryptoCurrencyStatus
import com.tangem.domain.tokens.model.TokenList import com.tangem.domain.tokens.model.TokenList
import com.tangem.domain.tokens.model.TotalFiatBalance import com.tangem.domain.tokens.model.TotalFiatBalance
import com.tangem.domain.wallets.models.UserWallet import com.tangem.domain.wallets.models.UserWallet
import com.tangem.domain.wallets.models.UserWalletId
import com.tangem.feature.wallet.presentation.wallet.analytics.WalletScreenAnalyticsEvent.Basic import com.tangem.feature.wallet.presentation.wallet.analytics.WalletScreenAnalyticsEvent.Basic
import com.tangem.feature.wallet.presentation.wallet.analytics.WalletScreenAnalyticsEvent.MainScreen import com.tangem.feature.wallet.presentation.wallet.analytics.WalletScreenAnalyticsEvent.MainScreen
import com.tangem.feature.wallet.presentation.wallet.state.model.WalletState import com.tangem.feature.wallet.presentation.wallet.state.model.WalletState
@ -31,11 +34,20 @@ internal class TokenListAnalyticsSender @Inject constructor(
private val balanceWasSentMap = mutableMapOf<String, Boolean>() private val balanceWasSentMap = mutableMapOf<String, Boolean>()
private val mutex = Mutex() private val mutex = Mutex()
private val loadingTraces = mutableMapOf<UserWalletId, Trace>()
suspend fun send(displayedUiState: WalletState?, userWallet: UserWallet, tokenList: TokenList) { suspend fun send(displayedUiState: WalletState?, userWallet: UserWallet, tokenList: TokenList) {
if (screenLifecycleProvider.isBackgroundState.value) return if (screenLifecycleProvider.isBackgroundState.value) return
if (displayedUiState == null || displayedUiState.pullToRefreshConfig.isRefreshing) return if (displayedUiState == null || displayedUiState.pullToRefreshConfig.isRefreshing) return
if (tokenList.totalFiatBalance is TotalFiatBalance.Loading) return
if (tokenList.totalFiatBalance is TotalFiatBalance.Loading) {
startLoadingTraceIfNeeded(userWallet.walletId, tokenList)
return
}
if (isTerminalState(tokenList.totalFiatBalance)) {
stopLoadingTraceIfNeeded(userWallet.walletId, tokenList.totalFiatBalance)
}
val currenciesStatuses = tokenList.flattenCurrencies() val currenciesStatuses = tokenList.flattenCurrencies()
@ -45,6 +57,35 @@ internal class TokenListAnalyticsSender @Inject constructor(
sendTokenBalancesIfNeeded(currenciesStatuses) sendTokenBalancesIfNeeded(currenciesStatuses)
} }
private suspend fun startLoadingTraceIfNeeded(userWalletId: UserWalletId, tokenList: TokenList) {
mutex.withLock {
if (!loadingTraces.containsKey(userWalletId)) {
val trace = FirebasePerformance.getInstance().newTrace(BALANCE_LOADED_TRACE_NAME)
trace.start()
trace.putAttribute(TOKENS_COUNT, tokenList.flattenCurrencies().size.toString())
loadingTraces[userWalletId] = trace
}
}
}
private suspend fun stopLoadingTraceIfNeeded(userWalletId: UserWalletId, totalFiatBalance: TotalFiatBalance) {
mutex.withLock {
loadingTraces[userWalletId]?.apply {
when (totalFiatBalance) {
is TotalFiatBalance.Loaded -> putAttribute(HAS_ERROR, "No")
is TotalFiatBalance.Failed -> putAttribute(HAS_ERROR, "Yes")
else -> { /* Intentionally do nothing */ }
}
stop()
loadingTraces.remove(userWalletId)
}
}
}
private fun isTerminalState(balance: TotalFiatBalance): Boolean {
return balance is TotalFiatBalance.Failed || balance is TotalFiatBalance.Loaded
}
private fun sendBalanceLoadedEventIfNeeded( private fun sendBalanceLoadedEventIfNeeded(
fiatBalance: TotalFiatBalance, fiatBalance: TotalFiatBalance,
currenciesStatuses: List<CryptoCurrencyStatus>, currenciesStatuses: List<CryptoCurrencyStatus>,
@ -179,4 +220,10 @@ internal class TokenListAnalyticsSender @Inject constructor(
analyticsEventHandler.send(MainScreen.NetworksUnreachable(unreachableCurrencies)) analyticsEventHandler.send(MainScreen.NetworksUnreachable(unreachableCurrencies))
} }
} }
companion object {
const val BALANCE_LOADED_TRACE_NAME = "Total_balance_loaded"
const val HAS_ERROR = "has_error"
const val TOKENS_COUNT = "tokens_count"
}
} }

View file

@ -7,6 +7,7 @@ androidGradlePlugin = "8.2.2"
firebaseCrashlytics = "3.0.1" firebaseCrashlytics = "3.0.1"
googleServices = "4.4.1" googleServices = "4.4.1"
kotlin = "1.9.22" kotlin = "1.9.22"
firebasePerf = "1.4.2"
# endregion Classpath # endregion Classpath
# region AndroidX # region AndroidX
@ -45,7 +46,7 @@ coil = "2.1.0"
compose-shimmer = "1.0.3" compose-shimmer = "1.0.3"
coroutine = "1.7.2" coroutine = "1.7.2"
desugarJdkLibs = "1.1.5" desugarJdkLibs = "1.1.5"
firebase = "33.1.0" firebase = "33.7.0"
googleMaterialComponent = "1.6.1" googleMaterialComponent = "1.6.1"
googlePlayReview = "2.0.1" googlePlayReview = "2.0.1"
googlePlayReviewKtx = "2.0.1" googlePlayReviewKtx = "2.0.1"
@ -122,6 +123,7 @@ kotlin-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", versi
android-application = { id = "com.android.application", version.ref = "androidGradlePlugin" } android-application = { id = "com.android.application", version.ref = "androidGradlePlugin" }
android-library = { id = "com.android.library", version.ref = "androidGradlePlugin" } android-library = { id = "com.android.library", version.ref = "androidGradlePlugin" }
firebase-crashlytics = { id = "com.google.firebase.crashlytics", version.ref = "firebaseCrashlytics" } firebase-crashlytics = { id = "com.google.firebase.crashlytics", version.ref = "firebaseCrashlytics" }
firebase-perf = { id = "com.google.firebase.firebase-perf", version.ref = "firebasePerf" }
google-services = { id = "com.google.gms.google-services", version.ref = "googleServices" } google-services = { id = "com.google.gms.google-services", version.ref = "googleServices" }
hilt-android = { id = "com.google.dagger.hilt.android", version.ref = "hilt" } hilt-android = { id = "com.google.dagger.hilt.android", version.ref = "hilt" }
detekt = { id = "io.gitlab.arturbosch.detekt", version.ref = "detekt" } detekt = { id = "io.gitlab.arturbosch.detekt", version.ref = "detekt" }
@ -182,6 +184,7 @@ firebase-bom = { module = "com.google.firebase:firebase-bom", version.ref = "fir
firebase-analytics = { module = "com.google.firebase:firebase-analytics-ktx" } firebase-analytics = { module = "com.google.firebase:firebase-analytics-ktx" }
firebase-crashlytics = { module = "com.google.firebase:firebase-crashlytics-ktx" } firebase-crashlytics = { module = "com.google.firebase:firebase-crashlytics-ktx" }
firebase-messaging = { module = "com.google.firebase:firebase-messaging-ktx" } firebase-messaging = { module = "com.google.firebase:firebase-messaging-ktx" }
firebase-perf = { module = "com.google.firebase:firebase-perf" }
# endregion Firebase # endregion Firebase
# region Tangem # region Tangem