diff --git a/main/java/com/amplitude/Amplitude.kt b/main/java/com/amplitude/Amplitude.kt index 846eb903..363950cd 100644 --- a/main/java/com/amplitude/Amplitude.kt +++ b/main/java/com/amplitude/Amplitude.kt @@ -3,6 +3,7 @@ package com.amplitude import com.amplitude.events.BaseEvent import com.amplitude.events.Identify import com.amplitude.events.Revenue +import com.amplitude.events.RevenueEvent import com.amplitude.platform.Plugin import com.amplitude.platform.Timeline import com.amplitude.platform.plugins.AmplitudeDestination @@ -75,8 +76,22 @@ open class Amplitude internal constructor( revenue(revenue) } + /** + * Create a Revenue object to hold your revenue data and properties, + * and log it as a revenue event using this method. + */ fun revenue(revenue: Revenue) { + if (!revenue.isValid()) { + return + } + revenue(revenue.toRevenueEvent()) + } + /** + * Log a Revenue Event + */ + fun revenue(event: RevenueEvent) { + process(event) } fun process(event: BaseEvent) { diff --git a/main/java/com/amplitude/events/BaseEvent.kt b/main/java/com/amplitude/events/BaseEvent.kt index de3c7ef4..d034b9c2 100644 --- a/main/java/com/amplitude/events/BaseEvent.kt +++ b/main/java/com/amplitude/events/BaseEvent.kt @@ -3,7 +3,7 @@ package com.amplitude.events import org.json.JSONObject open class BaseEvent : EventOptions() { - lateinit var eventType: String + open lateinit var eventType: String var eventProperties: JSONObject? = null var userProperties: JSONObject? = null var groups:JSONObject? = null diff --git a/main/java/com/amplitude/events/Revenue.kt b/main/java/com/amplitude/events/Revenue.kt index 4b0bf378..e745c19b 100644 --- a/main/java/com/amplitude/events/Revenue.kt +++ b/main/java/com/amplitude/events/Revenue.kt @@ -1,4 +1,113 @@ package com.amplitude.events +import org.json.JSONException +import org.json.JSONObject + class Revenue { -} \ No newline at end of file + /** + * The Product ID field. + */ + var productId: String? = null + set(value) { + if (!value.isNullOrEmpty()) { + field = value + } + } + + /** + * The Quantity field (defaults to 1). + */ + var quantity: Int = 1 + set(value) { + if (value > 0) { + field = value + } + } + + /** + * The Price field (required). + */ + var price: Double? = null + set(value) { + value?.let { + field = value + } + } + + /** + * The Revenue Type field (optional). + */ + var revenueType: String? = null + + /** + * The Receipt field (required if you want to verify the revenue event). + */ + private var receipt: String? = null + + /** + * The Receipt Signature field (required if you want to verify the revenue event). + */ + private var receiptSig: String? = null + + /** + * The Revenue Event Properties field with (optional). + */ + var properties: JSONObject? = null + + /** + * The revenue amount + */ + var revenue: Double? = null + set(value) { + value?.let { + field = value + } + } + + companion object { + internal const val REVENUE_PRODUCT_ID = "\$productId" + internal const val REVENUE_QUANTITY = "\$quantity" + internal const val REVENUE_PRICE = "\$price" + internal const val REVENUE_TYPE = "\$revenueType" + internal const val REVENUE_RECEIPT = "\$receipt" + internal const val REVENUE_RECEIPT_SIG = "\$receiptSig" + internal const val REVENUE = "\$revenue" + } + + /** + * Set the receipt and receipt signature. Both fields are required to verify the revenue event. + */ + fun setReceipt(receipt: String, receiptSignature: String): Revenue { + this.receipt = receipt + receiptSig = receiptSignature + return this + } + + /** + * Verifies that revenue object is valid and contains the required fields + */ + fun isValid(): Boolean { + if (price == null) { + return false + } + return true + } + + fun toRevenueEvent(): RevenueEvent { + val event = RevenueEvent() + val eventProperties = properties ?: JSONObject() + try { + eventProperties.put(REVENUE_PRODUCT_ID, productId) + eventProperties.put(REVENUE_QUANTITY, quantity) + eventProperties.put(REVENUE_PRICE, price) + eventProperties.put(REVENUE_TYPE, revenueType) + eventProperties.put(REVENUE_RECEIPT, receipt) + eventProperties.put(REVENUE_RECEIPT_SIG, receiptSig) + eventProperties.put(REVENUE, revenue) + } catch (e: JSONException) { + e.printStackTrace() + } + event.eventProperties = eventProperties + return event + } +} diff --git a/main/java/com/amplitude/events/RevenueEvent.kt b/main/java/com/amplitude/events/RevenueEvent.kt index bdc96d2d..2404a0d8 100644 --- a/main/java/com/amplitude/events/RevenueEvent.kt +++ b/main/java/com/amplitude/events/RevenueEvent.kt @@ -1,4 +1,7 @@ package com.amplitude.events +import com.amplitude.Constants + class RevenueEvent : BaseEvent() { + override var eventType = Constants.AMP_REVENUE_EVENT } \ No newline at end of file