-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathAppsFlyerKit.kt
More file actions
649 lines (568 loc) · 23 KB
/
AppsFlyerKit.kt
File metadata and controls
649 lines (568 loc) · 23 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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
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.AppsFlyerConsent
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.consent.ConsentState
import com.mparticle.internal.Logger
import com.mparticle.internal.MPUtility
import org.json.JSONArray
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,
KitIntegration.UserAttributeListener {
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) }
setting?.get(SHARING_FILTER_FOR_PARTNERS)?.let {
applySharingFilterForPartners(it)
}
val userConsentState = currentUser?.consentState
setConsent(userConsentState)
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 onIncrementUserAttribute(
key: String?,
incrementedBy: Number?,
value: String?,
user: FilteredMParticleUser?,
) {
}
override fun onRemoveUserAttribute(
key: String?,
user: FilteredMParticleUser?,
) {
}
override fun onSetUserAttribute(
key: String?,
value: Any?,
user: FilteredMParticleUser?,
) {
}
override fun onSetUserTag(
key: String?,
user: FilteredMParticleUser?,
) {
}
override fun onSetUserAttributeList(
attributeKey: String?,
attributeValueList: MutableList<String>?,
user: FilteredMParticleUser?,
) {
}
override fun onSetAllUserAttributes(
userAttributes: MutableMap<String, String>?,
userAttributeLists: MutableMap<String, MutableList<String>>?,
user: FilteredMParticleUser?,
) {
}
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()
private fun parseToNestedMap(jsonString: String): Map<String, Any> {
val topLevelMap = mutableMapOf<String, Any>()
try {
if (jsonString.isNullOrEmpty()) {
return topLevelMap
}
val jsonObject = JSONObject(jsonString)
for (key in jsonObject.keys()) {
val value = jsonObject.get(key)
if (value is JSONObject) {
topLevelMap[key] = parseToNestedMap(value.toString())
} else {
topLevelMap[key] = value
}
}
} catch (e: Exception) {
Logger.error(
e,
"The AppsFlyer kit was unable to parse the user's ConsentState, consent may not be set correctly on the AppsFlyer SDK",
)
}
return topLevelMap
}
private fun searchKeyInNestedMap(
map: Map<*, *>,
key: Any,
): Any? {
if (map.isNullOrEmpty()) {
return null
}
try {
for ((mapKey, mapValue) in map) {
if (mapKey.toString().equals(key.toString(), ignoreCase = true)) {
return mapValue
}
if (mapValue is Map<*, *>) {
val foundValue = searchKeyInNestedMap(mapValue, key)
if (foundValue != null) {
return foundValue
}
}
}
} catch (e: Exception) {
Logger.error(
e,
"The AppsFlyer kit threw an exception while searching for the configured consent purpose mapping in the current user's consent status.",
)
}
return null
}
override fun onConsentStateUpdated(
consentState: ConsentState,
consentState1: ConsentState,
filteredMParticleUser: FilteredMParticleUser,
) {
setConsent(consentState1)
}
private fun setConsent(consentState: ConsentState?) {
if (settings[GDPR_APPLIES].isNullOrEmpty()) {
return
}
val appsFlyerGDPRUser: AppsFlyerConsent
if (!settings[GDPR_APPLIES].toBoolean()) {
appsFlyerGDPRUser = AppsFlyerConsent(false, null, null, null)
} else {
var adStorageConsentValue: Boolean? = null
when (settings[DEFAULT_AD_STORAGE_CONSENT]) {
AppsFlyerConsentValues.GRANTED.consentValue -> adStorageConsentValue = true
AppsFlyerConsentValues.DENIED.consentValue -> adStorageConsentValue = false
}
var adUserDataConsentValue: Boolean? = null
when (settings[DEFAULT_AD_USER_DATA_CONSENT]) {
AppsFlyerConsentValues.GRANTED.consentValue -> adUserDataConsentValue = true
AppsFlyerConsentValues.DENIED.consentValue -> adUserDataConsentValue = false
}
var adPersonalizationConsentValue: Boolean? = null
when (settings[DEFAULT_AD_PERSONALIZATION_CONSENT]) {
AppsFlyerConsentValues.GRANTED.consentValue -> adPersonalizationConsentValue = true
AppsFlyerConsentValues.DENIED.consentValue -> adPersonalizationConsentValue = false
}
val clientConsentSettings = parseToNestedMap(consentState.toString())
parseConsentMapping(settings[CONSENT_MAPPING]).iterator().forEach { currentConsent ->
val isConsentAvailable =
searchKeyInNestedMap(clientConsentSettings, key = currentConsent.key)
if (isConsentAvailable != null) {
val isConsentGranted: Boolean =
JSONObject(isConsentAvailable.toString()).opt("consented") as Boolean
when (currentConsent.value) {
"ad_storage" -> adStorageConsentValue = isConsentGranted
"ad_user_data" -> adUserDataConsentValue = isConsentGranted
"ad_personalization" -> adPersonalizationConsentValue = isConsentGranted
}
}
}
appsFlyerGDPRUser = AppsFlyerConsent(true, adUserDataConsentValue, adPersonalizationConsentValue, adStorageConsentValue)
}
AppsFlyerLib.getInstance().setConsentData(appsFlyerGDPRUser)
}
private fun parseConsentMapping(json: String?): Map<String, String> {
if (json.isNullOrEmpty()) {
return emptyMap()
}
val jsonWithFormat = json.replace("\\", "")
return try {
JSONArray(jsonWithFormat)
.let { jsonArray ->
(0 until jsonArray.length())
.associate {
val jsonObject = jsonArray.getJSONObject(it)
val map = jsonObject.getString("map")
val value = jsonObject.getString("value")
map to value
}
}
} catch (jse: JSONException) {
Logger.error(
jse,
"The AppsFlyer kit threw an exception while searching for the configured consent purpose mapping in the current user's consent status.",
)
emptyMap()
}
}
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()
override fun onSettingsUpdated(settings: Map<String, String>) {
settings[SHARING_FILTER_FOR_PARTNERS]?.let { applySharingFilterForPartners(it) }
}
private fun applySharingFilterForPartners(jsonValue: String) {
val partners = parseSharingFilterForPartners(jsonValue)
if (!partners.isNullOrEmpty()) {
instance.setSharingFilterForPartners(*partners.toTypedArray())
}
}
private fun parseSharingFilterForPartners(json: String?): List<String>? {
if (json.isNullOrEmpty()) return null
return try {
val jsonWithFormat = json.replace("\\", "")
val array = JSONArray(jsonWithFormat)
List(array.length()) { i -> array.getString(i) }
} catch (e: JSONException) {
Logger.warning(
"AppsFlyer kit: failed to parse sharingFilterForPartners, " +
"consent filter for partners will not be applied. Error: ${e.message}",
)
null
}
}
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>? =
event?.products?.filter { !KitUtils.isEmpty(it.sku) }?.let {
if (it.isNotEmpty()) {
it.map { it.sku.replace(COMMA, "%2C") }
} else {
null
}
}
private const val SHARING_FILTER_FOR_PARTNERS = "sharingFilterForPartners"
private const val CONSENT_MAPPING = "consentMapping"
@Suppress("ktlint:standard:property-naming")
enum class AppsFlyerConsentValues(
val consentValue: String,
) {
GRANTED("Granted"),
DENIED("Denied"),
}
const val GDPR_APPLIES = "gdprApplies"
const val DEFAULT_AD_STORAGE_CONSENT = "defaultAdStorageConsent"
const val DEFAULT_AD_USER_DATA_CONSENT = "defaultAdUserDataConsent"
const val DEFAULT_AD_PERSONALIZATION_CONSENT = "defaultAdPersonalizationConsent"
}
}