@@ -4,20 +4,26 @@ import com.segment.analytics.kotlin.core.*
44import com.segment.analytics.kotlin.core.platform.EventPlugin
55import com.segment.analytics.kotlin.core.platform.Plugin
66import 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.*
98import com.segment.analytics.kotlin.core.utilities.putIntegrations
109
1110// A Destination plugin that adds session tracking to Amplitude cloud mode.
1211class 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