Skip to content

Commit

Permalink
feat: allow setting sessionId in initial configuration (#182)
Browse files Browse the repository at this point in the history
  • Loading branch information
justin-fiedler authored Mar 1, 2024
1 parent 111007f commit 81c5866
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 5 deletions.
2 changes: 1 addition & 1 deletion android/src/main/java/com/amplitude/android/Amplitude.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ open class Amplitude(
}

override fun createTimeline(): Timeline {
return Timeline().also { it.amplitude = this }
return Timeline(configuration.sessionId).also { it.amplitude = this }
}

override fun createIdentityConfiguration(): IdentityConfiguration {
Expand Down
2 changes: 2 additions & 0 deletions android/src/main/java/com/amplitude/android/Configuration.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ open class Configuration @JvmOverloads constructor(
var migrateLegacyData: Boolean = true,
override var offline: Boolean? = false,
override var deviceId: String? = null,
override var sessionId: Long? = null,
) : Configuration(
apiKey,
flushQueueSize,
Expand All @@ -70,6 +71,7 @@ open class Configuration @JvmOverloads constructor(
identityStorageProvider,
offline,
deviceId,
sessionId,
) {
companion object {
const val MIN_TIME_BETWEEN_SESSIONS_MILLIS: Long = 300000
Expand Down
15 changes: 11 additions & 4 deletions android/src/main/java/com/amplitude/android/Timeline.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,31 @@ import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.launch
import java.util.concurrent.atomic.AtomicLong

class Timeline : Timeline() {
class Timeline(
private val initialSessionId: Long? = null,
) : Timeline() {
private val eventMessageChannel: Channel<EventQueueMessage> = Channel(Channel.UNLIMITED)

private val _sessionId = AtomicLong(-1)
private val _sessionId = AtomicLong(initialSessionId ?: -1L)
val sessionId: Long
get() {
return _sessionId.get()
}

internal var lastEventId: Long = 0
var lastEventTime: Long = -1
var lastEventTime: Long = -1L

internal fun start() {
amplitude.amplitudeScope.launch(amplitude.storageIODispatcher) {
// Wait until build (including possible legacy data migration) is finished.
amplitude.isBuilt.await()

_sessionId.set(amplitude.storage.read(Storage.Constants.PREVIOUS_SESSION_ID)?.toLongOrNull() ?: -1)
if (initialSessionId == null) {
_sessionId.set(
amplitude.storage.read(Storage.Constants.PREVIOUS_SESSION_ID)?.toLongOrNull()
?: -1
)
}
lastEventId = amplitude.storage.read(Storage.Constants.LAST_EVENT_ID)?.toLongOrNull() ?: 0
lastEventTime = amplitude.storage.read(Storage.Constants.LAST_EVENT_TIME)?.toLongOrNull() ?: -1

Expand Down
14 changes: 14 additions & 0 deletions android/src/test/java/com/amplitude/android/AmplitudeTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class AmplitudeTest {
minTimeBetweenSessionsMillis: Long? = null,
storageProvider: StorageProvider = InMemoryStorageProvider(),
deviceId: String? = null,
sessionId: Long? = null,
): Configuration {
val configuration = Configuration(
apiKey = "api-key",
Expand All @@ -88,6 +89,7 @@ class AmplitudeTest {
loggerProvider = ConsoleLoggerProvider(),
identifyInterceptStorageProvider = InMemoryStorageProvider(),
identityStorageProvider = IMIdentityStorageProvider(),
sessionId = sessionId,
)

if (deviceId != null) {
Expand Down Expand Up @@ -217,6 +219,18 @@ class AmplitudeTest {
}
}

@Test
fun amplitude_should_set_sessionId_from_configuration() = runTest {
val testSessionId = 1337L
// set device Id in the config
amplitude = Amplitude(createConfiguration(sessionId = testSessionId))
setDispatcher(testScheduler)

if (amplitude?.isBuilt!!.await()) {
Assertions.assertEquals(testSessionId, amplitude?.sessionId)
}
}

companion object {
const val instanceName = "testInstance"
}
Expand Down
1 change: 1 addition & 0 deletions core/src/main/java/com/amplitude/core/Configuration.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ open class Configuration @JvmOverloads constructor(
open var identityStorageProvider: IdentityStorageProvider = IMIdentityStorageProvider(),
open var offline: Boolean? = false,
open var deviceId: String? = null,
open var sessionId: Long? = null,
) {

companion object {
Expand Down

0 comments on commit 81c5866

Please sign in to comment.