Skip to content

Commit 1e9ed77

Browse files
Add session start and end track calls (#18)
* Add session start and end track calls * else to reduce conditional checks Co-authored-by: Wenxi Zeng <[email protected]> * Updating analytics version --------- Co-authored-by: Wenxi Zeng <[email protected]>
1 parent 84513e9 commit 1e9ed77

File tree

2 files changed

+49
-10
lines changed

2 files changed

+49
-10
lines changed

lib/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ android {
4141
dependencies {
4242
coreLibraryDesugaring("com.android.tools:desugar_jdk_libs:2.0.2")
4343

44-
implementation("com.segment.analytics.kotlin:android:1.10.3")
44+
implementation("com.segment.analytics.kotlin:android:1.16.3")
4545
implementation("androidx.multidex:multidex:2.0.1")
4646

4747
implementation("androidx.core:core-ktx:1.8.0")

lib/src/main/java/com/segment/analytics/kotlin/destinations/amplitude/AmplitudeSession.kt

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,26 @@ import com.segment.analytics.kotlin.core.*
44
import com.segment.analytics.kotlin.core.platform.EventPlugin
55
import com.segment.analytics.kotlin.core.platform.Plugin
66
import com.segment.analytics.kotlin.core.platform.VersionedPlugin
7-
import com.segment.analytics.kotlin.core.platform.plugins.logger.LogKind
8-
import com.segment.analytics.kotlin.core.platform.plugins.logger.log
7+
import com.segment.analytics.kotlin.core.platform.plugins.logger.*
98
import com.segment.analytics.kotlin.core.utilities.putIntegrations
109

1110
// A Destination plugin that adds session tracking to Amplitude cloud mode.
1211
class AmplitudeSession (private val sessionTimeoutMs : Long = 300000) : EventPlugin, VersionedPlugin {
1312

1413
override val type: Plugin.Type = Plugin.Type.Enrichment
1514
override lateinit var analytics: Analytics
16-
var sessionID = java.lang.System.currentTimeMillis()
15+
private var eventSessionId = -1L
16+
private var sessionId = eventSessionId
1717
private val key = "Actions Amplitude"
18+
private val ampSessionEndEvent = "session_end"
19+
private val ampSessionStartEvent = "session_start"
1820
private var active = false
1921
private var lastEventFiredTime = java.lang.System.currentTimeMillis()
2022

23+
override fun setup(analytics: Analytics) {
24+
super.setup(analytics)
25+
startNewSessionIfNecessary()
26+
}
2127
override fun update(settings: Settings, type: Plugin.UpdateType) {
2228
active = settings.hasIntegrationSettings(key)
2329
}
@@ -27,14 +33,25 @@ class AmplitudeSession (private val sessionTimeoutMs : Long = 300000) : EventPlu
2733
return event
2834
}
2935

36+
startNewSessionIfNecessary()
37+
3038
val modified = super.execute(event)
3139
analytics.log(
32-
message = "Running ${event.type} payload through AmplitudeSession",
33-
kind = LogKind.DEBUG
40+
message = "Running ${event.type} payload through AmplitudeSession"
3441
)
35-
lastEventFiredTime = java.lang.System.currentTimeMillis()
3642

37-
return modified?.putIntegrations(key, mapOf("session_id" to sessionID))
43+
if (event is TrackEvent) {
44+
if(event.event == ampSessionStartEvent) {
45+
// Update session ID for all events after this in the queue
46+
eventSessionId = sessionId
47+
analytics.log(message = "NewSession = $eventSessionId")
48+
}
49+
else if (event.event == ampSessionEndEvent) {
50+
analytics.log(message = "EndSession = $eventSessionId")
51+
}
52+
}
53+
54+
return modified?.putIntegrations(key, mapOf("session_id" to eventSessionId))
3855
}
3956

4057
override fun track(payload: TrackEvent): BaseEvent {
@@ -51,10 +68,32 @@ class AmplitudeSession (private val sessionTimeoutMs : Long = 300000) : EventPlu
5168
}
5269

5370
private fun onForeground() {
71+
startNewSessionIfNecessary()
72+
}
73+
74+
private fun startNewSession() {
75+
analytics.track(ampSessionStartEvent)
76+
}
77+
78+
private fun endSession() {
79+
analytics.track(ampSessionEndEvent)
80+
}
81+
82+
private fun startNewSessionIfNecessary() {
5483
val current = java.lang.System.currentTimeMillis()
55-
if (current - lastEventFiredTime >= sessionTimeoutMs) {
56-
sessionID = current
84+
// Make sure the first event has a valid ID and we send a session start.
85+
// Subsequent events should have session IDs updated after the session track event is sent
86+
if(eventSessionId == -1L || sessionId == -1L) {
87+
sessionId = current
88+
eventSessionId = current
89+
startNewSession()
90+
} else if (current - lastEventFiredTime >= sessionTimeoutMs) {
91+
sessionId = current
92+
93+
endSession()
94+
startNewSession()
5795
}
96+
lastEventFiredTime = current
5897
}
5998

6099
override fun version(): String {

0 commit comments

Comments
 (0)