Skip to content

Commit 8cac126

Browse files
authored
fix: is_identified fallback value (#317)
* fix: is_identified fallback value * chore: add test
1 parent 05881a3 commit 8cac126

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## Next
22

3+
- fix: wrong is_identified fallback value ([#317](https://github.com/PostHog/posthog-ios/pull/317))
4+
35
## 3.20.0 - 2025-03-04
46

57
- feat: support multiple SDK instances ([#310](https://github.com/PostHog/posthog-ios/pull/310))

PostHog/PostHogStorageManager.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public class PostHogStorageManager {
9595
public func isIdentified() -> Bool {
9696
identifiedLock.withLock {
9797
if isIdentifiedValue == nil {
98-
isIdentifiedValue = storage.getBool(forKey: .isIdentified) ?? (getDistinctId() != getDistinctId())
98+
isIdentifiedValue = storage.getBool(forKey: .isIdentified) ?? (getDistinctId() != getAnonymousId())
9999
}
100100
}
101101
return isIdentifiedValue ?? false

PostHogTests/PostHogStorageManagerTest.swift

+21-5
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ import Nimble
1111
import Quick
1212

1313
class PostHogStorageManagerTest: QuickSpec {
14-
func getSut() -> PostHogStorageManager {
15-
let config = PostHogConfig(apiKey: "123")
16-
return PostHogStorageManager(config)
14+
func getSut(_ config: PostHogConfig? = nil) -> PostHogStorageManager {
15+
let theConfig = config ?? PostHogConfig(apiKey: "123")
16+
let storage = PostHogStorage(theConfig)
17+
storage.reset()
18+
return PostHogStorageManager(theConfig)
1719
}
1820

1921
override func spec() {
@@ -46,15 +48,29 @@ class PostHogStorageManagerTest: QuickSpec {
4648
sut.reset(true)
4749
}
4850

49-
it("Can can accept id customization via config") {
51+
it("Can accept anon id customization via config") {
5052
let config = PostHogConfig(apiKey: "123")
5153
let fixedUuid = UUID.v7()
5254
config.getAnonymousId = { _ in fixedUuid }
53-
let sut = PostHogStorageManager(config)
55+
let sut = self.getSut(config)
5456
let anonymousId = sut.getAnonymousId()
5557
expect(anonymousId) == fixedUuid.uuidString
5658

5759
sut.reset(true)
5860
}
61+
62+
it("Uses the correct fallback value for isIdentified") {
63+
let anonymousIdToSet = UUID.v7()
64+
let distinctIdToSet = UUID.v7().uuidString
65+
66+
let config = PostHogConfig(apiKey: "123")
67+
config.getAnonymousId = { _ in anonymousIdToSet }
68+
69+
let sut = self.getSut(config)
70+
sut.setDistinctId(distinctIdToSet)
71+
72+
// Don't call setIdentified(true), isIdentified should be derived from different anon and distinct ids
73+
expect(sut.isIdentified()) == true
74+
}
5975
}
6076
}

0 commit comments

Comments
 (0)