|
| 1 | +package com.segment.analytics.destinations.plugins |
| 2 | + |
| 3 | +import androidx.lifecycle.Lifecycle |
| 4 | +import androidx.lifecycle.LifecycleObserver |
| 5 | +import androidx.lifecycle.OnLifecycleEvent |
| 6 | +import androidx.lifecycle.ProcessLifecycleOwner |
| 7 | +import com.segment.analytics.* |
| 8 | +import com.segment.analytics.platform.DestinationPlugin |
| 9 | +import com.segment.analytics.platform.plugins.LogType |
| 10 | +import com.segment.analytics.platform.plugins.log |
| 11 | +import com.segment.analytics.utilities.putIntegrations |
| 12 | +import java.util.* |
| 13 | +import kotlin.concurrent.schedule |
| 14 | + |
| 15 | +// A Destination plugin that adds session tracking to Amplitude cloud mode. |
| 16 | +class AmplitudeSession() : DestinationPlugin() { |
| 17 | + |
| 18 | + override val name: String = "AmplitudeSession" |
| 19 | + var sessionID: Long = -1 |
| 20 | + |
| 21 | + private var timer: TimerTask? = null |
| 22 | + private val fireTime: Long = 300000 |
| 23 | + |
| 24 | + override fun setup(analytics: Analytics) { |
| 25 | + super.setup(analytics) |
| 26 | + } |
| 27 | + |
| 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(message = "Running ${payload.type} payload through $name", event = payload, type = LogType.INFO) |
| 33 | + returnPayload = payload.putIntegrations("Amplitude", mapOf("session_id" to sessionID)) as T? |
| 34 | + } |
| 35 | + return returnPayload |
| 36 | + } |
| 37 | + |
| 38 | + override fun track(payload: TrackEvent): BaseEvent? { |
| 39 | + if (payload.event == "Application Backgrounded") { |
| 40 | + onBackground() |
| 41 | + } else if (payload.event == "Application Opened") { |
| 42 | + onForeground() |
| 43 | + } |
| 44 | + insertSession(payload) |
| 45 | + return payload |
| 46 | + } |
| 47 | + |
| 48 | + override fun identify(payload: IdentifyEvent): BaseEvent? { |
| 49 | + insertSession(payload) |
| 50 | + return payload |
| 51 | + } |
| 52 | + |
| 53 | + override fun screen(payload: ScreenEvent): BaseEvent? { |
| 54 | + insertSession(payload) |
| 55 | + return payload |
| 56 | + } |
| 57 | + |
| 58 | + override fun group(payload: GroupEvent): BaseEvent? { |
| 59 | + insertSession(payload) |
| 60 | + return payload |
| 61 | + } |
| 62 | + |
| 63 | + override fun alias(payload: AliasEvent): BaseEvent? { |
| 64 | + insertSession(payload) |
| 65 | + return payload |
| 66 | + } |
| 67 | + |
| 68 | + fun onBackground() { |
| 69 | + stopTimer() |
| 70 | + } |
| 71 | + |
| 72 | + fun onForeground() { |
| 73 | + startTimer() |
| 74 | + } |
| 75 | + |
| 76 | + fun startTimer() { |
| 77 | + |
| 78 | + // Set the session id |
| 79 | + sessionID = Calendar.getInstance().timeInMillis |
| 80 | + |
| 81 | + timer = Timer().schedule(fireTime) { |
| 82 | + stopTimer() |
| 83 | + startTimer() |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + fun stopTimer() { |
| 88 | + timer?.cancel() |
| 89 | + sessionID = -1 |
| 90 | + } |
| 91 | +} |
0 commit comments