Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update session logic #15

Merged
merged 1 commit into from
Jun 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,130 +1,62 @@
package com.segment.analytics.kotlin.destinations.amplitude

import com.segment.analytics.kotlin.core.*
import com.segment.analytics.kotlin.core.platform.EventPlugin
import com.segment.analytics.kotlin.core.platform.Plugin
import com.segment.analytics.kotlin.core.platform.VersionedPlugin
import com.segment.analytics.kotlin.core.platform.plugins.logger.LogKind
import com.segment.analytics.kotlin.core.platform.plugins.logger.log
import com.segment.analytics.kotlin.core.utilities.putIntegrations
import java.util.*
import kotlin.concurrent.schedule

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

override val type: Plugin.Type = Plugin.Type.Enrichment
override lateinit var analytics: Analytics
var sessionID: Long = -1
var sessionID = java.lang.System.currentTimeMillis()
private val key = "Actions Amplitude"
private var active = false

private var timer: TimerTask? = null
private val fireTime: Long = sessionTimeoutMs
private var lastEventFiredTime = java.lang.System.currentTimeMillis()

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

// Add the session_id to the Amplitude payload for cloud mode to handle.
private inline fun <reified T : BaseEvent?> insertSession(payload: T?): BaseEvent? {
var returnPayload = payload
payload?.let {
analytics.log(
message = "Running ${payload.type} payload through AmplitudeSession",
kind = LogKind.DEBUG
)
refreshSessionID()
returnPayload =
payload.putIntegrations(key, mapOf("session_id" to sessionID)) as T?
}
return returnPayload
}

override fun execute(event: BaseEvent): BaseEvent? {
if (!active) { // If amplitude destination is disabled, no need to do this enrichment
return event
}

var result: BaseEvent? = event
when (result) {
is IdentifyEvent -> {
result = identify(result)
}
is TrackEvent -> {
result = track(result)
}
is GroupEvent -> {
result = group(result)
}
is ScreenEvent -> {
result = screen(result)
}
is AliasEvent -> {
result = alias(result)
}
else -> {}
}
return result
val modified = super.execute(event)
analytics.log(
message = "Running ${event.type} payload through AmplitudeSession",
kind = LogKind.DEBUG
)
lastEventFiredTime = java.lang.System.currentTimeMillis()

return modified?.putIntegrations(key, mapOf("session_id" to sessionID))
}

private fun track(payload: TrackEvent): BaseEvent? {
override fun track(payload: TrackEvent): BaseEvent {
if (payload.event == "Application Backgrounded") {
onBackground()
} else if (payload.event == "Application Opened") {
onForeground()
}
insertSession(payload)
return payload
}

private fun identify(payload: IdentifyEvent): BaseEvent? {
insertSession(payload)
return payload
}

private fun screen(payload: ScreenEvent): BaseEvent? {
insertSession(payload)
return payload
}

private fun group(payload: GroupEvent): BaseEvent? {
insertSession(payload)
return payload
}

private fun alias(payload: AliasEvent): BaseEvent? {
insertSession(payload)
return payload
}

private fun onBackground() {
}

private fun onForeground() {
refreshSessionID()
}

private fun refreshSessionID() {
if (sessionID == -1L) {
// get a new session ID if we've been inactive for more than 5 min
sessionID = Calendar.getInstance().timeInMillis
}
startTimer()
}

private fun startTimer() {
timer?.cancel()
timer = Timer().schedule(fireTime) {
// invalidate the session ID at the end of the timer
stopTimer()
val current = java.lang.System.currentTimeMillis()
if (current - lastEventFiredTime >= sessionTimeoutMs) {
sessionID = current
}
}

private fun stopTimer() {
timer?.cancel()
sessionID = -1
}

override fun version(): String {
return BuildConfig.VERSION_NAME
}
Expand Down