diff --git a/CHANGELOG.md b/CHANGELOG.md index 28984152b..a14f74252 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,12 +1,14 @@ ## Next +- `optOut` wasn't respected in capture methods [#114](https://github.com/PostHog/posthog-ios/pull/114) + ## 3.2.2 - 2024-03-01 -- API requests do a 10s timeoutInterval instead of 60s [#113](https://github.com/PostHog/posthog-ios/pull/113) +- API requests do a 10s timeoutInterval instead of 60s [#113](https://github.com/PostHog/posthog-ios/pull/113) ## 3.2.1 - 2024-02-26 -- PrivacyInfo manifest set in the SPM and CocoaPods config [#112](https://github.com/PostHog/posthog-ios/pull/112) +- PrivacyInfo manifest set in the SPM and CocoaPods config [#112](https://github.com/PostHog/posthog-ios/pull/112) ## 3.2.0 - 2024-02-23 @@ -110,7 +112,7 @@ Check out the [USAGE](https://github.com/PostHog/posthog-ios/blob/main/USAGE.md) ## 2.0.0 - 2022-08-29 -- Add support for groups, simplefeature flags, and multivariate feature flags +- Add support for groups, simplefeature flags, and multivariate feature flags ## 1.4.4 - 2021-11-19 diff --git a/PostHog/PostHogSDK.swift b/PostHog/PostHogSDK.swift index c1be3517a..422f299c7 100644 --- a/PostHog/PostHogSDK.swift +++ b/PostHog/PostHogSDK.swift @@ -321,6 +321,10 @@ private let sessionChangeThreshold: TimeInterval = 60 * 30 return } + if isOptOutState() { + return + } + guard let queue = queue, let sessionManager = sessionManager else { return } @@ -372,6 +376,14 @@ private let sessionChangeThreshold: TimeInterval = 60 * 30 capture(event, properties: properties, userProperties: userProperties, userPropertiesSetOnce: userPropertiesSetOnce, groupProperties: nil) } + private func isOptOutState() -> Bool { + if config.optOut { + hedgeLog("PostHog is in OptOut state.") + return true + } + return false + } + @objc(captureWithEvent:properties:userProperties:userPropertiesSetOnce:groupProperties:) public func capture(_ event: String, properties: [String: Any]? = nil, @@ -383,6 +395,10 @@ private let sessionChangeThreshold: TimeInterval = 60 * 30 return } + if isOptOutState() { + return + } + guard let queue = queue else { return } @@ -418,6 +434,10 @@ private let sessionChangeThreshold: TimeInterval = 60 * 30 return } + if isOptOutState() { + return + } + guard let queue = queue else { return } @@ -438,6 +458,10 @@ private let sessionChangeThreshold: TimeInterval = 60 * 30 return } + if isOptOutState() { + return + } + guard let queue = queue else { return } @@ -484,6 +508,10 @@ private let sessionChangeThreshold: TimeInterval = 60 * 30 return } + if isOptOutState() { + return + } + guard let queue = queue else { return } @@ -516,6 +544,10 @@ private let sessionChangeThreshold: TimeInterval = 60 * 30 return } + if isOptOutState() { + return + } + _ = groups([type: key]) groupIdentify(type: type, key: key, groupProperties: sanitizeDicionary(groupProperties)) diff --git a/PostHogTests/PostHogSDKTest.swift b/PostHogTests/PostHogSDKTest.swift index ab59b0fbd..9a89889ec 100644 --- a/PostHogTests/PostHogSDKTest.swift +++ b/PostHogTests/PostHogSDKTest.swift @@ -205,6 +205,21 @@ class PostHogSDKTest: QuickSpec { sut.close() } + it("does not capture event if opt out") { + let sut = self.getSut() + + sut.optOut() + + sut.capture("event") + + // no need to await 15s + let events = getBatchedEvents(server, timeout: 1.0, failIfNotCompleted: false) + expect(events.count) == 0 + + sut.reset() + sut.close() + } + it("calls reloadFeatureFlags") { let sut = self.getSut() diff --git a/PostHogTests/TestUtils/TestPostHog.swift b/PostHogTests/TestUtils/TestPostHog.swift index ee6107c4a..8351616d9 100644 --- a/PostHogTests/TestUtils/TestPostHog.swift +++ b/PostHogTests/TestUtils/TestPostHog.swift @@ -9,10 +9,10 @@ import Foundation import PostHog import XCTest -func getBatchedEvents(_ server: MockPostHogServer) -> [PostHogEvent] { - let result = XCTWaiter.wait(for: [server.batchExpectation!], timeout: 15.0) +func getBatchedEvents(_ server: MockPostHogServer, timeout: TimeInterval = 15.0, failIfNotCompleted: Bool = true) -> [PostHogEvent] { + let result = XCTWaiter.wait(for: [server.batchExpectation!], timeout: timeout) - if result != XCTWaiter.Result.completed { + if result != XCTWaiter.Result.completed, failIfNotCompleted { XCTFail("The expected requests never arrived") }