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

fix reset session when reset or close are called #107

Merged
merged 4 commits into from
Feb 18, 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## Next

- fix reset session when reset or close are called [#107](https://github.com/PostHog/posthog-ios/pull/107)

## 3.1.3 - 2024-02-09

- fix ISO8601 formatter to always use the 24h format [#106](https://github.com/PostHog/posthog-ios/pull/106)
Expand Down
33 changes: 33 additions & 0 deletions PostHog/PostHogSDK.swift
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,18 @@ private let sessionChangeThreshold: TimeInterval = 60 * 30
return
}

// storage also removes all feature flags
storage?.reset()
queue?.clear()
flagCallReported.removeAll()
resetSession()
}

private func resetSession() {
sessionLock.withLock {
sessionId = nil
sessionLastTimestamp = nil
}
}

private func getGroups() -> [String: String] {
Expand Down Expand Up @@ -674,12 +683,22 @@ private let sessionChangeThreshold: TimeInterval = 60 * 30
sessionManager = nil
config = PostHogConfig(apiKey: "")
api = nil
featureFlags = nil
storage = nil
#if !os(watchOS)
self.reachability?.stopNotifier()
reachability = nil
#endif
flagCallReported.removeAll()
featureFlags = nil
context = nil
resetSession()
unregisterNotifications()
capturedAppInstalled = false
appFromBackground = false
isInBackground = false
toggleHedgeLog(false)
// TODO: remove swizzlers
}
}

Expand All @@ -689,6 +708,20 @@ private let sessionChangeThreshold: TimeInterval = 60 * 30
return postHog
}

private func unregisterNotifications() {
let defaultCenter = NotificationCenter.default

#if os(iOS) || os(tvOS)
defaultCenter.removeObserver(self, name: UIApplication.didFinishLaunchingNotification, object: nil)
defaultCenter.removeObserver(self, name: UIApplication.didEnterBackgroundNotification, object: nil)
defaultCenter.removeObserver(self, name: UIApplication.didBecomeActiveNotification, object: nil)
#elseif os(macOS)
defaultCenter.removeObserver(self, name: NSApplication.didFinishLaunchingNotification, object: nil)
defaultCenter.removeObserver(self, name: NSApplication.didResignActiveNotification, object: nil)
defaultCenter.removeObserver(self, name: NSApplication.didBecomeActiveNotification, object: nil)
#endif
}

private func registerNotifications() {
let defaultCenter = NotificationCenter.default

Expand Down
30 changes: 30 additions & 0 deletions PostHogTests/PostHogSDKTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,36 @@ class PostHogSDKTest: QuickSpec {
sut.reset()
sut.close()
}

it("clears sessionId after reset") {
let sut = self.getSut(captureApplicationLifecycleEvents: true, flushAt: 1)
let mockNow = MockDate()
sut.now = { mockNow.date }

sut.capture("event captured with session")

var events = getBatchedEvents(server)
expect(events.count) == 1

expect(events[0].properties["$session_id"] as? String).toNot(beNil())

sut.reset()

server.stop()
server = nil
server = MockPostHogServer()
server.start()

sut.capture("event captured w/o session")

events = getBatchedEvents(server)
expect(events.count) == 1

expect(events[0].properties["$session_id"] as? String).to(beNil())

sut.reset()
sut.close()
}
}
}

Expand Down