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

recording: send meta event again if session rotates #183

Merged
merged 5 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## Next

- chore: add SwiftUI View extensions to capture screen view and views in general (postHogViewEvent, postHogScreenView) ([#180](https://github.com/PostHog/posthog-ios/pull/180))
- recording: send meta event again if session rotates ([#183](https://github.com/PostHog/posthog-ios/pull/183))

## 3.8.3 - 2024-09-03

Expand Down
32 changes: 26 additions & 6 deletions PostHog/PostHogSDK.swift
Original file line number Diff line number Diff line change
Expand Up @@ -183,15 +183,19 @@ let maxRetryDelay = 30.0
return
}

return PostHogSessionManager.shared.startSession()
PostHogSessionManager.shared.startSession {
self.resetViews()
}
}

@objc public func endSession() {
if !isEnabled() {
return
}

return PostHogSessionManager.shared.endSession()
PostHogSessionManager.shared.endSession {
self.resetViews()
}
}

// EVENT CAPTURE
Expand Down Expand Up @@ -310,13 +314,23 @@ let maxRetryDelay = 30.0
// storage also removes all feature flags
storage?.reset()
flagCallReported.removeAll()
PostHogSessionManager.shared.endSession()
PostHogSessionManager.shared.endSession {
self.resetViews()
}
PostHogSessionManager.shared.startSession()

// reload flags as anon user
reloadFeatureFlags()
}

private func resetViews() {
#if os(iOS)
if config.sessionReplay, featureFlags?.isSessionReplayFlagActive() ?? false {
replayIntegration?.resetViews()
}
#endif
}

private func getGroups() -> [String: String] {
guard let groups = storage?.getDictionary(forKey: .groups) as? [String: String] else {
return [:]
Expand Down Expand Up @@ -471,7 +485,9 @@ let maxRetryDelay = 30.0

// If events fire in the background after the threshold, they should no longer have a sessionId
if isInBackground {
PostHogSessionManager.shared.resetSessionIfExpired()
PostHogSessionManager.shared.resetSessionIfExpired {
self.resetViews()
}
}

let distinctId = getDistinctId()
Expand Down Expand Up @@ -814,7 +830,9 @@ let maxRetryDelay = 30.0
#endif
flagCallReported.removeAll()
context = nil
PostHogSessionManager.shared.endSession()
PostHogSessionManager.shared.endSession {
self.resetViews()
}
unregisterNotifications()
capturedAppInstalled = false
appFromBackground = false
Expand Down Expand Up @@ -948,7 +966,9 @@ let maxRetryDelay = 30.0
}

@objc func handleAppDidBecomeActive() {
PostHogSessionManager.shared.rotateSessionIdIfRequired()
PostHogSessionManager.shared.rotateSessionIdIfRequired {
self.resetViews()
}

isInBackground = false
captureAppOpened()
Expand Down
19 changes: 11 additions & 8 deletions PostHog/PostHogSessionManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,58 +33,61 @@ class PostHogSessionManager {
}
}

func endSession() {
func endSession(_ completion: () -> Void) {
sessionLock.withLock {
sessionId = nil
sessionLastTimestamp = nil
completion()
}
}

private func isExpired(_ timeNow: TimeInterval, _ sessionLastTimestamp: TimeInterval) -> Bool {
timeNow - sessionLastTimestamp > sessionChangeThreshold
}

func resetSessionIfExpired() {
func resetSessionIfExpired(_ completion: () -> Void) {
sessionLock.withLock {
let timeNow = now().timeIntervalSince1970
if sessionId != nil,
let sessionLastTimestamp = sessionLastTimestamp,
isExpired(timeNow, sessionLastTimestamp)
{
sessionId = nil
completion()
}
}
}

private func rotateSession() {
private func rotateSession(_ completion: (() -> Void)?) {
let newSessionId = UUID.v7().uuidString
let newSessionLastTimestamp = now().timeIntervalSince1970

sessionId = newSessionId
sessionLastTimestamp = newSessionLastTimestamp
completion?()
}

func startSession() {
func startSession(_ completion: (() -> Void)? = nil) {
sessionLock.withLock {
// only start if there is no session
if sessionId != nil {
return
}
rotateSession()
rotateSession(completion)
}
}

func rotateSessionIdIfRequired() {
func rotateSessionIdIfRequired(_ completion: @escaping (() -> Void)) {
sessionLock.withLock {
let timeNow = now().timeIntervalSince1970

guard sessionId != nil, let sessionLastTimestamp = sessionLastTimestamp else {
rotateSession()
rotateSession(completion)
return
}

if isExpired(timeNow, sessionLastTimestamp) {
rotateSession()
rotateSession(completion)
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions PostHog/Replay/PostHogReplayIntegration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@
timer = nil
}

func resetViews() {
windowViews.removeAllObjects()
}

private func generateSnapshot(_ view: UIView, _ screenName: String? = nil) {
var hasChanges = false

Expand Down
2 changes: 1 addition & 1 deletion PostHogTests/PostHogSDKTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class PostHogSDKTest: QuickSpec {
now = { Date() }
server.stop()
server = nil
PostHogSessionManager.shared.endSession()
PostHogSessionManager.shared.endSession {}
}

it("captures the capture event") {
Expand Down
Loading