From 63f7538c3ba7ea8fe43c333ec7f33d11d38a17b4 Mon Sep 17 00:00:00 2001 From: Carlos Munoz Date: Sat, 28 Dec 2024 16:58:48 +0100 Subject: [PATCH] Adds attestation analytics events. --- .../FinancialConnectionsSheetViewModel.kt | 24 +++++++++-------- .../FinancialConnectionsAnalyticsEvent.kt | 26 +++++++++++++++++++ .../domain/LookupAccount.kt | 6 ++--- .../domain/RequestIntegrityToken.kt | 24 +++++++++++++++++ 4 files changed, 66 insertions(+), 14 deletions(-) create mode 100644 financial-connections/src/main/java/com/stripe/android/financialconnections/domain/RequestIntegrityToken.kt diff --git a/financial-connections/src/main/java/com/stripe/android/financialconnections/FinancialConnectionsSheetViewModel.kt b/financial-connections/src/main/java/com/stripe/android/financialconnections/FinancialConnectionsSheetViewModel.kt index 979fcc5c8f5..c0d2d10df81 100644 --- a/financial-connections/src/main/java/com/stripe/android/financialconnections/FinancialConnectionsSheetViewModel.kt +++ b/financial-connections/src/main/java/com/stripe/android/financialconnections/FinancialConnectionsSheetViewModel.kt @@ -22,6 +22,8 @@ import com.stripe.android.financialconnections.FinancialConnectionsSheetViewEffe import com.stripe.android.financialconnections.FinancialConnectionsSheetViewEffect.OpenNativeAuthFlow import com.stripe.android.financialconnections.FinancialConnectionsSheetViewModel.Companion.QUERY_PARAM_PAYMENT_METHOD import com.stripe.android.financialconnections.analytics.FinancialConnectionsAnalyticsEvent +import com.stripe.android.financialconnections.analytics.FinancialConnectionsAnalyticsEvent.AttestationInitFailed +import com.stripe.android.financialconnections.analytics.FinancialConnectionsAnalyticsEvent.AttestationInitSucceeded import com.stripe.android.financialconnections.analytics.FinancialConnectionsAnalyticsTracker import com.stripe.android.financialconnections.analytics.FinancialConnectionsEvent.ErrorCode import com.stripe.android.financialconnections.analytics.FinancialConnectionsEvent.Metadata @@ -136,19 +138,19 @@ internal class FinancialConnectionsSheetViewModel @Inject constructor( // If previously within the application session an integrity check failed // do not initialize the request manager and directly launch the web flow. if (integrityVerdictManager.verdictFailed()) { + // TODO send event. return false } - val result = integrityRequestManager.prepare() - result.onFailure { - analyticsTracker.track( - FinancialConnectionsAnalyticsEvent.Error( - extraMessage = "Failed to warm up the IntegrityStandardRequestManager", - pane = Pane.CONSENT, - exception = it - ) - ) - } - return result.isSuccess + return integrityRequestManager.prepare().fold( + onSuccess = { + analyticsTracker.track(AttestationInitSucceeded()) + true + }, + onFailure = { + analyticsTracker.track(AttestationInitFailed(reason = it.message ?: "Unknown error")) + false + } + ) } /** diff --git a/financial-connections/src/main/java/com/stripe/android/financialconnections/analytics/FinancialConnectionsAnalyticsEvent.kt b/financial-connections/src/main/java/com/stripe/android/financialconnections/analytics/FinancialConnectionsAnalyticsEvent.kt index 429d68809d3..9ea9d4adbdd 100644 --- a/financial-connections/src/main/java/com/stripe/android/financialconnections/analytics/FinancialConnectionsAnalyticsEvent.kt +++ b/financial-connections/src/main/java/com/stripe/android/financialconnections/analytics/FinancialConnectionsAnalyticsEvent.kt @@ -410,6 +410,32 @@ internal sealed class FinancialConnectionsAnalyticsEvent( ).filterNotNullValues() ) + class AttestationInitSucceeded() : FinancialConnectionsAnalyticsEvent( + name = "attestation.init_succeeded" + ) + + class AttestationInitFailed( + reason: String + ) : FinancialConnectionsAnalyticsEvent( + name = "attestation.init_failed", + mapOf( + "reason" to reason + ) + ) + + class AttestationRequestSucceeded() : FinancialConnectionsAnalyticsEvent( + name = "attestation.request_token_succeeded" + ) + + class AttestationRequestFailed( + reason: String + ) : FinancialConnectionsAnalyticsEvent( + name = "attestation.request_token_failed", + mapOf( + "reason" to reason + ) + ) + internal val Pane.analyticsValue get() = when (this) { // We want to log partner_auth regardless of the pane being shown full-screen or as a drawer. diff --git a/financial-connections/src/main/java/com/stripe/android/financialconnections/domain/LookupAccount.kt b/financial-connections/src/main/java/com/stripe/android/financialconnections/domain/LookupAccount.kt index 3562b69833a..e52c68d8aa5 100644 --- a/financial-connections/src/main/java/com/stripe/android/financialconnections/domain/LookupAccount.kt +++ b/financial-connections/src/main/java/com/stripe/android/financialconnections/domain/LookupAccount.kt @@ -5,12 +5,11 @@ import com.stripe.android.financialconnections.FinancialConnectionsSheet import com.stripe.android.financialconnections.repository.FinancialConnectionsConsumerSessionRepository import com.stripe.android.model.ConsumerSessionLookup import com.stripe.android.model.EmailSource -import com.stripe.attestation.IntegrityRequestManager import javax.inject.Inject internal class LookupAccount @Inject constructor( private val application: Application, - private val integrityRequestManager: IntegrityRequestManager, + private val requestIntegrityToken: RequestIntegrityToken, private val consumerSessionRepository: FinancialConnectionsConsumerSessionRepository, val configuration: FinancialConnectionsSheet.Configuration, ) { @@ -22,11 +21,12 @@ internal class LookupAccount @Inject constructor( sessionId: String ): ConsumerSessionLookup { return if (verifiedFlow) { + val token = requestIntegrityToken() requireNotNull( consumerSessionRepository.mobileLookupConsumerSession( email = email.lowercase().trim(), emailSource = emailSource, - verificationToken = integrityRequestManager.requestToken().getOrThrow(), + verificationToken = token, appId = application.packageName, sessionId = sessionId ) diff --git a/financial-connections/src/main/java/com/stripe/android/financialconnections/domain/RequestIntegrityToken.kt b/financial-connections/src/main/java/com/stripe/android/financialconnections/domain/RequestIntegrityToken.kt new file mode 100644 index 00000000000..705528ed479 --- /dev/null +++ b/financial-connections/src/main/java/com/stripe/android/financialconnections/domain/RequestIntegrityToken.kt @@ -0,0 +1,24 @@ +package com.stripe.android.financialconnections.domain + +import com.stripe.android.financialconnections.analytics.FinancialConnectionsAnalyticsEvent.AttestationRequestFailed +import com.stripe.android.financialconnections.analytics.FinancialConnectionsAnalyticsEvent.AttestationRequestSucceeded +import com.stripe.android.financialconnections.analytics.FinancialConnectionsAnalyticsTracker +import com.stripe.attestation.IntegrityRequestManager + +internal class RequestIntegrityToken( + private val integrityRequestManager: IntegrityRequestManager, + private val analyticsTracker: FinancialConnectionsAnalyticsTracker +) { + suspend operator fun invoke(): String = integrityRequestManager.requestToken() + .onSuccess { + analyticsTracker.track( + AttestationRequestSucceeded() + ) + } + .onFailure { + analyticsTracker.track( + AttestationRequestFailed(it.message ?: "Unknown error") + ) + } + .getOrThrow() +} \ No newline at end of file