@@ -12,6 +12,7 @@ import com.mparticle.MPEvent
1212import com.mparticle.MParticle
1313import com.mparticle.MParticle.IdentityType
1414import com.mparticle.TypedUserAttributeListener
15+ import com.mparticle.UserAttributeListenerType
1516import com.mparticle.commerce.CommerceEvent
1617import com.mparticle.commerce.Product
1718import com.mparticle.consent.ConsentState
@@ -23,52 +24,31 @@ import java.util.*
2324
2425
2526class LeanplumKit : KitIntegration (), UserAttributeListener,
26- KitIntegration .EventListener , CommerceListener , IdentityListener , PushListener {
27+ KitIntegration .EventListener , CommerceListener , IdentityListener , PushListener {
28+
2729 public override fun onKitCreate (
2830 settings : Map <String , String >,
2931 context : Context
3032 ): List <ReportingMessage > {
3133
3234 val deviceIdType = settings[DEVICE_ID_TYPE ]
3335
34- val userId = MParticle .getInstance()?.Identity ()?.currentUser?.let {
35- generateLeanplumUserId(
36- it,
37- settings,
38- userIdentities
39- )
40- }
36+ val userId = generateLeanplumId()
4137
4238 if (deviceIdType != null ) {
4339 setDeviceIdType(deviceIdType)
4440 }
4541 if (MParticle .getInstance()?.environment == MParticle .Environment .Development ) {
4642 Leanplum .setLogLevel(3 )
47- Leanplum .setAppIdForDevelopmentMode(settings[APP_ID_KEY ], settings[CLIENT_KEY_KEY ])
43+ Leanplum .setAppIdForDevelopmentMode(settings[APP_ID_KEY ], settings[CLIENT_KEY_KEY ])
4844 } else {
4945 Leanplum .setAppIdForProductionMode(settings[APP_ID_KEY ], settings[CLIENT_KEY_KEY ])
5046 }
5147
52- if (! allUserAttributes.containsKey(LEANPLUM_EMAIL_USER_ATTRIBUTE )) {
53- if (userIdentities.containsKey(IdentityType .Email )) {
54- allUserAttributes[LEANPLUM_EMAIL_USER_ATTRIBUTE ] =
55- userIdentities[IdentityType .Email ]
56- } else {
57- allUserAttributes[LEANPLUM_EMAIL_USER_ATTRIBUTE ] = null
58- }
59- }
60- if (! TextUtils .isEmpty(userId)) {
61- if (allUserAttributes.isNotEmpty()) {
62- Leanplum .start(context, userId, allUserAttributes)
63- } else {
64- Leanplum .start(context, userId)
65- }
66- } else if (allUserAttributes.isNotEmpty()) {
67- Leanplum .start(context, allUserAttributes)
68- } else {
69- Leanplum .start(context)
70- }
48+ // Starting Leanplum with empty map to avoid db query, setting it after calling async fun
49+ Leanplum .start(context, userId)
7150 LeanplumActivityHelper .enableLifecycleCallbacks(context.applicationContext as Application )
51+ MParticle .getInstance()?.Identity ()?.currentUser?.getUserAttributes()
7252
7353 return listOf (
7454 ReportingMessage (
@@ -80,6 +60,16 @@ class LeanplumKit : KitIntegration(), UserAttributeListener,
8060 )
8161 }
8262
63+ private fun generateLeanplumId (): String? {
64+ return MParticle .getInstance()?.Identity ()?.currentUser?.let {
65+ generateLeanplumUserId(
66+ it,
67+ settings,
68+ userIdentities
69+ )?.ifEmpty { null }
70+ }
71+ }
72+
8373 override fun onIdentifyCompleted (
8474 mParticleUser : MParticleUser ,
8575 filteredIdentityApiRequest : FilteredIdentityApiRequest
@@ -143,12 +133,26 @@ class LeanplumKit : KitIntegration(), UserAttributeListener,
143133 )
144134 ) {
145135 if (userIdentities.containsKey(IdentityType .Email )) {
146- userAttributes[LEANPLUM_EMAIL_USER_ATTRIBUTE ] = userIdentities[IdentityType .Email ]
136+ userAttributes[LEANPLUM_EMAIL_USER_ATTRIBUTE ] =
137+ userIdentities[IdentityType .Email ]
147138 } else {
148139 userAttributes[LEANPLUM_EMAIL_USER_ATTRIBUTE ] = null
149140 }
150141 }
151- Leanplum .setUserAttributes(userAttributes)
142+ setAttributesAndCheckId(userAttributes)
143+ }
144+
145+ private fun setAttributesAndCheckId (userAttributes : MutableMap <String , Any ?>) {
146+ // If by the time onKitCreated was called, userAttributes were not available to create a LeanplumId, creating and setting one
147+ if (Leanplum .getUserId().isNullOrEmpty()) {
148+ generateLeanplumId()?.let { id ->
149+ Leanplum .setUserAttributes(id, userAttributes)
150+ } ? : run {
151+ Leanplum .setUserAttributes(userAttributes)
152+ }
153+ } else {
154+ Leanplum .setUserAttributes(userAttributes)
155+ }
152156 // per Leanplum - it's a good idea to make sure the SDK refreshes itself
153157 Leanplum .forceContentUpdate()
154158 }
@@ -193,9 +197,9 @@ class LeanplumKit : KitIntegration(), UserAttributeListener,
193197 override fun setOptOut (optedOut : Boolean ): List <ReportingMessage > = emptyList()
194198
195199 override fun onSetUserAttribute (key : String , value : Any , user : FilteredMParticleUser ) {
196- val attributes = HashMap <String , Any >( 1 )
200+ val attributes = mutableMapOf <String , Any ?>( )
197201 attributes[key] = value
198- Leanplum .setUserAttributes (attributes)
202+ setAttributesAndCheckId (attributes)
199203 }
200204
201205 override fun onSetUserTag (s : String , filteredMParticleUser : FilteredMParticleUser ) {}
@@ -204,9 +208,9 @@ class LeanplumKit : KitIntegration(), UserAttributeListener,
204208 list : List <String >,
205209 user : FilteredMParticleUser
206210 ) {
207- val attributes: MutableMap <String , Any > = HashMap ( 1 )
211+ val attributes = mutableMapOf <String , Any ?>( )
208212 attributes[key] = list
209- Leanplum .setUserAttributes (attributes)
213+ setAttributesAndCheckId (attributes)
210214 }
211215
212216 override fun onIncrementUserAttribute (
@@ -215,9 +219,9 @@ class LeanplumKit : KitIntegration(), UserAttributeListener,
215219 newValue : String ,
216220 user : FilteredMParticleUser ?
217221 ) {
218- val attributes = HashMap <String , Any >( 1 )
222+ val attributes = mutableMapOf <String , Any ?>( )
219223 attributes[attr.key.toString()] = newValue
220- Leanplum .setUserAttributes (attributes)
224+ setAttributesAndCheckId (attributes)
221225 }
222226
223227 override fun supportsAttributeLists (): Boolean = true
@@ -235,13 +239,17 @@ class LeanplumKit : KitIntegration(), UserAttributeListener,
235239 attributeLists : Map <String , List <String >>,
236240 user : FilteredMParticleUser
237241 ) {
242+ val map = mutableMapOf<String , Any ?>()
243+ map.putAll(attributes)
244+ map.putAll(attributeLists)
245+ setAttributesAndCheckId(map)
238246 // we set user attributes on start so there's no point in doing it here as well.
239247 }
240248
241249 override fun onRemoveUserAttribute (key : String , user : FilteredMParticleUser ) {
242- val attribute : MutableMap <String , Any ?> = HashMap ( 1 )
243- attribute [key] = null
244- Leanplum .setUserAttributes(attribute )
250+ val attributes = mutableMapOf <String , Any ?>( )
251+ attributes [key] = null
252+ setAttributesAndCheckId(attributes )
245253 }
246254
247255 override fun leaveBreadcrumb (s : String ): List <ReportingMessage > = emptyList()
0 commit comments