-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTaplyticsKit.kt
More file actions
314 lines (277 loc) · 9.4 KB
/
Copy pathTaplyticsKit.kt
File metadata and controls
314 lines (277 loc) · 9.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
package com.mparticle.kits
import android.content.Context
import com.mparticle.MPEvent
import com.mparticle.MParticle.IdentityType
import com.mparticle.commerce.CommerceEvent
import com.mparticle.commerce.Product
import com.mparticle.identity.MParticleUser
import com.mparticle.kits.KitIntegration.AttributeListener
import com.mparticle.kits.KitIntegration.CommerceListener
import com.mparticle.kits.KitIntegration.IdentityListener
import com.mparticle.kits.KitIntegration.SessionListener
import com.taplytics.sdk.Taplytics
import org.json.JSONException
import org.json.JSONObject
import java.lang.Exception
import java.math.BigDecimal
import java.util.HashMap
open class TaplyticsKit :
KitIntegration(),
AttributeListener,
KitIntegration.EventListener,
CommerceListener,
IdentityListener,
SessionListener {
private fun mergeOptions(
tlOptions: Map<String?, Any?>,
configuration: MutableMap<String, Any>?,
): HashMap<String?, Any?> {
var tlOptions: Map<String?, Any?>? = tlOptions
var configuration = configuration
if (tlOptions == null) {
tlOptions = HashMap()
}
if (configuration == null) {
configuration = HashMap()
}
val merged = HashMap(configuration)
for ((key, value) in tlOptions) {
merged[key] = value
}
return merged
}
public override fun onKitCreate(
settings: Map<String, String>,
context: Context,
): List<ReportingMessage> {
if (!started && !delayInitializationUntilSessionStart) {
started = true
startTaplytics(settings, context)
}
return emptyList()
}
private fun createReportingMessages(message: String): List<ReportingMessage> {
val reportingMessage =
ReportingMessage(
this,
message,
System.currentTimeMillis(),
null,
)
return listOf(reportingMessage)
}
protected open fun startTaplytics(
settings: Map<String, String>,
context: Context?,
) {
val apiKey = getAPIKey(settings)
val options = mergeOptions(tlOptions, getOptionsFromConfiguration(settings))
options[DELAYED_START] = true
Taplytics.startTaplytics(context, apiKey, options)
}
private fun getAPIKey(settings: Map<String, String>): String? {
val apiKey = settings[API_KEY]
require(!KitUtils.isEmpty(apiKey)) { FAILED_TO_INITIALIZE_KIT_MESSAGE }
return apiKey
}
private fun getOptionsFromConfiguration(settings: Map<String, String>): MutableMap<String, Any>? {
val options = HashMap<String, Any>()
addAggressiveOption(options, settings)
return if (options.isEmpty()) null else options
}
private fun addAggressiveOption(
options: MutableMap<String, Any>,
settings: Map<String, String>,
) {
val agg = settings[AGGRESSIVE].toBoolean()
options[TAPLYTICS_AGGRESSIVE] = agg
}
override fun getName(): String = KIT_NAME
/**
* AttributeListener Interface
*/
override fun setUserAttribute(
attributeKey: String,
attributeValue: String?,
) {
try {
val attr = JSONObject()
if (attributeValue != null) {
attr.put(attributeKey, attributeValue)
} else {
attr.put(attributeKey, "")
}
Taplytics.setUserAttributes(attr)
} catch (e: JSONException) {
}
}
override fun supportsAttributeLists(): Boolean = false
override fun setAllUserAttributes(
attributes: Map<String, String>,
attributeLists: Map<String, List<String>>,
) {
for ((key, value) in attributes) {
setUserAttribute(key, value)
}
}
override fun setUserIdentity(
identityType: IdentityType,
s: String?,
) {
// no-op
}
override fun removeUserIdentity(identityType: IdentityType) {
setUserIdentity(identityType, null)
}
override fun removeUserAttribute(attribute: String) {
setUserAttribute(attribute, null)
}
override fun onIdentifyCompleted(
mParticleUser: MParticleUser,
filteredIdentityApiRequest: FilteredIdentityApiRequest,
) {
setUserAttributeFromRequest(filteredIdentityApiRequest)
}
/**
* Identity Listener
*/
private fun setUserAttributeFromRequest(request: FilteredIdentityApiRequest) {
val identities = request.userIdentities
try {
val attr = JSONObject()
if (identities[IdentityType.CustomerId] != null) {
attr.put(USER_ID, identities[IdentityType.CustomerId])
}
if (identities[IdentityType.Email] != null) {
attr.put(EMAIL, identities[IdentityType.Email])
}
Taplytics.setUserAttributes(attr)
} catch (e: JSONException) {
}
}
override fun onLoginCompleted(
user: MParticleUser,
request: FilteredIdentityApiRequest,
) {
setUserAttributeFromRequest(request)
}
override fun onLogoutCompleted(
mParticleUser: MParticleUser,
filteredIdentityApiRequest: FilteredIdentityApiRequest,
) {
Taplytics.resetAppUser {
// no-op
}
}
override fun onModifyCompleted(
mParticleUser: MParticleUser,
request: FilteredIdentityApiRequest,
) {
setUserAttributeFromRequest(request)
}
override fun onUserIdentified(mParticleUser: MParticleUser) {
// no-op
}
/**
* Unsupported methods
*/
override fun logout(): List<ReportingMessage> = emptyList()
override fun setUserAttributeList(
attribute: String,
attributeValueList: List<String>,
) {}
/**
* CommerceListener Interface
*/
override fun logEvent(event: CommerceEvent): List<ReportingMessage> {
if (!KitUtils.isEmpty(event.productAction) &&
event.productAction.equals(Product.PURCHASE, true)
) {
val transactionAttributes = event.transactionAttributes ?: return emptyList()
val id = transactionAttributes.id
val revenue = transactionAttributes.revenue ?: return emptyList()
Taplytics.logRevenue(id, revenue)
return listOf(ReportingMessage.fromEvent(this, event))
}
return emptyList()
}
/**
* EventListener Interface
*/
override fun logEvent(event: MPEvent): List<ReportingMessage>? {
val eventName = event.eventName
val metaDataMap = event.customAttributeStrings
var metaData: JSONObject? = null
if (metaDataMap != null) {
metaData = (metaDataMap as Map<*, *>?)?.let { JSONObject(it) }
}
Taplytics.logEvent(eventName, null, metaData)
return listOf(ReportingMessage.fromEvent(this, event))
}
override fun logScreen(
screenName: String,
screenAttributes: Map<String, String>,
): List<ReportingMessage> {
Taplytics.logEvent(screenName)
return createReportingMessages(ReportingMessage.MessageType.SCREEN_VIEW)
}
/**
* Unsupported Methods
*/
override fun logException(
exception: Exception,
exceptionAttributes: Map<String, String>,
message: String,
): List<ReportingMessage> = emptyList()
override fun logError(
message: String,
errorAttributes: Map<String, String>,
): List<ReportingMessage> = emptyList()
override fun leaveBreadcrumb(breadcrumb: String): List<ReportingMessage> = emptyList()
// put all these together
override fun logLtvIncrease(
valueIncreased: BigDecimal,
valueTotal: BigDecimal,
eventName: String,
contextInfo: Map<String, String>,
): List<ReportingMessage> = emptyList()
override fun setOptOut(optedOut: Boolean): List<ReportingMessage> {
Taplytics.hasUserOptedOutTracking(null) { hasOptedOut ->
if (!hasOptedOut && optedOut) {
Taplytics.optOutUserTracking(null)
} else if (hasOptedOut && !optedOut) {
Taplytics.optInUserTracking(null)
}
}
return createReportingMessages(ReportingMessage.MessageType.OPT_OUT)
}
override fun onSessionStart(): List<ReportingMessage> {
if (!started && delayInitializationUntilSessionStart) {
started = true
startTaplytics(configuration.settings, context)
}
return emptyList()
}
override fun onSessionEnd(): List<ReportingMessage> = emptyList()
companion object {
/**
* Option Keys
*/
private const val API_KEY = "apiKey"
private const val AGGRESSIVE = "TaplyticsOptionAggressive"
private const val TAPLYTICS_AGGRESSIVE = "aggressive"
private const val USER_ID = "user_id"
private const val EMAIL = "email"
private const val DELAYED_START = "delayedStartTaplytics"
private const val FAILED_TO_INITIALIZE_KIT_MESSAGE = "Failed to initialize Taplytics SDK - an API key is required"
private const val KIT_NAME = "Taplytics"
@JvmField
var delayInitializationUntilSessionStart = false
@JvmField
var started = false
/**
* tlOptions get and set methods
*/
var tlOptions = HashMap<String?, Any?>()
}
}