-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathApptimizeKit.kt
More file actions
327 lines (297 loc) · 10.6 KB
/
Copy pathApptimizeKit.kt
File metadata and controls
327 lines (297 loc) · 10.6 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
package com.mparticle.kits
import android.content.Context
import android.text.TextUtils
import com.apptimize.Apptimize
import com.apptimize.Apptimize.IsFirstTestRun
import com.apptimize.Apptimize.OnTestRunListener
import com.apptimize.ApptimizeOptions
import com.apptimize.ApptimizeTestInfo
import com.mparticle.MPEvent
import com.mparticle.MParticle
import com.mparticle.MParticle.IdentityType
import com.mparticle.commerce.CommerceEvent
import com.mparticle.kits.KitIntegration.AttributeListener
import com.mparticle.kits.KitIntegration.CommerceListener
import com.mparticle.kits.KitIntegration.LogoutListener
import java.math.BigDecimal
class ApptimizeKit :
KitIntegration(),
AttributeListener,
LogoutListener,
KitIntegration.EventListener,
CommerceListener,
OnTestRunListener {
private fun toMessageList(message: ReportingMessage): List<ReportingMessage> = listOf(message)
private fun createReportingMessage(messageType: String): ReportingMessage =
ReportingMessage(
this,
messageType,
System.currentTimeMillis(),
null,
)
override fun onKitCreate(
settings: Map<String, String>,
context: Context,
): List<ReportingMessage> {
val appKey = getSettings()[APP_MP_KEY]
require(!TextUtils.isEmpty(appKey)) { APP_MP_KEY }
val options = buildApptimizeOptions(settings)
Apptimize.setup(context, appKey, options)
if (java.lang.Boolean.parseBoolean(settings[TRACK_EXPERIMENTS])) {
Apptimize.setOnTestRunListener(this)
}
return emptyList()
}
private fun buildApptimizeOptions(settings: Map<String, String>): ApptimizeOptions {
val o = ApptimizeOptions()
o.isThirdPartyEventImportingEnabled = false
o.isThirdPartyEventExportingEnabled = false
configureApptimizeUpdateMetaDataTimeout(o, settings)
configureApptimizeDeviceName(o, settings)
configureApptimizeDeveloperModeDisabled(o, settings)
configureApptimizeExplicitEnablingRequired(o, settings)
configureApptimizeMultiprocessModeEnabled(o, settings)
configureApptimizeLogLevel(o, settings)
return o
}
private fun configureApptimizeUpdateMetaDataTimeout(
o: ApptimizeOptions,
settings: Map<String, String>,
) {
try {
settings[UPDATE_METDATA_TIMEOUT_MP_KEY]?.let {
val l = it.toLong()
o.setUpdateMetadataTimeout(l)
}
} catch (nfe: NumberFormatException) {
}
}
private fun configureApptimizeDeviceName(
o: ApptimizeOptions,
settings: Map<String, String>,
) {
val v = settings[DEVICE_NAME_MP_KEY]
o.deviceName = v
}
private fun configureApptimizeDeveloperModeDisabled(
o: ApptimizeOptions,
settings: Map<String, String>,
) {
val b = settings[DEVELOPER_MODE_DISABLED_MP_KEY]
o.isDeveloperModeDisabled = b.toBoolean()
}
private fun configureApptimizeExplicitEnablingRequired(
o: ApptimizeOptions,
settings: Map<String, String>,
) {
val b = settings[EXPLICIT_ENABLING_REQUIRED_MP_KEY]
o.isExplicitEnablingRequired = b.toBoolean()
}
private fun configureApptimizeMultiprocessModeEnabled(
o: ApptimizeOptions,
settings: Map<String, String>,
) {
val b = settings[MULTIPROCESS_MODE_ENABLED_MP_KEY]
o.setMultiprocessMode(b.toBoolean())
}
private fun configureApptimizeLogLevel(
o: ApptimizeOptions,
settings: Map<String, String>,
) {
try {
val l =
settings[LOG_LEVEL_MP_KEY]
?.let { ApptimizeOptions.LogLevel.valueOf(it) }
?.let { o.logLevel = it }
} catch (iae: IllegalArgumentException) {
} catch (npe: NullPointerException) {
}
}
override fun getName(): String = KIT_NAME
override fun setUserAttribute(
key: String,
value: String,
) {
Apptimize.setUserAttribute(key, value)
}
/**
* Not supported by the Apptimize kit.
*/
override fun setUserAttributeList(
key: String,
list: List<String>,
) {
// not supported
}
override fun supportsAttributeLists(): Boolean = false
/**
* @param attributeLists is ignored by the Apptimize kit.
*/
override fun setAllUserAttributes(
attributes: Map<String, String>,
attributeLists: Map<String, List<String>>,
) {
for ((key, value) in attributes) {
setUserAttribute(key, value)
}
}
override fun onRemoveUserAttribute(
key: String,
user: FilteredMParticleUser,
) {
Apptimize.clearUserAttribute(key)
}
/**
* @param identityType only Alias and CustomerId are suppoted by the Apptimize kit.
*/
override fun setUserIdentity(
identityType: IdentityType,
id: String?,
) {
when (identityType) {
IdentityType.Alias, IdentityType.CustomerId -> {
Apptimize.setPilotTargetingId(id)
}
else -> {}
}
}
override fun removeUserIdentity(identityType: IdentityType) {
setUserIdentity(identityType, null)
}
override fun logout(): List<ReportingMessage> {
Apptimize.track(LOGOUT_TAG)
return toMessageList(ReportingMessage.logoutMessage(this))
}
/**
* Not supported by the Apptimize kit.
*/
override fun leaveBreadcrumb(s: String): List<ReportingMessage> = emptyList()
/**
* Not supported by the Apptimize kit.
*/
override fun logError(
s: String,
map: Map<String, String>,
): List<ReportingMessage> = emptyList()
/**
* Not supported by the Apptimize kit.
*/
override fun logException(
e: Exception,
map: Map<String, String>,
s: String,
): List<ReportingMessage> = emptyList()
override fun logEvent(mpEvent: MPEvent): List<ReportingMessage> {
Apptimize.track(mpEvent.eventName)
return toMessageList(ReportingMessage.fromEvent(this, mpEvent))
}
/**
* @param eventAttributes is ignored by the Apptimize kit.
*/
override fun logScreen(
screenName: String,
eventAttributes: Map<String, String>,
): List<ReportingMessage> {
val event = String.format(VIEWED_EVENT_FORMAT, screenName)
Apptimize.track(event)
return toMessageList(
createReportingMessage(ReportingMessage.MessageType.SCREEN_VIEW).setScreenName(
screenName,
),
)
}
/**
* @param valueTotal is ignored by the Apptimize kit.
* @param contextInfo is ignored by the Apptimize kit.
*/
override fun logLtvIncrease(
valueIncreased: BigDecimal,
valueTotal: BigDecimal,
eventName: String,
contextInfo: Map<String, String>,
): List<ReportingMessage> {
// match the iOS style, where only the delta is sent rather than an absolute final value.
Apptimize.track(LTV_TAG, valueIncreased.toDouble())
return toMessageList(createReportingMessage(ReportingMessage.MessageType.COMMERCE_EVENT))
}
override fun logEvent(commerceEvent: CommerceEvent): List<ReportingMessage>? {
val customEvents = CommerceEventUtils.expand(commerceEvent)
if (customEvents.size == 0) {
return null
}
for (event in customEvents) {
Apptimize.track(event.eventName)
}
return toMessageList(ReportingMessage.fromEvent(this, commerceEvent))
}
/**
* After opting out, it is not possible to opt back in via the Apptimize kit.
* @param optedOut only a value of 'true' supported by the Apptimize kit.
*/
override fun setOptOut(optedOut: Boolean): List<ReportingMessage>? {
var ret: List<ReportingMessage>? = null
if (optedOut) {
Apptimize.disable()
ret =
toMessageList(
createReportingMessage(ReportingMessage.MessageType.OPT_OUT).setOptOut(optedOut),
)
}
return ret
}
override fun onTestRun(
apptimizeTestInfo: ApptimizeTestInfo,
isFirstTestRun: IsFirstTestRun,
) {
if (isFirstTestRun != IsFirstTestRun.YES) {
return
}
val testInfoMap = Apptimize.getTestInfo()
val participatedExperiments: MutableList<String> = ArrayList()
if (testInfoMap == null) {
return
}
for (key in testInfoMap.keys) {
val testInfo = testInfoMap[key]
if (testInfo != null) {
if (testInfo.userHasParticipated()) {
val nameAndVariation = testInfo.testName + "-" + testInfo.enrolledVariantName
participatedExperiments.add(nameAndVariation)
}
}
}
val user = MParticle.getInstance()!!.Identity().currentUser
user?.setUserAttributeList("Apptimize experiment", participatedExperiments)
val eventInfo = HashMap<String, String?>(5)
eventInfo["VariationID"] = apptimizeTestInfo.enrolledVariantId.toString()
eventInfo["ID"] = apptimizeTestInfo.testId.toString()
eventInfo["Name"] = apptimizeTestInfo.testName
eventInfo["Variation"] = apptimizeTestInfo.enrolledVariantName
eventInfo["Name and Variation"] =
apptimizeTestInfo.testName + "-" +
apptimizeTestInfo.enrolledVariantName
val event =
MPEvent
.Builder(
"Apptimize experiment",
MParticle.EventType
.Other,
).customAttributes(eventInfo)
.build()
MParticle.getInstance()?.logEvent(event)
}
companion object {
private const val APP_MP_KEY = "appKey"
private const val UPDATE_METDATA_TIMEOUT_MP_KEY = "metadataTimeout"
private const val DEVICE_NAME_MP_KEY = "deviceName"
private const val DEVELOPER_MODE_DISABLED_MP_KEY = "developerModeDisabled"
private const val EXPLICIT_ENABLING_REQUIRED_MP_KEY = "explicitEnablingRequired"
private const val MULTIPROCESS_MODE_ENABLED_MP_KEY = "multiprocessModeEnabled"
private const val LOG_LEVEL_MP_KEY = "logLevel"
private const val LOGOUT_TAG = "logout"
private const val LTV_TAG = "ltv"
private const val VIEWED_EVENT_FORMAT = "screenView %s"
private const val TRACK_EXPERIMENTS = "trackExperiments"
private const val KIT_NAME = "Apptimize"
}
}