Skip to content

Commit e5689f6

Browse files
committed
chore(flipcash): track account creation payments
Signed-off-by: Brandon McAnsh <[email protected]>
1 parent cc4e51a commit e5689f6

File tree

3 files changed

+79
-21
lines changed

3 files changed

+79
-21
lines changed

services/flipcash/src/main/kotlin/com/flipcash/services/analytics/Analytics.kt

Lines changed: 63 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,42 @@
11
package com.flipcash.services.analytics
22

3+
import com.getcode.ed25519.Ed25519.KeyPair
34
import com.getcode.libs.analytics.AnalyticsService
45
import com.getcode.libs.analytics.AppAction
56
import com.getcode.libs.analytics.AppActionSource
7+
import com.getcode.opencode.model.financial.CurrencyCode
68
import com.getcode.opencode.model.financial.Fiat
79
import com.getcode.opencode.model.financial.LocalFiat
810
import com.getcode.services.flipcash.BuildConfig
911
import com.getcode.utils.TraceType
12+
import com.getcode.utils.getPublicKeyBase58
1013
import com.getcode.utils.trace
1114
import com.google.firebase.ktx.Firebase
1215
import com.google.firebase.perf.ktx.performance
1316
import com.google.firebase.perf.metrics.Trace
1417
import com.mixpanel.android.mpmetrics.MixpanelAPI
1518
import org.json.JSONObject
16-
import timber.log.Timber
1719
import javax.inject.Inject
1820

1921
interface FlipcashAnalyticsService : AnalyticsService {
2022
fun transfer(
21-
event: AnalyticsEvent,
23+
event: AnalyticsEvent.Transfer,
2224
amount: LocalFiat? = null,
2325
successful: Boolean = true,
2426
error: Throwable? = null
2527
)
2628
fun transfer(
27-
event: AnalyticsEvent,
29+
event: AnalyticsEvent.Transfer,
2830
fiat: Fiat? = null,
2931
successful: Boolean = true,
3032
error: Throwable? = null
3133
)
34+
35+
fun paidForAccount(
36+
price: Double,
37+
currency: CurrencyCode,
38+
owner: KeyPair,
39+
)
3240
}
3341

