Skip to content

Commit

Permalink
feat: add revenue helper class (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
qingzhuozhen authored Feb 19, 2022
1 parent ae825d6 commit 341fcf1
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 2 deletions.
15 changes: 15 additions & 0 deletions main/java/com/amplitude/Amplitude.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion main/java/com/amplitude/events/BaseEvent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
111 changes: 110 additions & 1 deletion main/java/com/amplitude/events/Revenue.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,113 @@
package com.amplitude.events

import org.json.JSONException
import org.json.JSONObject

class Revenue {
}
/**
* 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
}
}
3 changes: 3 additions & 0 deletions main/java/com/amplitude/events/RevenueEvent.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
package com.amplitude.events

import com.amplitude.Constants

class RevenueEvent : BaseEvent() {
override var eventType = Constants.AMP_REVENUE_EVENT
}

0 comments on commit 341fcf1

Please sign in to comment.