1
1
package com.segment.analytics.kotlin.core
2
2
3
3
import com.segment.analytics.kotlin.core.platform.DestinationPlugin
4
+ import com.segment.analytics.kotlin.core.platform.EnrichmentClosure
4
5
import com.segment.analytics.kotlin.core.platform.EventPlugin
5
6
import com.segment.analytics.kotlin.core.platform.Plugin
6
7
import com.segment.analytics.kotlin.core.platform.Timeline
@@ -153,12 +154,13 @@ open class Analytics protected constructor(
153
154
*
154
155
* @param name Name of the action
155
156
* @param properties [Properties] to describe the action.
157
+ * @param enrichment a closure that enables enrichment on the generated event
156
158
* @see <a href="https://segment.com/docs/spec/track/">Track Documentation</a>
157
159
*/
158
160
@JvmOverloads
159
- fun track (name : String , properties : JsonObject = emptyJsonObject) {
161
+ fun track (name : String , properties : JsonObject = emptyJsonObject, enrichment : EnrichmentClosure ? = null ) {
160
162
val event = TrackEvent (event = name, properties = properties)
161
- process(event)
163
+ process(event, enrichment )
162
164
}
163
165
164
166
/* *
@@ -169,14 +171,16 @@ open class Analytics protected constructor(
169
171
* @param name Name of the action
170
172
* @param properties to describe the action. Needs to be [serializable](https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/serializers.md)
171
173
* @param serializationStrategy strategy to serialize [properties]
174
+ * @param enrichment a closure that enables enrichment on the generated event
172
175
* @see <a href="https://segment.com/docs/spec/track/">Track Documentation</a>
173
176
*/
174
177
fun <T > track (
175
178
name : String ,
176
179
properties : T ,
177
180
serializationStrategy : SerializationStrategy <T >,
181
+ enrichment : EnrichmentClosure ? = null
178
182
) {
179
- track(name, Json .encodeToJsonElement(serializationStrategy, properties).jsonObject)
183
+ track(name, Json .encodeToJsonElement(serializationStrategy, properties).jsonObject, enrichment )
180
184
}
181
185
182
186
/* *
@@ -186,13 +190,15 @@ open class Analytics protected constructor(
186
190
*
187
191
* @param name Name of the action
188
192
* @param properties to describe the action. Needs to be [serializable](https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/serializers.md)
193
+ * @param enrichment a closure that enables enrichment on the generated event
189
194
* @see <a href="https://segment.com/docs/spec/track/">Track Documentation</a>
190
195
*/
191
196
inline fun <reified T > track (
192
197
name : String ,
193
198
properties : T ,
199
+ noinline enrichment : EnrichmentClosure ? = null
194
200
) {
195
- track(name, properties, JsonAnySerializer .serializersModule.serializer())
201
+ track(name, properties, JsonAnySerializer .serializersModule.serializer(), enrichment )
196
202
}
197
203
198
204
/* *
@@ -209,15 +215,16 @@ open class Analytics protected constructor(
209
215
*
210
216
* @param userId Unique identifier which you recognize a user by in your own database
211
217
* @param traits [Traits] about the user.
218
+ * @param enrichment a closure that enables enrichment on the generated event
212
219
* @see <a href="https://segment.com/docs/spec/identify/">Identify Documentation</a>
213
220
*/
214
221
@JvmOverloads
215
- fun identify (userId : String , traits : JsonObject = emptyJsonObject) {
222
+ fun identify (userId : String , traits : JsonObject = emptyJsonObject, enrichment : EnrichmentClosure ? = null ) {
216
223
analyticsScope.launch(analyticsDispatcher) {
217
224
store.dispatch(UserInfo .SetUserIdAndTraitsAction (userId, traits), UserInfo ::class )
218
225
}
219
226
val event = IdentifyEvent (userId = userId, traits = traits)
220
- process(event)
227
+ process(event, enrichment )
221
228
}
222
229
223
230
/* *
@@ -235,14 +242,16 @@ open class Analytics protected constructor(
235
242
* @param userId Unique identifier which you recognize a user by in your own database
236
243
* @param traits [Traits] about the user. Needs to be [serializable](https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/serializers.md)
237
244
* @param serializationStrategy strategy to serialize [traits]
245
+ * @param enrichment a closure that enables enrichment on the generated event
238
246
* @see <a href="https://segment.com/docs/spec/identify/">Identify Documentation</a>
239
247
*/
240
248
fun <T > identify (
241
249
userId : String ,
242
250
traits : T ,
243
251
serializationStrategy : SerializationStrategy <T >,
252
+ enrichment : EnrichmentClosure ? = null
244
253
) {
245
- identify(userId, Json .encodeToJsonElement(serializationStrategy, traits).jsonObject)
254
+ identify(userId, Json .encodeToJsonElement(serializationStrategy, traits).jsonObject, enrichment )
246
255
}
247
256
248
257
/* *
@@ -258,12 +267,14 @@ open class Analytics protected constructor(
258
267
* info.
259
268
*
260
269
* @param traits [Traits] about the user. Needs to be [serializable](https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/serializers.md)
270
+ * @param enrichment a closure that enables enrichment on the generated event
261
271
* @see <a href="https://segment.com/docs/spec/identify/">Identify Documentation</a>
262
272
*/
263
273
inline fun <reified T > identify (
264
274
traits : T ,
275
+ noinline enrichment : EnrichmentClosure ? = null
265
276
) {
266
- identify(traits, JsonAnySerializer .serializersModule.serializer())
277
+ identify(traits, JsonAnySerializer .serializersModule.serializer(), enrichment )
267
278
}
268
279
269
280
/* *
@@ -278,18 +289,19 @@ open class Analytics protected constructor(
278
289
* info.
279
290
*
280
291
* @param traits [Traits] about the user.
292
+ * @param enrichment a closure that enables enrichment on the generated event
281
293
* @see <a href="https://segment.com/docs/spec/identify/">Identify Documentation</a>
282
294
*/
283
295
@JvmOverloads
284
- fun identify (traits : JsonObject = emptyJsonObject) {
296
+ fun identify (traits : JsonObject = emptyJsonObject, enrichment : EnrichmentClosure ? = null ) {
285
297
analyticsScope.launch(analyticsDispatcher) {
286
298
store.dispatch(UserInfo .SetTraitsAction (traits), UserInfo ::class )
287
299
}
288
300
val event = IdentifyEvent (
289
301
userId = " " , // using "" for userId, which will get filled down the pipe
290
302
traits = traits
291
303
)
292
- process(event)
304
+ process(event, enrichment )
293
305
}
294
306
295
307
/* *
@@ -306,13 +318,15 @@ open class Analytics protected constructor(
306
318
*
307
319
* @param traits [Traits] about the user. Needs to be [serializable](https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/serializers.md)
308
320
* @param serializationStrategy strategy to serialize [traits]
321
+ * @param enrichment a closure that enables enrichment on the generated event
309
322
* @see <a href="https://segment.com/docs/spec/identify/">Identify Documentation</a>
310
323
*/
311
324
fun <T > identify (
312
325
traits : T ,
313
326
serializationStrategy : SerializationStrategy <T >,
327
+ enrichment : EnrichmentClosure ? = null
314
328
) {
315
- identify(Json .encodeToJsonElement(serializationStrategy, traits).jsonObject)
329
+ identify(Json .encodeToJsonElement(serializationStrategy, traits).jsonObject, enrichment )
316
330
}
317
331
318
332
/* *
@@ -329,13 +343,15 @@ open class Analytics protected constructor(
329
343
*
330
344
* @param userId Unique identifier which you recognize a user by in your own database
331
345
* @param traits [Traits] about the user. Needs to be [serializable](https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/serializers.md)
346
+ * @param enrichment a closure that enables enrichment on the generated event
332
347
* @see <a href="https://segment.com/docs/spec/identify/">Identify Documentation</a>
333
348
*/
334
349
inline fun <reified T > identify (
335
350
userId : String ,
336
351
traits : T ,
352
+ noinline enrichment : EnrichmentClosure ? = null
337
353
) {
338
- identify(userId, traits, JsonAnySerializer .serializersModule.serializer())
354
+ identify(userId, traits, JsonAnySerializer .serializersModule.serializer(), enrichment )
339
355
}
340
356
341
357
/* *
@@ -346,16 +362,18 @@ open class Analytics protected constructor(
346
362
* @param title A name for the screen.
347
363
* @param category A category to describe the screen.
348
364
* @param properties [Properties] to add extra information to this call.
365
+ * @param enrichment a closure that enables enrichment on the generated event
349
366
* @see <a href="https://segment.com/docs/spec/screen/">Screen Documentation</a>
350
367
*/
351
368
@JvmOverloads
352
369
fun screen (
353
370
title : String ,
354
371
properties : JsonObject = emptyJsonObject,
355
372
category : String = "",
373
+ enrichment : EnrichmentClosure ? = null
356
374
) {
357
375
val event = ScreenEvent (name = title, category = category, properties = properties)
358
- process(event)
376
+ process(event, enrichment )
359
377
}
360
378
361
379
/* *
@@ -367,18 +385,21 @@ open class Analytics protected constructor(
367
385
* @param category A category to describe the screen.
368
386
* @param properties [Properties] to add extra information to this call. Needs to be [serializable](https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/serializers.md)
369
387
* @param serializationStrategy strategy to serialize [properties]
388
+ * @param enrichment a closure that enables enrichment on the generated event
370
389
* @see <a href="https://segment.com/docs/spec/screen/">Screen Documentation</a>
371
390
*/
372
391
fun <T > screen (
373
392
title : String ,
374
393
properties : T ,
375
394
serializationStrategy : SerializationStrategy <T >,
376
395
category : String = "",
396
+ enrichment : EnrichmentClosure ? = null
377
397
) {
378
398
screen(
379
399
title,
380
400
Json .encodeToJsonElement(serializationStrategy, properties).jsonObject,
381
- category
401
+ category,
402
+ enrichment
382
403
)
383
404
}
384
405
@@ -390,14 +411,16 @@ open class Analytics protected constructor(
390
411
* @param title A name for the screen.
391
412
* @param category A category to describe the screen.
392
413
* @param properties [Properties] to add extra information to this call. Needs to be [serializable](https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/serializers.md)
414
+ * @param enrichment a closure that enables enrichment on the generated event
393
415
* @see <a href="https://segment.com/docs/spec/screen/">Screen Documentation</a>
394
416
*/
395
417
inline fun <reified T > screen (
396
418
title : String ,
397
419
properties : T ,
398
420
category : String = "",
421
+ noinline enrichment : EnrichmentClosure ? = null
399
422
) {
400
- screen(title, properties, JsonAnySerializer .serializersModule.serializer(), category)
423
+ screen(title, properties, JsonAnySerializer .serializersModule.serializer(), category, enrichment )
401
424
}
402
425
403
426
/* *
@@ -409,12 +432,13 @@ open class Analytics protected constructor(
409
432
*
410
433
* @param groupId Unique identifier which you recognize a group by in your own database
411
434
* @param traits [Traits] about the group
435
+ * @param enrichment a closure that enables enrichment on the generated event
412
436
* @see <a href="https://segment.com/docs/spec/group/">Group Documentation</a>
413
437
*/
414
438
@JvmOverloads
415
- fun group (groupId : String , traits : JsonObject = emptyJsonObject) {
439
+ fun group (groupId : String , traits : JsonObject = emptyJsonObject, enrichment : EnrichmentClosure ? = null ) {
416
440
val event = GroupEvent (groupId = groupId, traits = traits)
417
- process(event)
441
+ process(event, enrichment )
418
442
}
419
443
420
444
/* *
@@ -427,14 +451,16 @@ open class Analytics protected constructor(
427
451
* @param groupId Unique identifier which you recognize a group by in your own database
428
452
* @param traits [Traits] about the group. Needs to be [serializable](https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/serializers.md)
429
453
* @param serializationStrategy strategy to serialize [traits]
454
+ * @param enrichment a closure that enables enrichment on the generated event
430
455
* @see <a href="https://segment.com/docs/spec/group/">Group Documentation</a>
431
456
*/
432
457
fun <T > group (
433
458
groupId : String ,
434
459
traits : T ,
435
460
serializationStrategy : SerializationStrategy <T >,
461
+ enrichment : EnrichmentClosure ? = null
436
462
) {
437
- group(groupId, Json .encodeToJsonElement(serializationStrategy, traits).jsonObject)
463
+ group(groupId, Json .encodeToJsonElement(serializationStrategy, traits).jsonObject, enrichment )
438
464
}
439
465
440
466
/* *
@@ -446,13 +472,15 @@ open class Analytics protected constructor(
446
472
*
447
473
* @param groupId Unique identifier which you recognize a group by in your own database
448
474
* @param traits [Traits] about the group. Needs to be [serializable](https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/serializers.md)
475
+ * @param enrichment a closure that enables enrichment on the generated event
449
476
* @see <a href="https://segment.com/docs/spec/group/">Group Documentation</a>
450
477
*/
451
478
inline fun <reified T > group (
452
479
groupId : String ,
453
480
traits : T ,
481
+ noinline enrichment : EnrichmentClosure ? = null
454
482
) {
455
- group(groupId, traits, JsonAnySerializer .serializersModule.serializer())
483
+ group(groupId, traits, JsonAnySerializer .serializersModule.serializer(), enrichment )
456
484
}
457
485
458
486
/* *
@@ -462,9 +490,10 @@ open class Analytics protected constructor(
462
490
*
463
491
* @param newId The new ID you want to alias the existing ID to. The existing ID will be either
464
492
* the previousId if you have called identify, or the anonymous ID.
493
+ * @param enrichment a closure that enables enrichment on the generated event
465
494
* @see <a href="https://segment.com/docs/tracking-api/alias/">Alias Documentation</a>
466
495
*/
467
- fun alias (newId : String ) {
496
+ fun alias (newId : String , enrichment : EnrichmentClosure ? = null ) {
468
497
analyticsScope.launch(analyticsDispatcher) {
469
498
val curUserInfo = store.currentState(UserInfo ::class )
470
499
if (curUserInfo != null ) {
@@ -475,14 +504,14 @@ open class Analytics protected constructor(
475
504
launch {
476
505
store.dispatch(UserInfo .SetUserIdAction (newId), UserInfo ::class )
477
506
}
478
- process(event)
507
+ process(event, enrichment )
479
508
} else {
480
509
log(" failed to fetch current UserInfo state" )
481
510
}
482
511
}
483
512
}
484
513
485
- fun process (event : BaseEvent ) {
514
+ fun process (event : BaseEvent , enrichment : EnrichmentClosure ? = null ) {
486
515
if (! enabled) return
487
516
488
517
event.applyBaseData()
@@ -491,7 +520,7 @@ open class Analytics protected constructor(
491
520
analyticsScope.launch(analyticsDispatcher) {
492
521
event.applyBaseEventData(store)
493
522
log(" processing event on ${Thread .currentThread().name} " )
494
- timeline.process(event)
523
+ timeline.process(event, enrichment )
495
524
}
496
525
}
497
526
0 commit comments