@@ -7,8 +7,13 @@ import com.segment.analytics.kotlin.core.platform.Timeline
7
7
import com.segment.analytics.kotlin.core.platform.plugins.ContextPlugin
8
8
import com.segment.analytics.kotlin.core.platform.plugins.SegmentDestination
9
9
import com.segment.analytics.kotlin.core.platform.plugins.StartupQueue
10
- import kotlinx.coroutines.*
11
- import com.segment.analytics.kotlin.core.platform.plugins.logger.*
10
+ import com.segment.analytics.kotlin.core.platform.plugins.logger.SegmentLog
11
+ import com.segment.analytics.kotlin.core.platform.plugins.logger.log
12
+ import kotlinx.coroutines.CoroutineScope
13
+ import kotlinx.coroutines.SupervisorJob
14
+ import kotlinx.coroutines.asCoroutineDispatcher
15
+ import kotlinx.coroutines.launch
16
+ import kotlinx.coroutines.runBlocking
12
17
import kotlinx.serialization.DeserializationStrategy
13
18
import kotlinx.serialization.SerializationStrategy
14
19
import kotlinx.serialization.json.Json
@@ -32,7 +37,7 @@ import kotlin.reflect.KClass
32
37
*/
33
38
open class Analytics protected constructor(
34
39
val configuration : Configuration ,
35
- coroutineConfig : CoroutineConfiguration
40
+ coroutineConfig : CoroutineConfiguration ,
36
41
) : Subscriber, CoroutineConfiguration by coroutineConfig {
37
42
38
43
// use lazy to avoid the instance being leak before fully initialized
@@ -74,13 +79,17 @@ open class Analytics protected constructor(
74
79
* Public constructor of Analytics.
75
80
* @property configuration configuration that analytics can use
76
81
*/
77
- constructor (configuration: Configuration ): this (configuration, object : CoroutineConfiguration {
78
- override val store = Store ()
79
- override val analyticsScope = CoroutineScope (SupervisorJob ())
80
- override val analyticsDispatcher = Executors .newCachedThreadPool().asCoroutineDispatcher()
81
- override val networkIODispatcher = Executors .newSingleThreadExecutor().asCoroutineDispatcher()
82
- override val fileIODispatcher = Executors .newFixedThreadPool(2 ).asCoroutineDispatcher()
83
- })
82
+ constructor (configuration: Configuration ) : this (configuration,
83
+ object : CoroutineConfiguration {
84
+ override val store = Store ()
85
+ override val analyticsScope = CoroutineScope (SupervisorJob ())
86
+ override val analyticsDispatcher =
87
+ Executors .newCachedThreadPool().asCoroutineDispatcher()
88
+ override val networkIODispatcher =
89
+ Executors .newSingleThreadExecutor().asCoroutineDispatcher()
90
+ override val fileIODispatcher =
91
+ Executors .newFixedThreadPool(2 ).asCoroutineDispatcher()
92
+ })
84
93
85
94
// This function provides a default state to the store & attaches the storage and store instances
86
95
// Initiates the initial call to settings and adds default system plugins
@@ -89,7 +98,7 @@ open class Analytics protected constructor(
89
98
add(SegmentLog ())
90
99
add(StartupQueue ())
91
100
add(ContextPlugin ())
92
-
101
+
93
102
// Setup store
94
103
analyticsScope.launch(analyticsDispatcher) {
95
104
store.also {
@@ -138,7 +147,7 @@ open class Analytics protected constructor(
138
147
fun <T : Any > track (
139
148
name : String ,
140
149
properties : T ,
141
- serializationStrategy : SerializationStrategy <T >
150
+ serializationStrategy : SerializationStrategy <T >,
142
151
) {
143
152
track(name, Json .encodeToJsonElement(serializationStrategy, properties).jsonObject)
144
153
}
@@ -154,7 +163,7 @@ open class Analytics protected constructor(
154
163
*/
155
164
inline fun <reified T : Any > track (
156
165
name : String ,
157
- properties : T
166
+ properties : T ,
158
167
) {
159
168
track(name, properties, Json .serializersModule.serializer())
160
169
}
@@ -204,11 +213,81 @@ open class Analytics protected constructor(
204
213
fun <T : Any > identify (
205
214
userId : String ,
206
215
traits : T ,
207
- serializationStrategy : SerializationStrategy <T >
216
+ serializationStrategy : SerializationStrategy <T >,
208
217
) {
209
218
identify(userId, Json .encodeToJsonElement(serializationStrategy, traits).jsonObject)
210
219
}
211
220
221
+ /* *
222
+ * Identify lets you tie one of your users and their actions to a recognizable {@code userId}.
223
+ * It also lets you record {@code traits} about the user, like their email, name, account type,
224
+ * etc.
225
+ *
226
+ * <p>Traits and userId will be automatically cached and available on future sessions for the
227
+ * same user. To update a trait on the server, call identify with the same user id.
228
+ * You can also use {@link #identify(Traits)} for this purpose.
229
+ *
230
+ * In the case when user logs out, make sure to call {@link #reset()} to clear user's identity
231
+ * info.
232
+ *
233
+ * @param traits [Traits] about the user. Needs to be [serializable](https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/serializers.md)
234
+ * @see <a href="https://segment.com/docs/spec/identify/">Identify Documentation</a>
235
+ */
236
+ inline fun <reified T : Any > identify (
237
+ traits : T ,
238
+ ) {
239
+ identify(traits, Json .serializersModule.serializer())
240
+ }
241
+
242
+ /* *
243
+ * Identify lets you record {@code traits} about the user, like their email, name, account type,
244
+ * etc.
245
+ *
246
+ * <p>Traits and userId will be automatically cached and available on future sessions for the
247
+ * same user. To update a trait on the server, call identify with the same user id.
248
+ * You can also use {@link #identify(Traits)} for this purpose.
249
+ *
250
+ * In the case when user logs out, make sure to call {@link #reset()} to clear user's identity
251
+ * info.
252
+ *
253
+ * @param traits [Traits] about the user.
254
+ * @see <a href="https://segment.com/docs/spec/identify/">Identify Documentation</a>
255
+ */
256
+ @JvmOverloads
257
+ fun identify (traits : JsonObject = emptyJsonObject) {
258
+ analyticsScope.launch(analyticsDispatcher) {
259
+ store.dispatch(UserInfo .SetTraitsAction (traits), UserInfo ::class )
260
+ }
261
+ val event = IdentifyEvent (
262
+ userId = " " , // using "" for userId, which will get filled down the pipe
263
+ traits = traits
264
+ )
265
+ process(event)
266
+ }
267
+
268
+ /* *
269
+ * Identify lets you tie one of your users and their actions to a recognizable {@code userId}.
270
+ * It also lets you record {@code traits} about the user, like their email, name, account type,
271
+ * etc.
272
+ *
273
+ * <p>Traits and userId will be automatically cached and available on future sessions for the
274
+ * same user. To update a trait on the server, call identify with the same user id.
275
+ * You can also use {@link #identify(Traits)} for this purpose.
276
+ *
277
+ * In the case when user logs out, make sure to call {@link #reset()} to clear user's identity
278
+ * info.
279
+ *
280
+ * @param traits [Traits] about the user. Needs to be [serializable](https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/serializers.md)
281
+ * @param serializationStrategy strategy to serialize [traits]
282
+ * @see <a href="https://segment.com/docs/spec/identify/">Identify Documentation</a>
283
+ */
284
+ fun <T : Any > identify (
285
+ traits : T ,
286
+ serializationStrategy : SerializationStrategy <T >,
287
+ ) {
288
+ identify(Json .encodeToJsonElement(serializationStrategy, traits).jsonObject)
289
+ }
290
+
212
291
/* *
213
292
* Identify lets you tie one of your users and their actions to a recognizable {@code userId}.
214
293
* It also lets you record {@code traits} about the user, like their email, name, account type,
@@ -246,7 +325,7 @@ open class Analytics protected constructor(
246
325
fun screen (
247
326
title : String ,
248
327
properties : JsonObject = emptyJsonObject,
249
- category : String = ""
328
+ category : String = "",
250
329
) {
251
330
val event = ScreenEvent (name = title, category = category, properties = properties)
252
331
process(event)
@@ -267,7 +346,7 @@ open class Analytics protected constructor(
267
346
title : String ,
268
347
properties : T ,
269
348
serializationStrategy : SerializationStrategy <T >,
270
- category : String = ""
349
+ category : String = "",
271
350
) {
272
351
screen(
273
352
title,
@@ -326,7 +405,7 @@ open class Analytics protected constructor(
326
405
fun <T : Any > group (
327
406
groupId : String ,
328
407
traits : T ,
329
- serializationStrategy : SerializationStrategy <T >
408
+ serializationStrategy : SerializationStrategy <T >,
330
409
) {
331
410
group(groupId, Json .encodeToJsonElement(serializationStrategy, traits).jsonObject)
332
411
}
@@ -404,7 +483,7 @@ open class Analytics protected constructor(
404
483
* 2. or the first instance of subclass of the given class/interface
405
484
* @param plugin [KClass]
406
485
*/
407
- fun <T : Plugin > find (plugin : KClass <T >): T ? = this .timeline.find(plugin)
486
+ fun <T : Plugin > find (plugin : KClass <T >): T ? = this .timeline.find(plugin)
408
487
409
488
/* *
410
489
* Retrieve the first match of registered destination plugin by key. It finds
@@ -418,7 +497,7 @@ open class Analytics protected constructor(
418
497
* 2. and all instances of subclass of the given class/interface
419
498
* @param plugin [KClass]
420
499
*/
421
- fun <T : Plugin > findAll (plugin : KClass <T >): List <T > = this .timeline.findAll(plugin)
500
+ fun <T : Plugin > findAll (plugin : KClass <T >): List <T > = this .timeline.findAll(plugin)
422
501
423
502
/* *
424
503
* Remove a plugin from the analytics timeline using its name
@@ -552,7 +631,7 @@ open class Analytics protected constructor(
552
631
/* *
553
632
* Retrieve the version of this library in use.
554
633
* - Returns: A string representing the version in "BREAKING.FEATURE.FIX" format.
555
- */
634
+ */
556
635
fun version () = Analytics .version()
557
636
}
558
637
0 commit comments