-
Notifications
You must be signed in to change notification settings - Fork 26
feat: auto-rotate session after 24 hours #247
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
package com.posthog | ||
|
||
import com.posthog.internal.MAX_SESSION_AGE_MILLIS | ||
import com.posthog.internal.PostHogApi | ||
import com.posthog.internal.PostHogApiEndpoint | ||
import com.posthog.internal.PostHogMemoryPreferences | ||
|
@@ -432,7 +433,7 @@ public class PostHog private constructor( | |
} | ||
|
||
val mergedProperties = | ||
buildProperties( | ||
enforceMaxSessionLength(buildProperties( | ||
newDistinctId, | ||
properties = properties, | ||
userProperties = userProperties, | ||
|
@@ -442,7 +443,7 @@ public class PostHog private constructor( | |
appendSharedProps = !snapshotEvent, | ||
// only append groups if not a group identify event and not a snapshot | ||
appendGroups = !groupIdentify, | ||
) | ||
)) | ||
|
||
// sanitize the properties or fallback to the original properties | ||
val sanitizedProperties = config?.propertiesSanitizer?.sanitize(mergedProperties.toMutableMap()) ?: mergedProperties | ||
|
@@ -466,6 +467,30 @@ public class PostHog private constructor( | |
} | ||
} | ||
|
||
private fun enforceMaxSessionLength(buildProperties: Map<String, Any>): Map<String, Any> { | ||
val sessionId = buildProperties["\$session_id"] ?: return buildProperties | ||
|
||
val sessionStartTimestamp = | ||
TimeBasedEpochGenerator.getTimestampFromUuid(UUID.fromString(sessionId.toString())) | ||
?: return buildProperties | ||
|
||
val currentTimeMillis = | ||
config?.dateProvider?.currentTimeMillis() ?: System.currentTimeMillis() | ||
val sessionAge = currentTimeMillis - sessionStartTimestamp | ||
if (sessionAge > MAX_SESSION_AGE_MILLIS) { | ||
// rotate session | ||
// update properties | ||
PostHogSessionManager.endSession() | ||
PostHogSessionManager.startSession(currentTimeMillis) | ||
Comment on lines
+490
to
+491
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this changes the session id but the session replay would need to be notified to eg send a new full snapshot with metadata etc otherwise it'll assume its already sent and the replay will be broken (missing meta event and full snapshot for the new session id) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤔 in the JS SDK we have an event emitter that we pass around that can be used for this kind of "onBlah" behavior I guess the sessionManager can have a Is there a more android-y way of doing it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah a callback would be one way, since |
||
val newSessionId = PostHogSessionManager.getActiveSessionId().toString() | ||
val newProps = buildProperties.toMutableMap() | ||
newProps["\$session_id"] = newSessionId | ||
newProps["\$window_id"] = newSessionId | ||
return newProps | ||
} | ||
return buildProperties | ||
} | ||
|
||
public override fun optIn() { | ||
if (!isEnabled()) { | ||
return | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,8 @@ import com.posthog.PostHogInternal | |
import com.posthog.vendor.uuid.TimeBasedEpochGenerator | ||
import java.util.UUID | ||
|
||
public const val MAX_SESSION_AGE_MILLIS: Int = 24 * 60 * 60 * 60 | ||
pauldambra marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can it be private instead? |
||
|
||
/** | ||
* Class that manages the Session ID | ||
*/ | ||
|
@@ -16,10 +18,10 @@ public object PostHogSessionManager { | |
|
||
private var sessionId = sessionIdNone | ||
|
||
public fun startSession() { | ||
public fun startSession(sessionStartTime: Long? = null) { | ||
synchronized(sessionLock) { | ||
if (sessionId == sessionIdNone) { | ||
sessionId = TimeBasedEpochGenerator.generate() | ||
sessionId = TimeBasedEpochGenerator.generate(sessionStartTime ?: System.currentTimeMillis()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I passed the time in here... but I guess it's only for tests so I suppose this is OK There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thats fine, another approach would be to make a TimeBasedEpochGenerator an interface, and the current TimeBasedEpochGenerator is the production implementation and we could have a test implementation and swap during SDK initialization just for testing, the prod code would be cleaner but we would pay the price of an extra interface |
||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,10 +29,10 @@ internal class FakePostHogDateProvider : PostHogDateProvider { | |
} | ||
|
||
override fun currentTimeMillis(): Long { | ||
return System.currentTimeMillis() | ||
return currentDate?.time ?: System.currentTimeMillis() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this threw me 🤣 |
||
} | ||
|
||
override fun nanoTime(): Long { | ||
return System.nanoTime() | ||
return currentDate?.time?.times(1000000) ?: System.nanoTime() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so I updated here too |
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
react native has its own session id rotation so we should not do this for RN I think
https://github.com/PostHog/posthog-js-lite/blob/b8372284df5489457d95b16b0e4b12a91dd66c26/posthog-core/src/index.ts#L1309-L1315
otherwise they will be out of sync (RN uses a different session id than Android)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so, needs to be configurable? and then RN would turn off the Android one?
or sync up a release and RN stops doing its own?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, we have something similar for flutter already
today the way how it works is react native manages the session id and update the session id on android when its rotated.
now we want to do the other way around, from android to call the react native but you cant do this because the bridge only works in one direction, so its better to let the react native manage the whole session id thing otherwise they will be out of sync