Skip to content

Commit

Permalink
fix: event options should override event properties, refresh last eve…
Browse files Browse the repository at this point in the history
…nt time on exit foreground (#122)
  • Loading branch information
falconandy authored May 18, 2023
1 parent 789feee commit 652ea42
Show file tree
Hide file tree
Showing 11 changed files with 662 additions and 304 deletions.
25 changes: 19 additions & 6 deletions android/src/main/java/com/amplitude/android/Amplitude.kt
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,20 @@ open class Amplitude(
return
}

val dummySessionStartEvent = BaseEvent()
dummySessionStartEvent.eventType = START_SESSION_EVENT
dummySessionStartEvent.timestamp = timestamp
dummySessionStartEvent.sessionId = -1
timeline.process(dummySessionStartEvent)
val dummyEnterForegroundEvent = BaseEvent()
dummyEnterForegroundEvent.eventType = DUMMY_ENTER_FOREGROUND_EVENT
dummyEnterForegroundEvent.timestamp = timestamp
timeline.process(dummyEnterForegroundEvent)
}

fun onExitForeground() {
fun onExitForeground(timestamp: Long) {
inForeground = false

val dummyExitForegroundEvent = BaseEvent()
dummyExitForegroundEvent.eventType = DUMMY_EXIT_FOREGROUND_EVENT
dummyExitForegroundEvent.timestamp = timestamp
timeline.process(dummyExitForegroundEvent)

amplitudeScope.launch(amplitudeDispatcher) {
isBuilt.await()
if ((configuration as Configuration).flushEventsOnClose) {
Expand All @@ -132,6 +136,15 @@ open class Amplitude(
* The event type for end session events.
*/
const val END_SESSION_EVENT = "session_end"

/**
* The event type for dummy enter foreground events.
*/
internal const val DUMMY_ENTER_FOREGROUND_EVENT = "dummy_enter_foreground"
/**
* The event type for dummy exit foreground events.
*/
internal const val DUMMY_EXIT_FOREGROUND_EVENT = "dummy_exit_foreground"
}
}
/**
Expand Down
24 changes: 13 additions & 11 deletions android/src/main/java/com/amplitude/android/Timeline.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ class Timeline : Timeline() {
amplitude.amplitudeScope.launch(amplitude.storageIODispatcher) {
amplitude.isBuilt.await()

_sessionId.set(amplitude.storage.read(Storage.Constants.PREVIOUS_SESSION_ID)?.toLong() ?: -1)
lastEventId = amplitude.storage.read(Storage.Constants.LAST_EVENT_ID)?.toLong() ?: 0
lastEventTime = amplitude.storage.read(Storage.Constants.LAST_EVENT_TIME)?.toLong() ?: -1
_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

for (message in eventMessageChannel) {
processEventMessage(message)
Expand All @@ -49,18 +49,20 @@ class Timeline : Timeline() {
val event = message.event
var sessionEvents: Iterable<BaseEvent>? = null
val eventTimestamp = event.timestamp!!
val eventSessionId = event.sessionId
var skipEvent = false

if (event.eventType == Amplitude.START_SESSION_EVENT) {
if (event.sessionId < 0) { // dummy start_session event
skipEvent = true
sessionEvents = startNewSessionIfNeeded(eventTimestamp)
} else {
setSessionId(event.sessionId)
refreshSessionTime(eventTimestamp)
}
setSessionId(eventSessionId ?: eventTimestamp)
refreshSessionTime(eventTimestamp)
} else if (event.eventType == Amplitude.END_SESSION_EVENT) {
// do nothing
} else if (event.eventType == Amplitude.DUMMY_ENTER_FOREGROUND_EVENT) {
skipEvent = true
sessionEvents = startNewSessionIfNeeded(eventTimestamp)
} else if (event.eventType == Amplitude.DUMMY_EXIT_FOREGROUND_EVENT) {
skipEvent = true
refreshSessionTime(eventTimestamp)
} else {
if (!message.inForeground) {
sessionEvents = startNewSessionIfNeeded(eventTimestamp)
Expand All @@ -69,7 +71,7 @@ class Timeline : Timeline() {
}
}

if (!skipEvent && event.sessionId < 0) {
if (!skipEvent && event.sessionId == null) {
event.sessionId = sessionId
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class AndroidLifecyclePlugin : Application.ActivityLifecycleCallbacks, Plugin {
}

override fun onActivityPaused(activity: Activity) {
(amplitude as com.amplitude.android.Amplitude).onExitForeground()
(amplitude as com.amplitude.android.Amplitude).onExitForeground(getCurrentTimeMillis())
}

override fun onActivityStopped(activity: Activity) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class AmplitudeRobolectricTests {
event2.timestamp = 1700
amplitudeInstance.track(event2)

amplitudeInstance.onExitForeground()
amplitudeInstance.onExitForeground(2000)

verify(exactly = 0) { mockedPluginTest.track(any()) }
}
Expand Down
Loading

0 comments on commit 652ea42

Please sign in to comment.