Skip to content

Commit e80a354

Browse files
authored
update session logic (#15)
1 parent dccb046 commit e80a354

File tree

1 file changed

+16
-84
lines changed

1 file changed

+16
-84
lines changed

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

Lines changed: 16 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,130 +1,62 @@
11
package com.segment.analytics.kotlin.destinations.amplitude
22

33
import com.segment.analytics.kotlin.core.*
4+
import com.segment.analytics.kotlin.core.platform.EventPlugin
45
import com.segment.analytics.kotlin.core.platform.Plugin
56
import com.segment.analytics.kotlin.core.platform.VersionedPlugin
67
import com.segment.analytics.kotlin.core.platform.plugins.logger.LogKind
78
import com.segment.analytics.kotlin.core.platform.plugins.logger.log
89
import com.segment.analytics.kotlin.core.utilities.putIntegrations
9-
import java.util.*
10-
import kotlin.concurrent.schedule
1110

1211
// A Destination plugin that adds session tracking to Amplitude cloud mode.
13-
class AmplitudeSession (sessionTimeoutMs : Long = 300000) : Plugin, VersionedPlugin {
12+
class AmplitudeSession (private val sessionTimeoutMs : Long = 300000) : EventPlugin, VersionedPlugin {
1413

1514
override val type: Plugin.Type = Plugin.Type.Enrichment
1615
override lateinit var analytics: Analytics
17-
var sessionID: Long = -1
16+
var sessionID = java.lang.System.currentTimeMillis()
1817
private val key = "Actions Amplitude"
1918
private var active = false
20-
21-
private var timer: TimerTask? = null
22-
private val fireTime: Long = sessionTimeoutMs
19+
private var lastEventFiredTime = java.lang.System.currentTimeMillis()
2320

2421
override fun update(settings: Settings, type: Plugin.UpdateType) {
2522
active = settings.hasIntegrationSettings(key)
2623
}
2724

28-
// Add the session_id to the Amplitude payload for cloud mode to handle.
29-
private inline fun <reified T : BaseEvent?> insertSession(payload: T?): BaseEvent? {
30-
var returnPayload = payload
31-
payload?.let {
32-
analytics.log(
33-
message = "Running ${payload.type} payload through AmplitudeSession",
34-
kind = LogKind.DEBUG
35-
)
36-
refreshSessionID()
37-
returnPayload =
38-
payload.putIntegrations(key, mapOf("session_id" to sessionID)) as T?
39-
}
40-
return returnPayload
41-
}
42-
4325
override fun execute(event: BaseEvent): BaseEvent? {
4426
if (!active) { // If amplitude destination is disabled, no need to do this enrichment
4527
return event
4628
}
4729

48-
var result: BaseEvent? = event
49-
when (result) {
50-
is IdentifyEvent -> {
51-
result = identify(result)
52-
}
53-
is TrackEvent -> {
54-
result = track(result)
55-
}
56-
is GroupEvent -> {
57-
result = group(result)
58-
}
59-
is ScreenEvent -> {
60-
result = screen(result)
61-
}
62-
is AliasEvent -> {
63-
result = alias(result)
64-
}
65-
else -> {}
66-
}
67-
return result
30+
val modified = super.execute(event)
31+
analytics.log(
32+
message = "Running ${event.type} payload through AmplitudeSession",
33+
kind = LogKind.DEBUG
34+
)
35+
lastEventFiredTime = java.lang.System.currentTimeMillis()
36+
37+
return modified?.putIntegrations(key, mapOf("session_id" to sessionID))
6838
}
6939

70-
private fun track(payload: TrackEvent): BaseEvent? {
40+
override fun track(payload: TrackEvent): BaseEvent {
7141
if (payload.event == "Application Backgrounded") {
7242
onBackground()
7343
} else if (payload.event == "Application Opened") {
7444
onForeground()
7545
}
76-
insertSession(payload)
77-
return payload
78-
}
79-
80-
private fun identify(payload: IdentifyEvent): BaseEvent? {
81-
insertSession(payload)
82-
return payload
83-
}
8446

85-
private fun screen(payload: ScreenEvent): BaseEvent? {
86-
insertSession(payload)
87-
return payload
88-
}
89-
90-
private fun group(payload: GroupEvent): BaseEvent? {
91-
insertSession(payload)
92-
return payload
93-
}
94-
95-
private fun alias(payload: AliasEvent): BaseEvent? {
96-
insertSession(payload)
9747
return payload
9848
}
9949

10050
private fun onBackground() {
10151
}
10252

10353
private fun onForeground() {
104-
refreshSessionID()
105-
}
106-
107-
private fun refreshSessionID() {
108-
if (sessionID == -1L) {
109-
// get a new session ID if we've been inactive for more than 5 min
110-
sessionID = Calendar.getInstance().timeInMillis
111-
}
112-
startTimer()
113-
}
114-
115-
private fun startTimer() {
116-
timer?.cancel()
117-
timer = Timer().schedule(fireTime) {
118-
// invalidate the session ID at the end of the timer
119-
stopTimer()
54+
val current = java.lang.System.currentTimeMillis()
55+
if (current - lastEventFiredTime >= sessionTimeoutMs) {
56+
sessionID = current
12057
}
12158
}
12259

123-
private fun stopTimer() {
124-
timer?.cancel()
125-
sessionID = -1
126-
}
127-
12860
override fun version(): String {
12961
return BuildConfig.VERSION_NAME
13062
}

0 commit comments

Comments
 (0)