Skip to content

Commit

Permalink
feat: add identify support (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
qingzhuozhen authored Mar 29, 2022
1 parent e25ecd4 commit 2f86adc
Show file tree
Hide file tree
Showing 11 changed files with 698 additions and 28 deletions.
71 changes: 63 additions & 8 deletions core/src/main/java/com/amplitude/core/Amplitude.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ package com.amplitude.core

import com.amplitude.common.Logger
import com.amplitude.core.events.BaseEvent
import com.amplitude.core.events.EventOptions
import com.amplitude.core.events.GroupIdentifyEvent
import com.amplitude.core.events.Identify
import com.amplitude.core.events.IdentifyEvent
import com.amplitude.core.events.Revenue
import com.amplitude.core.events.RevenueEvent
import com.amplitude.core.platform.EventPlugin
import com.amplitude.core.platform.ObservePlugin
import com.amplitude.core.platform.Plugin
import com.amplitude.core.platform.Timeline
Expand Down Expand Up @@ -68,18 +72,33 @@ open class Amplitude internal constructor(
track(event)
}

fun track(event: BaseEvent, callback: ((BaseEvent) -> Unit)? = null) {
fun track(event: BaseEvent, callback: EventCallBack? = null) {
callback ?. let {
event.callback = it
}
process(event)
}

fun track(eventType: String, eventProperties: JSONObject? = null) {
fun track(eventType: String, eventProperties: JSONObject? = null, options: EventOptions? = null) {
val event = BaseEvent()
event.eventType = eventType
event.eventProperties = eventProperties
options ?. let {
event.mergeEventOptions(it)
}
process(event)
}

fun identify(identify: Identify) {
/**
* Identify. Use this to send an Identify object containing user property operations to Amplitude server.
*/
fun identify(identify: Identify, options: EventOptions? = null) {
val event = IdentifyEvent()
event.userProperties = identify.properties
options ?. let {
event.mergeEventOptions(it)
}
process(event)
}

fun identify(userId: String) {
Expand All @@ -90,10 +109,36 @@ open class Amplitude internal constructor(
this.idContainer.identityManager.editIdentity().setDeviceId(deviceId).commit()
}

fun groupIdentify(identify: Identify) {
fun groupIdentify(groupType: String, groupName: String, identify: Identify, options: EventOptions? = null) {
val event = GroupIdentifyEvent()
var group: JSONObject? = null
try {
group = JSONObject().put(groupType, groupName)
} catch (e: Exception) {
logger.error(e.toString())
}
event.groups = group
event.groupProperties = identify.properties
options ?. let {
event.mergeEventOptions(it)
}
process(event)
}

/**
* Sets the user's group.
*/
fun setGroup(groupType: String, groupName: String, options: EventOptions? = null) {
val identify = Identify().set(groupType, groupName)
identify(identify, options)
}

fun setGroup(groupType: String, groupName: Array<String>) {
/**
* ets the user's groups.
*/
fun setGroup(groupType: String, groupName: Array<String>, options: EventOptions? = null) {
val identify = Identify().set(groupType, groupName)
identify(identify, options)
}

@Deprecated("Please use 'revenue' instead.", ReplaceWith("revenue"))
Expand All @@ -105,11 +150,15 @@ open class Amplitude internal constructor(
* 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) {
fun revenue(revenue: Revenue, options: EventOptions? = null) {
if (!revenue.isValid()) {
return
}
revenue(revenue.toRevenueEvent())
val event = revenue.toRevenueEvent()
options ?. let {
event.mergeEventOptions(it)
}
revenue(event)
}

/**
Expand All @@ -119,7 +168,10 @@ open class Amplitude internal constructor(
process(event)
}

fun process(event: BaseEvent) {
private fun process(event: BaseEvent) {
if (configuration.optOut) {
logger.info("Skip event for opt out config.")
}
amplitudeScope.launch(amplitudeDispatcher) {
timeline.process(event)
}
Expand Down Expand Up @@ -150,5 +202,8 @@ open class Amplitude internal constructor(
}

fun flush() {
this.timeline.applyClosure {
(it as? EventPlugin)?.flush()
}
}
}
2 changes: 1 addition & 1 deletion core/src/main/java/com/amplitude/core/Configuration.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ open class Configuration(
val flushQueueSize: Int = FLUSH_QUEUE_SIZE,
val flushIntervalMillis: Int = FLUSH_INTERVAL_MILLIS,
val instanceName: String = DEFAULT_INSTANCE,
val optOut: Boolean = false,
var optOut: Boolean = false,
val storageProvider: StorageProvider = InMemoryStorageProvider(),
val loggerProvider: LoggerProvider = ConsoleLoggerProvider(),
val minIdLength: Int? = null,
Expand Down
40 changes: 40 additions & 0 deletions core/src/main/java/com/amplitude/core/events/BaseEvent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,44 @@ open class BaseEvent : EventOptions() {
var userProperties: JSONObject? = null
var groups: JSONObject? = null
var groupProperties: JSONObject? = null

fun mergeEventOptions(options: EventOptions) {
userId ?: let { userId = options.userId }
deviceId ?: let { deviceId = options.deviceId }
timestamp ?: let { timestamp = options.timestamp }
eventId ?: let { eventId = options.eventId }
insertId ?: let { insertId = options.insertId }
locationLat ?: let { locationLat = options.locationLat }
locationLng ?: let { locationLng = options.locationLng }
appVersion ?: let { appVersion = options.appVersion }
versionName ?: let { versionName = options.versionName }
platform ?: let { platform = options.platform }
osName ?: let { osName = options.osName }
osVersion ?: let { osVersion = options.osVersion }
deviceBrand ?: let { deviceBrand = options.deviceBrand }
deviceManufacturer ?: let { deviceManufacturer = options.deviceManufacturer }
deviceModel ?: let { deviceModel = options.deviceModel }
carrier ?: let { carrier = options.carrier }
country ?: let { country = options.country }
region ?: let { region = options.region }
city ?: let { city = options.city }
dma ?: let { dma = options.dma }
idfa ?: let { idfa = options.idfa }
idfv ?: let { idfv = options.idfv }
adid ?: let { adid = options.adid }
appSetId ?: let { appSetId = options.appSetId }
androidId ?: let { androidId = options.androidId }
language ?: let { language = options.language }
library ?: let { library = options.library }
ip ?: let { ip = options.ip }
plan ?: let { plan = options.plan }
revenue ?: let { revenue = options.revenue }
price ?: let { price = options.price }
quantity ?: let { quantity = options.quantity }
productId ?: let { productId = options.productId }
revenueType ?: let { revenueType = options.revenueType }
extra ?: let { extra = options.extra }
callback ?: let { callback = options.callback }
partnerId ?: let { partnerId = options.partnerId }
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
package com.amplitude.core.events

class GroupIdentifyEvent : BaseEvent()
import com.amplitude.core.Constants

class GroupIdentifyEvent : BaseEvent() {
override var eventType = Constants.GROUP_IDENTIFY_EVENT
}
Loading

0 comments on commit 2f86adc

Please sign in to comment.