Skip to content

Commit dee7842

Browse files
authored
chore(chat): add analytics to track sent cash and message (success/failures) (#1013)
Signed-off-by: Brandon McAnsh <git@bmcreations.dev>
1 parent 76877df commit dee7842

5 files changed

Lines changed: 49 additions & 3 deletions

File tree

apps/flipcash/features/messenger/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ android {
77
}
88

99
dependencies {
10+
implementation(project(":apps:flipcash:shared:analytics"))
1011
implementation(project(":apps:flipcash:shared:chat"))
1112
implementation(project(":apps:flipcash:shared:chat-ui"))
1213
implementation(project(":apps:flipcash:shared:amount-entry"))

apps/flipcash/features/messenger/src/main/kotlin/com/flipcash/app/messenger/internal/ChatViewModel.kt

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import androidx.paging.PagingData
99
import androidx.paging.cachedIn
1010
import androidx.paging.flatMap
1111
import androidx.paging.insertSeparators
12+
import com.flipcash.app.analytics.Analytics
13+
import com.flipcash.app.analytics.FlipcashAnalyticsService
1214
import com.flipcash.app.contacts.ContactCoordinator
1315
import com.flipcash.app.core.AppRoute
1416
import com.flipcash.app.core.chat.ChatIdentifier
@@ -94,6 +96,7 @@ internal class ChatViewModel @Inject constructor(
9496
private val userManager: UserManager,
9597
private val resources: ResourceHelper,
9698
private val featureFlags: FeatureFlagController,
99+
private val analytics: FlipcashAnalyticsService,
97100
) : BaseViewModel<ChatViewModel.State, ChatViewModel.Event>(
98101
initialState = State(),
99102
updateStateForEvent = updateStateForEvent,
@@ -473,8 +476,14 @@ internal class ChatViewModel @Inject constructor(
473476

474477
viewModelScope.launch {
475478
chatCoordinator.sendMessage(chatId, textToSend)
476-
.onSuccess { trace("message sent successfully") }
477-
.onFailure { trace("message failed to send - ${it.localizedMessage}") }
479+
.onSuccess {
480+
trace("message sent successfully")
481+
analytics.messageSentInChat()
482+
}
483+
.onFailure { cause ->
484+
trace("message failed to send - ${cause.localizedMessage}")
485+
analytics.messageSentInChat(error = cause)
486+
}
478487
}
479488
}
480489
.flowOn(Dispatchers.Main.immediate)
@@ -618,12 +627,22 @@ internal class ChatViewModel @Inject constructor(
618627
chatCoordinator.refreshFeed()
619628
}
620629
delay(400.milliseconds)
630+
analytics.transfer(
631+
event = Analytics.Transfer.SentCash,
632+
amount = verifiedFiat.localFiat,
633+
successful = true,
634+
)
621635
dispatchEvent(
622636
Dispatchers.Main,
623637
Event.SendComplete(amount.localFiat.nativeAmount)
624638
)
625-
}.onFailure {
639+
}.onFailure { cause ->
626640
dispatchEvent(Event.SendStateUpdated())
641+
analytics.transfer(
642+
event = Analytics.Transfer.SentCash,
643+
amount = verifiedFiat.localFiat,
644+
error = cause,
645+
)
627646
BottomBarManager.showError(
628647
title = resources.getString(R.string.error_title_cashFailedToSend),
629648
message = resources.getString(R.string.error_description_cashFailedToSend),

apps/flipcash/shared/analytics/src/main/kotlin/com/flipcash/app/analytics/Analytics.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ interface FlipcashAnalyticsService : AnalyticsService {
2929
fun openTokenInfo(source: Analytics.TokenInfoSource, mint: Mint)
3030
fun buy(method: Analytics.PurchaseMethod, mint: Mint, amount: Fiat, error: Throwable? = null)
3131
fun sell(mint: Mint, amount: Fiat, feeAmount: Fiat, error: Throwable? = null)
32+
fun messageSentInChat(error: Throwable? = null)
3233
fun deeplinkOpened(url: String)
3334
fun deeplinkParsed(type: DeeplinkType?, url: String)
3435
fun deeplinkRouted(type: DeeplinkType, error: Throwable? = null)
@@ -55,6 +56,8 @@ object Analytics {
5556
data object Clipboard : SentCashLink
5657
data class App(val name: String) : SentCashLink
5758
}
59+
60+
data object SentCash : Transfer
5861
}
5962
enum class OnrampSource { Settings, Balance, Give }
6063
enum class OnrampVerificationStep { ShowInfo, EnterPhone, ConfirmPhone, EnterEmail, ConfirmEmail }
@@ -96,6 +99,8 @@ class StubFlipcashAnalytics : FlipcashAnalyticsService {
9699
override fun buy(method: Analytics.PurchaseMethod, mint: Mint, amount: Fiat, error: Throwable?) = Unit
97100
override fun sell(mint: Mint, amount: Fiat, feeAmount: Fiat, error: Throwable?) = Unit
98101

102+
override fun messageSentInChat(error: Throwable?) = Unit
103+
99104
override fun deeplinkOpened(url: String) = Unit
100105
override fun deeplinkParsed(type: DeeplinkType?, url: String) = Unit
101106
override fun deeplinkRouted(type: DeeplinkType, error: Throwable?) = Unit

apps/flipcash/shared/analytics/src/main/kotlin/com/flipcash/app/analytics/Events.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,22 @@ internal sealed interface AnalyticsEvent {
133133
override val name = "Receive Cash Link"
134134
}
135135

136+
sealed interface ChatEvent: AnalyticsEvent {
137+
data object SentCash : ChatEvent {
138+
override val name = "Sent Cash"
139+
}
140+
141+
data class SentMessage(
142+
val error: Throwable? = null
143+
): ChatEvent {
144+
override val name = "Sent Message"
145+
146+
override fun toProperties() = buildMap {
147+
error?.let { put("Error", it.message.orEmpty()) }
148+
}
149+
}
150+
}
151+
136152
sealed interface PoolEvent : AnalyticsEvent {
137153
val id: ID
138154
override fun toProperties() = mapOf("ID" to id.base58)
@@ -310,4 +326,5 @@ internal fun Analytics.Transfer.toAnalyticsEvent(): AnalyticsEvent = when (this)
310326
is Analytics.Transfer.ClaimedCashLink -> AnalyticsEvent.ClaimedCashLink
311327
is Analytics.Transfer.SentCashLink.Clipboard -> AnalyticsEvent.SentCashLink(clipboard = true)
312328
is Analytics.Transfer.SentCashLink.App -> AnalyticsEvent.SentCashLink(app = name)
329+
is Analytics.Transfer.SentCash -> AnalyticsEvent.ChatEvent.SentCash
313330
}

apps/flipcash/shared/analytics/src/main/kotlin/com/flipcash/app/analytics/internal/MixpanelAnalyticsDelegate.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,10 @@ internal class MixpanelAnalyticsDelegate @Inject constructor(
204204
track(AnalyticsEvent.TokenTransactionEvent.Sell(mint, amount, feeAmount, error))
205205
}
206206

207+
override fun messageSentInChat(error: Throwable?) {
208+
track(AnalyticsEvent.ChatEvent.SentMessage(error = error))
209+
}
210+
207211
override fun deeplinkOpened(url: String) {
208212
track(AnalyticsEvent.DeeplinkEvent.Open(url))
209213
}

0 commit comments

Comments
 (0)