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

allow anonymous id generation to be configurable #133

Merged
merged 3 commits into from
May 21, 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

- allow anonymous id generation to be configurable ([#133](https://github.com/PostHog/posthog-ios/pull/133))

## 3.3.0 - 2024-05-21

- chore: apply patches from 3.2.5 to 3.3.0 and session recording fixes [#135](https://github.com/PostHog/posthog-ios/pull/135)
Expand Down
8 changes: 4 additions & 4 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@
"repositoryURL": "https://github.com/mattgallagher/CwlCatchException.git",
"state": {
"branch": null,
"revision": "3b123999de19bf04905bc1dfdb76f817b0f2cc00",
"version": "2.1.2"
"revision": "3ef6999c73b6938cc0da422f2c912d0158abb0a0",
"version": "2.2.0"
}
},
{
"package": "CwlPreconditionTesting",
"repositoryURL": "https://github.com/mattgallagher/CwlPreconditionTesting.git",
"state": {
"branch": null,
"revision": "a23ded2c91df9156628a6996ab4f347526f17b6b",
"version": "2.1.2"
"revision": "2ef56b2caf25f55fa7eef8784c30d5a767550f54",
"version": "2.2.1"
}
},
{
Expand Down
1 change: 1 addition & 0 deletions PostHog/PostHogConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import Foundation
@objc public var captureScreenViews: Bool = true
@objc public var debug: Bool = false
@objc public var optOut: Bool = false
@objc public var getAnonymousId: ((UUID) -> UUID) = { uuid in uuid }
/// Internal
var snapshotEndpoint: String = "/s/"

Expand Down
5 changes: 4 additions & 1 deletion PostHog/PostHogSessionManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ class PostHogSessionManager {

private let anonLock = NSLock()
private let distinctLock = NSLock()
private let idGen: (UUID) -> UUID

init(_ config: PostHogConfig) {
storage = PostHogStorage(config)
idGen = config.getAnonymousId
marandaneto marked this conversation as resolved.
Show resolved Hide resolved
}

public func getAnonymousId() -> String {
Expand All @@ -22,7 +25,7 @@ class PostHogSessionManager {
anonymousId = storage.getString(forKey: .anonymousId)

if anonymousId == nil {
anonymousId = UUID().uuidString
anonymousId = idGen(UUID()).uuidString
marandaneto marked this conversation as resolved.
Show resolved Hide resolved
setAnonId(anonymousId ?? "")
}
}
Expand Down
11 changes: 11 additions & 0 deletions PostHogTests/PostHogSessionManagerTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,16 @@ class PostHogSessionManagerTest: QuickSpec {

sut.reset()
}

it("Can can accept id customization via config") {
marandaneto marked this conversation as resolved.
Show resolved Hide resolved
let config = PostHogConfig(apiKey: "123")
let fixedUuid = UUID()
config.getAnonymousId = { _ in fixedUuid }
let sut = PostHogSessionManager(config)
let anonymousId = sut.getAnonymousId()
expect(anonymousId) == fixedUuid.uuidString

sut.reset()
}
}
}
Loading