Skip to content

Commit c88bd1c

Browse files
authored
add top level anonymous id method (#78)
* address api discrepancies between kotlin and swift * update java compat * add unit tests * correct typo
1 parent 98966a0 commit c88bd1c

File tree

4 files changed

+71
-5
lines changed

4 files changed

+71
-5
lines changed

core/src/main/java/com/segment/analytics/kotlin/core/Analytics.kt

+30
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ open class Analytics protected constructor(
5656
SegmentLog.loggingEnabled = value
5757
field = value
5858
}
59+
60+
/**
61+
* Retrieve the version of this library in use.
62+
* - Returns: A string representing the version in "BREAKING.FEATURE.FIX" format.
63+
*/
64+
fun version(): String = Constants.LIBRARY_VERSION
5965
}
6066

6167
init {
@@ -517,6 +523,30 @@ open class Analytics protected constructor(
517523
val system = store.currentState(System::class)
518524
return system?.settings
519525
}
526+
527+
/**
528+
* Retrieve the anonymousId in a blocking way.
529+
* Note: this method invokes `runBlocking` internal, it's not recommended to be used
530+
* in coroutines.
531+
*/
532+
@BlockingApi
533+
fun anonymousId(): String = runBlocking {
534+
anonymousIdAsync()
535+
}
536+
537+
/**
538+
* Retrieve the anonymousId
539+
*/
540+
suspend fun anonymousIdAsync(): String {
541+
val userInfo = store.currentState(UserInfo::class)
542+
return userInfo?.anonymousId ?: ""
543+
}
544+
545+
/**
546+
* Retrieve the version of this library in use.
547+
* - Returns: A string representing the version in "BREAKING.FEATURE.FIX" format.
548+
*/
549+
fun version() = Analytics.version()
520550
}
521551

522552

core/src/main/java/com/segment/analytics/kotlin/core/compat/JavaAnalytics.kt

+11
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,17 @@ class JavaAnalytics private constructor() {
244244
*/
245245
fun settings() = analytics.settings()
246246

247+
/**
248+
* Retrieve the anonymousId
249+
*/
250+
fun anonymousId() = analytics.anonymousId()
251+
252+
/**
253+
* Retrieve the version of this library in use.
254+
* - Returns: A string representing the version in "BREAKING.FEATURE.FIX" format.
255+
*/
256+
fun version() = analytics.version()
257+
247258
private fun setup(analytics: Analytics) {
248259
store = analytics.store
249260
storage = analytics.storage

core/src/test/kotlin/com/segment/analytics/kotlin/core/AnalyticsTests.kt

+20-5
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@ package com.segment.analytics.kotlin.core
33
import com.segment.analytics.kotlin.core.platform.Plugin
44
import com.segment.analytics.kotlin.core.platform.plugins.ContextPlugin
55
import com.segment.analytics.kotlin.core.utils.*
6-
import io.mockk.every
7-
import io.mockk.mockkStatic
8-
import io.mockk.slot
9-
import io.mockk.spyk
10-
import io.mockk.verify
6+
import io.mockk.*
117
import kotlinx.coroutines.test.UnconfinedTestDispatcher
128
import kotlinx.coroutines.test.TestScope
139
import kotlinx.coroutines.test.runTest
@@ -391,6 +387,20 @@ class AnalyticsTests {
391387
}
392388
}
393389

390+
@Nested
391+
inner class AnonymousId {
392+
@Test
393+
fun `anonymousId fetches current Analytics anonymousId`() = runTest {
394+
assertEquals("qwerty-qwerty-123", analytics.anonymousIdAsync())
395+
}
396+
397+
@Test
398+
fun `anonymousId returns empty string when null`() = runTest {
399+
coEvery { analytics.store.currentState(UserInfo::class) } returns null
400+
assertEquals("", analytics.anonymousIdAsync())
401+
}
402+
}
403+
394404
@Test
395405
fun `settings fetches current Analytics Settings`() = runTest {
396406
val settings = Settings(
@@ -405,6 +415,11 @@ class AnalyticsTests {
405415
assertEquals(settings, analytics.settings())
406416
}
407417

418+
@Test
419+
fun `version fetches current Analytics version`() {
420+
assertEquals(Constants.LIBRARY_VERSION, analytics.version())
421+
}
422+
408423
private fun BaseEvent.populate() = apply {
409424
anonymousId = "qwerty-qwerty-123"
410425
messageId = "qwerty-qwerty-123"

core/src/test/kotlin/com/segment/analytics/kotlin/core/compat/JavaAnalyticsTest.kt

+10
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,16 @@ internal class JavaAnalyticsTest {
464464
assertEquals(settings, analytics.settings())
465465
}
466466

467+
@Test
468+
fun anonymousId() {
469+
assertEquals("qwerty-qwerty-123", analytics.anonymousId())
470+
}
471+
472+
@Test
473+
fun version() {
474+
assertEquals(Constants.LIBRARY_VERSION, analytics.version())
475+
}
476+
467477
private fun BaseEvent.populate() = apply {
468478
anonymousId = "qwerty-qwerty-123"
469479
messageId = "qwerty-qwerty-123"

0 commit comments

Comments
 (0)