Skip to content

fix: adding ability to track revenue currency #244

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/src/main/java/com/amplitude/core/events/BaseEvent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ open class BaseEvent : EventOptions() {
options.quantity?.let { quantity = it }
options.productId?.let { productId = it }
options.revenueType?.let { revenueType = it }
options.currency?.let { currency = it }
options.extra?.let { extra = it }
options.callback?.let { callback = it }
options.partnerId?.let { partnerId = it }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ open class EventOptions {
var quantity: Int? = null
var productId: String? = null
var revenueType: String? = null
var currency: String? = null
var extra: Map<String, Any>? = null
var callback: EventCallBack? = null
var partnerId: String? = null
Expand Down
7 changes: 7 additions & 0 deletions core/src/main/java/com/amplitude/core/events/Revenue.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ open class Revenue {
*/
var revenueType: String? = null

/**
* The 3 letter currency code (optional).
*/
var currency: String? = null

/**
* The Receipt field (required if you want to verify the revenue event).
*/
Expand Down Expand Up @@ -66,6 +71,7 @@ open class Revenue {
internal const val REVENUE_QUANTITY = "\$quantity"
internal const val REVENUE_PRICE = "\$price"
internal const val REVENUE_TYPE = "\$revenueType"
internal const val REVENUE_CURRENCY = "\$currency"
internal const val REVENUE_RECEIPT = "\$receipt"
internal const val REVENUE_RECEIPT_SIG = "\$receiptSig"
internal const val REVENUE = "\$revenue"
Expand Down Expand Up @@ -97,6 +103,7 @@ open class Revenue {
eventProperties.put(REVENUE_QUANTITY, quantity)
price?.let { eventProperties.put(REVENUE_PRICE, it) }
revenueType?.let { eventProperties.put(REVENUE_TYPE, it) }
currency?.let { eventProperties.put(REVENUE_CURRENCY, it) }
receipt?.let { eventProperties.put(REVENUE_RECEIPT, it) }
receiptSig?.let { eventProperties.put(REVENUE_RECEIPT_SIG, it) }
revenue?.let { eventProperties.put(REVENUE, it) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ object JSONUtil {
eventJSON.addValue("revenue", event.revenue)
eventJSON.addValue("productId", event.productId)
eventJSON.addValue("revenueType", event.revenueType)
eventJSON.addValue("currency", event.currency)
eventJSON.addValue("location_lat", event.locationLat)
eventJSON.addValue("location_lng", event.locationLng)
eventJSON.addValue("ip", event.ip)
Expand Down
21 changes: 21 additions & 0 deletions core/src/test/kotlin/com/amplitude/core/events/RevenueTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,23 @@ class RevenueTest {
assertEquals(revenue.revenueType, revenueType)
}

@Test
fun testRevenueCurrency() {
val revenue = Revenue()
assertEquals(revenue.currency, null)
val currency = "CAD"
revenue.currency = currency
assertEquals(revenue.currency, currency)

// verify that null and empty strings allowed
revenue.currency = null
assertNull(revenue.currency)
revenue.currency = ""
assertEquals(revenue.currency, "")
revenue.currency = currency
assertEquals(revenue.currency, currency)
}

@Test
fun testReceipt() {
val revenue = Revenue()
Expand All @@ -79,11 +96,13 @@ class RevenueTest {
revenue.revenueType = "testRevenueType"
revenue.quantity = 20
revenue.productId = "testProductId"
revenue.currency = "CAD"
val event = revenue.toRevenueEvent()
assertEquals(19.99, event.eventProperties?.get(Revenue.REVENUE_PRICE))
assertEquals("testRevenueType", event.eventProperties?.get(Revenue.REVENUE_TYPE))
assertEquals(20, event.eventProperties?.get(Revenue.REVENUE_QUANTITY))
assertEquals("testProductId", event.eventProperties?.get(Revenue.REVENUE_PRODUCT_ID))
assertEquals("CAD", event.eventProperties?.get(Revenue.REVENUE_CURRENCY))
}

@Test
Expand All @@ -93,13 +112,15 @@ class RevenueTest {
revenue.revenueType = "testRevenueType"
revenue.quantity = 20
revenue.productId = "testProductId"
revenue.currency = "CAD"
val receipt = "testReceipt"
val receiptSig = "testReceiptSig"
revenue.setReceipt(receipt, receiptSig)
revenue.properties = mutableMapOf(Pair("city", "san francisco"))
val event = revenue.toRevenueEvent()
assertEquals(receipt, event.eventProperties?.get(Revenue.REVENUE_RECEIPT))
assertEquals(receiptSig, event.eventProperties?.get(Revenue.REVENUE_RECEIPT_SIG))
assertEquals("CAD", event.eventProperties?.get(Revenue.REVENUE_CURRENCY))
assertEquals("san francisco", event.eventProperties?.get("city"))
}

Expand Down
Loading