diff --git a/CHANGELOG.md b/CHANGELOG.md index e466e5442..2d50d4b1b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/Package.resolved b/Package.resolved index caad2dd4d..113b208ec 100644 --- a/Package.resolved +++ b/Package.resolved @@ -6,8 +6,8 @@ "repositoryURL": "https://github.com/mattgallagher/CwlCatchException.git", "state": { "branch": null, - "revision": "3b123999de19bf04905bc1dfdb76f817b0f2cc00", - "version": "2.1.2" + "revision": "3ef6999c73b6938cc0da422f2c912d0158abb0a0", + "version": "2.2.0" } }, { @@ -15,8 +15,8 @@ "repositoryURL": "https://github.com/mattgallagher/CwlPreconditionTesting.git", "state": { "branch": null, - "revision": "a23ded2c91df9156628a6996ab4f347526f17b6b", - "version": "2.1.2" + "revision": "2ef56b2caf25f55fa7eef8784c30d5a767550f54", + "version": "2.2.1" } }, { diff --git a/PostHog/PostHogConfig.swift b/PostHog/PostHogConfig.swift index 924468906..e54df3ed1 100644 --- a/PostHog/PostHogConfig.swift +++ b/PostHog/PostHogConfig.swift @@ -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/" diff --git a/PostHog/PostHogSessionManager.swift b/PostHog/PostHogSessionManager.swift index 0c771f524..72ea396f2 100644 --- a/PostHog/PostHogSessionManager.swift +++ b/PostHog/PostHogSessionManager.swift @@ -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 } public func getAnonymousId() -> String { @@ -22,7 +25,7 @@ class PostHogSessionManager { anonymousId = storage.getString(forKey: .anonymousId) if anonymousId == nil { - anonymousId = UUID().uuidString + anonymousId = idGen(UUID()).uuidString setAnonId(anonymousId ?? "") } } diff --git a/PostHogTests/PostHogSessionManagerTest.swift b/PostHogTests/PostHogSessionManagerTest.swift index 3ae343116..a445daebd 100644 --- a/PostHogTests/PostHogSessionManagerTest.swift +++ b/PostHogTests/PostHogSessionManagerTest.swift @@ -45,5 +45,16 @@ class PostHogSessionManagerTest: QuickSpec { sut.reset() } + + it("Can can accept id customization via config") { + 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() + } } }