|
1 | 1 | package com.segment.analytics.kotlin.destinations.amplitude |
2 | 2 |
|
3 | 3 | import com.segment.analytics.kotlin.core.* |
| 4 | +import com.segment.analytics.kotlin.core.platform.EventPlugin |
4 | 5 | import com.segment.analytics.kotlin.core.platform.Plugin |
5 | 6 | import com.segment.analytics.kotlin.core.platform.VersionedPlugin |
6 | 7 | import com.segment.analytics.kotlin.core.platform.plugins.logger.LogKind |
7 | 8 | import com.segment.analytics.kotlin.core.platform.plugins.logger.log |
8 | 9 | import com.segment.analytics.kotlin.core.utilities.putIntegrations |
9 | | -import java.util.* |
10 | | -import kotlin.concurrent.schedule |
11 | 10 |
|
12 | 11 | // 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 { |
14 | 13 |
|
15 | 14 | override val type: Plugin.Type = Plugin.Type.Enrichment |
16 | 15 | override lateinit var analytics: Analytics |
17 | | - var sessionID: Long = -1 |
| 16 | + var sessionID = java.lang.System.currentTimeMillis() |
18 | 17 | private val key = "Actions Amplitude" |
19 | 18 | 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() |
23 | 20 |
|
24 | 21 | override fun update(settings: Settings, type: Plugin.UpdateType) { |
25 | 22 | active = settings.hasIntegrationSettings(key) |
26 | 23 | } |
27 | 24 |
|
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 | | - |
43 | 25 | override fun execute(event: BaseEvent): BaseEvent? { |
44 | 26 | if (!active) { // If amplitude destination is disabled, no need to do this enrichment |
45 | 27 | return event |
46 | 28 | } |
47 | 29 |
|
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)) |
68 | 38 | } |
69 | 39 |
|
70 | | - private fun track(payload: TrackEvent): BaseEvent? { |
| 40 | + override fun track(payload: TrackEvent): BaseEvent { |
71 | 41 | if (payload.event == "Application Backgrounded") { |
72 | 42 | onBackground() |
73 | 43 | } else if (payload.event == "Application Opened") { |
74 | 44 | onForeground() |
75 | 45 | } |
76 | | - insertSession(payload) |
77 | | - return payload |
78 | | - } |
79 | | - |
80 | | - private fun identify(payload: IdentifyEvent): BaseEvent? { |
81 | | - insertSession(payload) |
82 | | - return payload |
83 | | - } |
84 | 46 |
|
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) |
97 | 47 | return payload |
98 | 48 | } |
99 | 49 |
|
100 | 50 | private fun onBackground() { |
101 | 51 | } |
102 | 52 |
|
103 | 53 | 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 |
120 | 57 | } |
121 | 58 | } |
122 | 59 |
|
123 | | - private fun stopTimer() { |
124 | | - timer?.cancel() |
125 | | - sessionID = -1 |
126 | | - } |
127 | | - |
128 | 60 | override fun version(): String { |
129 | 61 | return BuildConfig.VERSION_NAME |
130 | 62 | } |
|
0 commit comments