-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathAppsFlyerKit.kt
More file actions
393 lines (335 loc) · 14.4 KB
/
AppsFlyerKit.kt
File metadata and controls
393 lines (335 loc) · 14.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
package com.mparticle.kits
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.location.Location
import android.os.Bundle
import com.appsflyer.AFInAppEventParameterName.CONTENT_ID
import com.appsflyer.AFInAppEventParameterName.CONTENT_TYPE
import com.appsflyer.AFInAppEventParameterName.CURRENCY
import com.appsflyer.AFInAppEventParameterName.PRICE
import com.appsflyer.AFInAppEventParameterName.QUANTITY
import com.appsflyer.AFInAppEventParameterName.REVENUE
import com.appsflyer.AFInAppEventType
import com.appsflyer.AFInAppEventType.ADD_TO_CART
import com.appsflyer.AFInAppEventType.ADD_TO_WISH_LIST
import com.appsflyer.AFInAppEventType.INITIATED_CHECKOUT
import com.appsflyer.AFInAppEventType.PURCHASE
import com.appsflyer.AppsFlyerConversionListener
import com.appsflyer.AppsFlyerLib
import com.appsflyer.AppsFlyerProperties
import com.appsflyer.deeplink.DeepLinkListener
import com.appsflyer.deeplink.DeepLinkResult
import com.mparticle.AttributionError
import com.mparticle.AttributionResult
import com.mparticle.MPEvent
import com.mparticle.MParticle
import com.mparticle.commerce.CommerceEvent
import com.mparticle.commerce.Product
import com.mparticle.internal.Logger
import com.mparticle.internal.MPUtility
import org.json.JSONException
import org.json.JSONObject
import java.math.BigDecimal
import java.util.LinkedList
/**
* mParticle Kit wrapper for the AppsFlyer SDK
*/
class AppsFlyerKit : KitIntegration(), KitIntegration.EventListener,
KitIntegration.AttributeListener, KitIntegration.CommerceListener,
AppsFlyerConversionListener, KitIntegration.ActivityListener {
override fun getInstance(): AppsFlyerLib = AppsFlyerLib.getInstance();
override fun getName() = NAME
public override fun onKitCreate(
setting: Map<String?, String?>?,
context: Context
): List<ReportingMessage> {
AppsFlyerLib.getInstance()
.setDebugLog(MParticle.getInstance()?.environment == MParticle.Environment.Development)
settings[DEV_KEY]?.let { AppsFlyerLib.getInstance().init(it, this, context) }
AppsFlyerLib.getInstance().start(context.applicationContext)
AppsFlyerLib.getInstance().setCollectAndroidID(MParticle.isAndroidIdEnabled())
val integrationAttributes = HashMap<String, String?>(1)
integrationAttributes[APPSFLYERID_INTEGRATION_KEY] =
AppsFlyerLib.getInstance().getAppsFlyerUID(context)
setIntegrationAttributes(integrationAttributes)
AppsFlyerLib.getInstance().subscribeForDeepLink(deepLinkListener())
val messages: MutableList<ReportingMessage> = ArrayList()
messages.add(
ReportingMessage(
this,
ReportingMessage.MessageType.APP_STATE_TRANSITION,
System.currentTimeMillis(),
null
)
)
return messages
}
override fun leaveBreadcrumb(breadcrumb: String): List<ReportingMessage> = emptyList()
override fun logError(
message: String,
eventData: Map<String, String>
): List<ReportingMessage> = emptyList()
override fun logException(
exception: Exception,
eventData: Map<String, String>,
message: String
): List<ReportingMessage> = emptyList()
override fun logLtvIncrease(
valueIncreased: BigDecimal,
valueTotal: BigDecimal,
eventName: String,
contextInfo: Map<String, String>
): List<ReportingMessage> = emptyList()
override fun logEvent(event: CommerceEvent): List<ReportingMessage> {
val messages: MutableList<ReportingMessage> = LinkedList()
val eventValues: MutableMap<String, Any?> = HashMap()
val productList = event.products
if (isSalesEvent(event)) {
logSalesEvent(event, eventValues, productList, messages)
} else {
logNotSalesEvent(event, messages)
}
return messages
}
private fun logNotSalesEvent(
event: CommerceEvent,
messages: MutableList<ReportingMessage>
) {
val eventList = CommerceEventUtils.expand(event)
if (eventList.isNotEmpty()) {
for (e in eventList) {
try {
logEvent(e)
messages.add(ReportingMessage.fromEvent(this, event))
} catch (e: Exception) {
Logger.warning("Failed to call logCustomEvent to AppsFlyer kit: $e")
}
}
}
}
private fun logSalesEvent(
event: CommerceEvent,
eventValues: MutableMap<String, Any?>,
productList: MutableList<Product>?,
messages: MutableList<ReportingMessage>
) {
event.customAttributes?.let { eventValues.putAll(it) }
if (!KitUtils.isEmpty(event.currency)) {
eventValues[CURRENCY] = event.currency
}
if (event.productAction == Product.ADD_TO_CART
|| event.productAction == Product.ADD_TO_WISHLIST
) {
val eventName =
if (event.productAction == Product.ADD_TO_CART) ADD_TO_CART
else ADD_TO_WISH_LIST
productList?.iterator()?.forEach { product ->
val productEventValues: MutableMap<String, Any?> = hashMapOf()
productEventValues.putAll(eventValues)
with(product) {
productEventValues[PRICE] = unitPrice
productEventValues[QUANTITY] = quantity
if (!KitUtils.isEmpty(sku)) {
productEventValues[CONTENT_ID] = sku
}
if (!KitUtils.isEmpty(category)) {
productEventValues[CONTENT_TYPE] = category
}
}
instance.logEvent(context, eventName, productEventValues)
messages.add(ReportingMessage.fromEvent(this, event))
}
} else {
val eventName =
if (event.productAction == Product.CHECKOUT)
INITIATED_CHECKOUT else PURCHASE
eventValues[CONTENT_ID] = generateProductIdList(event)
if (!productList.isNullOrEmpty()) {
var totalQuantity = 0.0
for (product in productList) {
totalQuantity += product.quantity
}
eventValues[QUANTITY] = totalQuantity
}
val transactionAttributes = event.transactionAttributes
if ((transactionAttributes != null) && (transactionAttributes.revenue != 0.0)) {
val revenue = transactionAttributes.revenue
if (event.productAction == Product.PURCHASE) {
eventValues[REVENUE] = revenue
if (!MPUtility.isEmpty(transactionAttributes.id)) {
eventValues[AFInAppEventType.ORDER_ID] = transactionAttributes.id
}
} else {
eventValues[PRICE] = revenue
}
}
instance.logEvent(context, eventName, eventValues)
messages.add(ReportingMessage.fromEvent(this, event))
}
}
private fun isSalesEvent(event: CommerceEvent) =
(event.productAction == Product.ADD_TO_CART
|| event.productAction == Product.ADD_TO_WISHLIST
|| event.productAction == Product.CHECKOUT
|| event.productAction == Product.PURCHASE)
override fun logEvent(event: MPEvent): List<ReportingMessage> {
var hashMap: HashMap<String?, Any?>? = hashMapOf()
if (event.customAttributes?.isNotEmpty() == true) {
hashMap = event.customAttributes?.let { HashMap(it) }
}
instance.logEvent(context, event.eventName, hashMap)
val messages: MutableList<ReportingMessage> = LinkedList()
messages.add(ReportingMessage.fromEvent(this, event))
return messages
}
override fun logScreen(
screenName: String,
eventAttributes: Map<String, String>
): List<ReportingMessage> = emptyList()
override fun setOptOut(optOutStatus: Boolean): List<ReportingMessage> {
instance.anonymizeUser(optOutStatus)
val messageList: MutableList<ReportingMessage> = LinkedList()
messageList.add(
ReportingMessage(
this,
ReportingMessage.MessageType.OPT_OUT,
System.currentTimeMillis(),
null
).setOptOut(optOutStatus)
)
return messageList
}
override fun setUserAttribute(attributeKey: String, attributeValue: String) {}
override fun setUserAttributeList(s: String, list: List<String>) {}
override fun supportsAttributeLists(): Boolean = true
override fun setAllUserAttributes(map: Map<String, String>, map1: Map<String, List<String>>) {}
override fun removeUserAttribute(key: String) {}
override fun removeUserIdentity(identityType: MParticle.IdentityType) {
with(instance) {
if (MParticle.IdentityType.CustomerId == identityType) {
setCustomerUserId("")
} else if (MParticle.IdentityType.Email == identityType) {
setUserEmails(AppsFlyerProperties.EmailsCryptType.NONE, "")
}
}
}
override fun setUserIdentity(identityType: MParticle.IdentityType, identity: String) {
with(instance) {
if (MParticle.IdentityType.CustomerId == identityType) {
setCustomerUserId(identity)
} else if (MParticle.IdentityType.Email == identityType) {
setUserEmails(AppsFlyerProperties.EmailsCryptType.NONE, identity)
}
}
}
override fun logout(): List<ReportingMessage> = emptyList()
override fun onConversionDataSuccess(conversionDataN: MutableMap<String, Any>?) {
var conversionData = conversionDataN
val jsonResult = JSONObject()
if (conversionData == null) {
conversionData = hashMapOf()
}
conversionData[INSTALL_CONVERSION_RESULT] = "true"
for ((key, value) in conversionData) {
try {
jsonResult.put(key, value)
} catch (e: JSONException) {
}
}
val result = AttributionResult()
.setParameters(jsonResult)
.setServiceProviderId(configuration.kitId)
kitManager.onResult(result)
}
override fun onConversionDataFail(conversionFailure: String) {
if (!KitUtils.isEmpty(conversionFailure)) {
val error = AttributionError()
.setMessage(conversionFailure)
.setServiceProviderId(configuration.kitId)
kitManager.onError(error)
}
}
fun deepLinkListener() = DeepLinkListener { deepLinkResult ->
val deepLinkObj = deepLinkResult.deepLink
when (deepLinkResult.status) {
DeepLinkResult.Status.FOUND -> {
try {
deepLinkObj.clickEvent.put(APP_OPEN_ATTRIBUTION_RESULT, true.toString())
val result = AttributionResult()
.setParameters(deepLinkObj.clickEvent)
.setServiceProviderId(configuration.kitId)
kitManager.onResult(result)
} catch (e: Exception) {
return@DeepLinkListener
}
}
DeepLinkResult.Status.NOT_FOUND -> {
return@DeepLinkListener
}
else -> {
val dlError = deepLinkResult.error
if (!KitUtils.isEmpty(dlError.toString())) {
val error = AttributionError()
.setMessage(dlError.toString())
.setServiceProviderId(configuration.kitId)
kitManager.onError(error)
}
return@DeepLinkListener
}
}
}
override fun onAppOpenAttribution(attributionDataN: MutableMap<String, String>?) {
//do nothing, Appsflyer new UDL implementation will use new deep linking method with both
// custom URI and appsflyer's Onelink
}
override fun onAttributionFailure(attributionFailure: String) {
//do nothing, Appsflyer new UDL implementation will use new deep linking method with both
// custom URI and appsflyer's Onelink
}
override fun setInstallReferrer(intent: Intent) {
//do nothing, Appsflyer will fetch the install referrer data internally,
// as long as the proper Play Install Referrer dependency is present.
}
override fun setLocation(location: Location) {
instance.logLocation(context, location.latitude, location.longitude)
}
override fun onActivityCreated(
activity: Activity,
bundle: Bundle?
): List<ReportingMessage> {
instance.start(activity)
return emptyList()
}
override fun onActivityStarted(activity: Activity): List<ReportingMessage> = emptyList()
override fun onActivityResumed(activity: Activity): List<ReportingMessage> = emptyList()
override fun onActivityPaused(activity: Activity): List<ReportingMessage> = emptyList()
override fun onActivityStopped(activity: Activity): List<ReportingMessage> = emptyList()
override fun onActivitySaveInstanceState(
activity: Activity,
bundle: Bundle?
): List<ReportingMessage> = emptyList()
override fun onActivityDestroyed(activity: Activity): List<ReportingMessage> = emptyList()
companion object {
const val DEV_KEY = "devKey"
const val APPSFLYERID_INTEGRATION_KEY = "appsflyer_id_integration_setting"
const val NAME = "AppsFlyer"
const val COMMA = ","
/**
* This key will be present when returning a result from AppsFlyer's onInstallConversionDataLoaded API
*/
const val INSTALL_CONVERSION_RESULT = "MPARTICLE_APPSFLYER_INSTALL_CONVERSION_RESULT"
/**
* This key will be present when returning a result from AppsFlyer's onAppOpenAttribution API
*/
const val APP_OPEN_ATTRIBUTION_RESULT =
"MPARTICLE_APPSFLYER_APP_OPEN_ATTRIBUTION_RESULT"
fun generateProductIdList(event: CommerceEvent?): List<String>? {
return event?.products?.filter { !KitUtils.isEmpty(it.sku) }?.let {
if (it.isNotEmpty()) {
it.map { it.sku.replace(COMMA, "%2C") }
} else { null }
}
}
}
}