3442
class FlipcashAnalyticsManager @Inject constructor(
@@ -63,16 +71,22 @@ class FlipcashAnalyticsManager @Inject constructor(
6371

6472
override fun action(action: AppAction, source: AppActionSource?) = Unit
6573

66-
override fun transfer(event: AnalyticsEvent, amount: LocalFiat?, successful: Boolean, error: Throwable?) {
74+
override fun transfer(event: AnalyticsEvent.Transfer, amount: LocalFiat?, successful: Boolean, error: Throwable?) {
6775
val properties = event.properties(localizedAmount = amount, successful = successful, error = error)
6876
track(event.name, *properties.toList().toTypedArray())
6977
}
7078

71-
override fun transfer(event: AnalyticsEvent, fiat: Fiat?, successful: Boolean, error: Throwable?) {
79+
override fun transfer(event: AnalyticsEvent.Transfer, fiat: Fiat?, successful: Boolean, error: Throwable?) {
7280
val properties = event.properties(nativeAmount = fiat, successful = successful, error = error)
7381
track(event.name, *properties.toList().toTypedArray())
7482
}
7583

84+
override fun paidForAccount(price: Double, currency: CurrencyCode, owner: KeyPair) {
85+
val event = AnalyticsEvent.PaidForAccount(price, currency, owner)
86+
val properties = event.properties()
87+
track(event.name, *properties.toList().toTypedArray())
88+
}
89+
7690
private fun track(name: String, vararg properties: Pair<String, String>) {
7791
if (BuildConfig.DEBUG) {
7892
trace(
@@ -90,25 +104,51 @@ class FlipcashAnalyticsManager @Inject constructor(
90104
}
91105
}
92106

93-
sealed class AnalyticsEvent(val name: String) {
94-
data object GrabBill : AnalyticsEvent("Grab Bill")
95-
data object GiveBill : AnalyticsEvent("Give Bill")
96-
data object Withdrawal : AnalyticsEvent("Withdrawal")
97-
data class SentCashLink(val clipboard: Boolean? = null, val app: String? = null) : AnalyticsEvent("Sent Cash Link")
98-
data object ClaimedCashLink : AnalyticsEvent("Receive Cash Link")
107+
sealed interface AnalyticsEvent {
108+
109+
val name: String
110+
data class PaidForAccount(
111+
val price: Double,
112+
val currency: CurrencyCode,
113+
val owner: KeyPair
114+
) : AnalyticsEvent {
115+
override val name: String = "Create Account Payment"
116+
}
117+
118+
sealed interface Transfer : AnalyticsEvent
119+
data object GrabBill : Transfer {
120+
override val name: String = "Grab Bill"
121+
}
122+
data object GiveBill : Transfer {
123+
override val name: String = "Give Bill"
124+
}
125+
data object Withdrawal : Transfer {
126+
override val name: String = "Withdrawal"
127+
}
128+
data class SentCashLink(
129+
val clipboard: Boolean? = null,
130+
val app: String? = null
131+
) : Transfer {
132+
override val name: String = "Send Cash Link"
133+
}
134+
data object ClaimedCashLink : Transfer {
135+
override val name: String = "Receive Cash Link"
136+
}
99137
}
100138

101139
private fun AnalyticsEvent.properties(
102140
localizedAmount: LocalFiat? = null,
103141
nativeAmount: Fiat? = null,
104-
successful: Boolean,
105-
error: Throwable?
142+
successful: Boolean? = null,
143+
error: Throwable? = null,
106144
): Map<String, String> {
107145
return buildMap {
108-
if (successful) {
109-
put("State", "Success")
110-
} else {
111-
put("State", "Failure")
146+
if (successful != null) {
147+
if (successful) {
148+
put("State", "Success")
149+
} else {
150+
put("State", "Failure")
151+
}
112152
}
113153

114154
when (val event = this@properties) {
@@ -124,6 +164,12 @@ private fun AnalyticsEvent.properties(
124164
AnalyticsEvent.ClaimedCashLink,
125165
AnalyticsEvent.GiveBill,
126166
AnalyticsEvent.GrabBill -> Unit
167+
168+
is AnalyticsEvent.PaidForAccount -> {
169+
put("Fiat", event.price.toString())
170+
put("Currency", event.currency.name)
171+
put("Owner Public Key", event.owner.getPublicKeyBase58())
172+
}
127173
}
128174

129175
if (localizedAmount != null) {

services/flipcash/src/main/kotlin/com/flipcash/services/inject/FlipcashModule.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.flipcash.services.inject
22

33
import android.content.Context
4+
import com.flipcash.services.analytics.FlipcashAnalyticsService
45
import com.flipcash.services.billing.BillingClient
56
import com.flipcash.services.internal.annotations.FlipcashManagedChannel
67
import com.flipcash.services.internal.annotations.FlipcashProtocol
@@ -109,5 +110,6 @@ internal object FlipcashModule {
109110
@ApplicationContext context: Context,
110111
repository: PurchaseRepository,
111112
userManager: UserManager,
112-
): BillingClient = GooglePlayBillingClient(context, userManager, repository)
113+
analytics: FlipcashAnalyticsService
114+
): BillingClient = GooglePlayBillingClient(context, userManager, repository, analytics)
113115
}

services/flipcash/src/main/kotlin/com/flipcash/services/internal/billing/GooglePlayBillingClient.kt

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@ import com.android.billingclient.api.QueryProductDetailsParams
2020
import com.android.billingclient.api.QueryPurchasesParams
2121
import com.android.billingclient.api.acknowledgePurchase
2222
import com.android.billingclient.api.consumePurchase
23+
import com.flipcash.services.analytics.FlipcashAnalyticsService
2324
import com.flipcash.services.billing.BillingClient
2425
import com.flipcash.services.billing.BillingClientState
2526
import com.flipcash.services.billing.IapPaymentError
2627
import com.flipcash.services.billing.IapPaymentEvent
2728
import com.flipcash.services.billing.IapProduct
2829
import com.flipcash.services.billing.ProductPrice
30+
import com.flipcash.services.internal.extensions.asPublicKey
2931
import com.flipcash.services.internal.model.billing.IapMetadata
3032
import com.flipcash.services.internal.model.billing.Receipt
3133
import com.flipcash.services.repository.PurchaseRepository
@@ -58,7 +60,8 @@ import com.android.billingclient.api.BillingClient as GooglePlayBillingClient
5860
internal class GooglePlayBillingClient(
5961
@ApplicationContext context: Context,
6062
private val userManager: UserManager,
61-
private val purchaseRepository: PurchaseRepository
63+
private val purchaseRepository: PurchaseRepository,
64+
private val analytics: FlipcashAnalyticsService,
6265
) : BillingClient, PurchasesUpdatedListener {
6366

6467
companion object {
@@ -209,7 +212,7 @@ internal class GooglePlayBillingClient(
209212
val receipt = Receipt(item.purchaseToken)
210213

211214
printLog(
212-
message = "completing purchasen",
215+
message = "completing purchase",
213216
metadata = {
214217
"token" to item.purchaseToken
215218
"product" to productId
@@ -219,15 +222,22 @@ internal class GooglePlayBillingClient(
219222
}
220223
)
221224

225+
226+
val owner = userManager.accountCluster?.authority?.keyPair!!
222227
purchaseRepository.onPurchaseCompleted(
223-
owner = userManager.accountCluster?.authority?.keyPair!!,
228+
owner = owner,
224229
receipt = receipt,
225230
metadata = IapMetadata(
226231
product = productId,
227232
amount = purchasePrice.amount,
228233
currency = purchasePrice.currency
229234
)
230235
).onSuccess {
236+
analytics.paidForAccount(
237+
price = purchasePrice.amount,
238+
currency = purchasePrice.currency,
239+
owner = owner,
240+
)
231241
acknowledgeOrConsume(item)
232242
}.onFailure {
233243
val cause = if (isFromRestore) SuppressibleException(it) else it

0 commit comments

Comments
 (0)