Skip to content

Commit 488b44a

Browse files
feat: Allow Client to Manually Call Appsflyer Start
1 parent 541195a commit 488b44a

3 files changed

Lines changed: 355 additions & 82 deletions

File tree

src/main/kotlin/com/mparticle/kits/AppsFlyerKit.kt

Lines changed: 58 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import com.mparticle.MParticle
2929
import com.mparticle.commerce.CommerceEvent
3030
import com.mparticle.commerce.Product
3131
import com.mparticle.consent.ConsentState
32+
import com.mparticle.identity.MParticleUser
3233
import com.mparticle.internal.Logger
3334
import com.mparticle.internal.MPUtility
3435
import org.json.JSONArray
@@ -47,7 +48,8 @@ class AppsFlyerKit :
4748
KitIntegration.CommerceListener,
4849
AppsFlyerConversionListener,
4950
KitIntegration.ActivityListener,
50-
KitIntegration.UserAttributeListener {
51+
KitIntegration.UserAttributeListener,
52+
KitIntegration.IdentityListener {
5153
override fun getInstance(): AppsFlyerLib = AppsFlyerLib.getInstance()
5254

5355
override fun getName() = NAME
@@ -62,12 +64,12 @@ class AppsFlyerKit :
6264
MParticle.getInstance()?.environment == MParticle.Environment.Development,
6365
)
6466
settings[DEV_KEY]?.let { AppsFlyerLib.getInstance().init(it, this, context) }
65-
setting?.get(SHARING_FILTER_FOR_PARTNERS)?.let {
66-
applySharingFilterForPartners(it)
67-
}
6867
val userConsentState = currentUser?.consentState
6968
setConsent(userConsentState)
70-
AppsFlyerLib.getInstance().start(context.applicationContext)
69+
updateCustomerUserIdFromMpid(currentUser?.id)
70+
if (!isManualStart()) {
71+
AppsFlyerLib.getInstance().start(context.applicationContext)
72+
}
7173
AppsFlyerLib.getInstance().setCollectAndroidID(MParticle.isAndroidIdEnabled())
7274
val integrationAttributes = HashMap<String, String?>(1)
7375
integrationAttributes[APPSFLYERID_INTEGRATION_KEY] =
@@ -307,7 +309,9 @@ class AppsFlyerKit :
307309

308310
override fun removeUserIdentity(identityType: MParticle.IdentityType) {
309311
with(instance) {
310-
if (MParticle.IdentityType.CustomerId == identityType) {
312+
if (isUserIdentificationMpid()) {
313+
updateCustomerUserIdFromMpid(currentUser?.id)
314+
} else if (MParticle.IdentityType.CustomerId == identityType) {
311315
setCustomerUserId("")
312316
} else if (MParticle.IdentityType.Email == identityType) {
313317
setUserEmails(AppsFlyerProperties.EmailsCryptType.NONE, "")
@@ -320,7 +324,9 @@ class AppsFlyerKit :
320324
identity: String,
321325
) {
322326
with(instance) {
323-
if (MParticle.IdentityType.CustomerId == identityType) {
327+
if (isUserIdentificationMpid()) {
328+
updateCustomerUserIdFromMpid(currentUser?.id)
329+
} else if (MParticle.IdentityType.CustomerId == identityType) {
324330
setCustomerUserId(identity)
325331
} else if (MParticle.IdentityType.Email == identityType) {
326332
setUserEmails(AppsFlyerProperties.EmailsCryptType.NONE, identity)
@@ -330,6 +336,38 @@ class AppsFlyerKit :
330336

331337
override fun logout(): List<ReportingMessage> = emptyList()
332338

339+
override fun onIdentifyCompleted(
340+
mParticleUser: MParticleUser,
341+
filteredIdentityApiRequest: FilteredIdentityApiRequest,
342+
) {
343+
updateCustomerUserIdFromMpid(mParticleUser.id)
344+
}
345+
346+
override fun onLoginCompleted(
347+
mParticleUser: MParticleUser,
348+
filteredIdentityApiRequest: FilteredIdentityApiRequest,
349+
) {
350+
updateCustomerUserIdFromMpid(mParticleUser.id)
351+
}
352+
353+
override fun onLogoutCompleted(
354+
mParticleUser: MParticleUser,
355+
filteredIdentityApiRequest: FilteredIdentityApiRequest,
356+
) {
357+
updateCustomerUserIdFromMpid(mParticleUser.id)
358+
}
359+
360+
override fun onModifyCompleted(
361+
mParticleUser: MParticleUser,
362+
filteredIdentityApiRequest: FilteredIdentityApiRequest,
363+
) {
364+
updateCustomerUserIdFromMpid(mParticleUser.id)
365+
}
366+
367+
override fun onUserIdentified(mParticleUser: MParticleUser) {
368+
updateCustomerUserIdFromMpid(mParticleUser.id)
369+
}
370+
333371
private fun parseToNestedMap(jsonString: String): Map<String, Any> {
334372
val topLevelMap = mutableMapOf<String, Any>()
335373
try {
@@ -559,7 +597,9 @@ class AppsFlyerKit :
559597
activity: Activity,
560598
bundle: Bundle?,
561599
): List<ReportingMessage> {
562-
instance.start(activity)
600+
if (!isManualStart()) {
601+
instance.start(activity)
602+
}
563603
return emptyList()
564604
}
565605

@@ -578,30 +618,15 @@ class AppsFlyerKit :
578618

579619
override fun onActivityDestroyed(activity: Activity): List<ReportingMessage> = emptyList()
580620

581-
override fun onSettingsUpdated(settings: Map<String, String>) {
582-
settings[SHARING_FILTER_FOR_PARTNERS]?.let { applySharingFilterForPartners(it) }
583-
}
621+
private fun isManualStart(): Boolean =
622+
settings[MANUAL_START]?.lowercase() == "true"
584623

585-
private fun applySharingFilterForPartners(jsonValue: String) {
586-
val partners = parseSharingFilterForPartners(jsonValue)
587-
if (!partners.isNullOrEmpty()) {
588-
instance.setSharingFilterForPartners(*partners.toTypedArray())
589-
}
590-
}
624+
private fun isUserIdentificationMpid(): Boolean =
625+
settings[USER_IDENTIFICATION_TYPE] == USER_IDENTIFICATION_MPID
591626

592-
private fun parseSharingFilterForPartners(json: String?): List<String>? {
593-
if (json.isNullOrEmpty()) return null
594-
return try {
595-
val jsonWithFormat = json.replace("\\", "")
596-
val array = JSONArray(jsonWithFormat)
597-
List(array.length()) { i -> array.getString(i) }
598-
} catch (e: JSONException) {
599-
Logger.warning(
600-
"AppsFlyer kit: failed to parse sharingFilterForPartners, " +
601-
"consent filter for partners will not be applied. Error: ${e.message}",
602-
)
603-
null
604-
}
627+
private fun updateCustomerUserIdFromMpid(mpid: Long?) {
628+
if (!isUserIdentificationMpid() || mpid == null) return
629+
instance.setCustomerUserId(mpid.toString())
605630
}
606631

607632
companion object {
@@ -630,7 +655,9 @@ class AppsFlyerKit :
630655
}
631656
}
632657

633-
private const val SHARING_FILTER_FOR_PARTNERS = "sharingFilterForPartners"
658+
const val MANUAL_START = "manualStart"
659+
const val USER_IDENTIFICATION_TYPE = "userIdentificationType"
660+
const val USER_IDENTIFICATION_MPID = "MPID"
634661
private const val CONSENT_MAPPING = "consentMapping"
635662

636663
@Suppress("ktlint:standard:property-naming")

src/test/kotlin/com/appsflyer/AppsFlyerLib.kt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,36 @@ import android.content.Context
55
class AppsFlyerLib {
66
private var consentData: AppsFlyerConsent? = null
77

8+
var startCallCount = 0
9+
private set
10+
11+
var customerUserId: String? = null
12+
private set
13+
814
fun setConsentData(consent: AppsFlyerConsent) {
915
consentData = consent
1016
}
1117

1218
fun getConsentData(): AppsFlyerConsent? = consentData
1319

20+
fun start(context: Context) {
21+
startCallCount++
22+
}
23+
24+
fun setCustomerUserId(id: String?) {
25+
customerUserId = id
26+
}
27+
28+
fun init(devKey: String, conversionListener: Any?, context: Context) {}
29+
30+
fun setCollectAndroidID(collect: Boolean) {}
31+
32+
fun getAppsFlyerUID(context: Context): String = "test-appsflyer-uid"
33+
34+
fun subscribeForDeepLink(listener: Any?) {}
35+
36+
fun setDebugLog(debug: Boolean) {}
37+
1438
fun getConsentState(): MutableMap<Any, Any> {
1539
val stateMap = mutableMapOf<Any, Any>()
1640
consentData?.let { consent ->

0 commit comments

Comments
 (0)