Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/brave-sessions-remain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"posthog-ios": patch
---

Keep tracking foreground and background transitions after `reset()`. Previously `reset()` unsubscribed the session manager from app lifecycle and UI events, so sessions stopped clearing in the background, or were never created again after a reset in the background, until the app restarted.
3 changes: 0 additions & 3 deletions PostHog/PostHogSessionManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,6 @@ import Foundation

func reset() {
resetSession()
didBecomeActiveToken = nil
didEnterBackgroundToken = nil
applicationEventToken = nil
}

private let queue = DispatchQueue(label: "com.posthog.PostHogSessionManager", target: .global(qos: .utility))
Expand Down
35 changes: 35 additions & 0 deletions PostHogTests/PostHogSessionManagerTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,41 @@ enum PostHogSessionManagerTest {

#expect(newSessionId != originalSessionId)
}

@Test("reset() keeps tracking the app moving to the background")
func resetKeepsBackgroundTracking() throws {
let mockNow = MockDate()
now = { mockNow.date }
let posthog = getSut()

mockAppLifecycle.simulateAppDidBecomeActive()
posthog.reset()
try #require(posthog.getSessionManager()?.getSessionId(readOnly: true) != nil)

mockAppLifecycle.simulateAppDidEnterBackground()
mockNow.date.addTimeInterval(60 * 31) // past 30 minutes in the background (session should clear)

#expect(posthog.getSessionManager()?.getSessionId() == nil)
}

@Test("reset() keeps tracking the app moving to the foreground")
func resetKeepsForegroundTracking() throws {
let mockNow = MockDate()
now = { mockNow.date }
let posthog = getSut()

mockAppLifecycle.simulateAppDidBecomeActive()
mockAppLifecycle.simulateAppDidEnterBackground()
posthog.reset()
let resetSessionId = try #require(posthog.getSessionManager()?.getSessionId(readOnly: true))

mockAppLifecycle.simulateAppDidBecomeActive()
mockNow.date.addTimeInterval(60 * 31) // past 30 minutes in the foreground (session should rotate, not clear)
let newSessionId = posthog.getSessionManager()?.getSessionId()

#expect(newSessionId != nil)
#expect(newSessionId != resetSessionId)
}
}

@Suite("Test $session_id property in events")
Expand Down
Loading