From ef7aed89f24ec15bdf455322a1d6786504bc3c88 Mon Sep 17 00:00:00 2001 From: Manoel Aranda Neto Date: Tue, 5 Mar 2024 16:15:02 +0100 Subject: [PATCH 1/3] fix: opt out wasnt respected in capture method --- PostHog/PostHogSDK.swift | 32 ++++++++++++++++++++++++ PostHogTests/PostHogSDKTest.swift | 15 +++++++++++ PostHogTests/TestUtils/TestPostHog.swift | 6 ++--- 3 files changed, 50 insertions(+), 3 deletions(-) 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") } From ddbbf53ec94b3f232715c1c9354a73d3ff5bd674 Mon Sep 17 00:00:00 2001 From: Manoel Aranda Neto Date: Tue, 5 Mar 2024 16:15:48 +0100 Subject: [PATCH 2/3] changelog --- CHANGELOG.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 28984152b..66f8ce608 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,12 +1,14 @@ ## Next +- `optOut` wasn't respected in capture methods [#113](https://github.com/PostHog/posthog-ios/pull/113) + ## 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 From 51b490dd7a8fc600970d87e3d80b0a69c90e1a9c Mon Sep 17 00:00:00 2001 From: Manoel Aranda Neto Date: Tue, 5 Mar 2024 16:16:41 +0100 Subject: [PATCH 3/3] pr id --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 66f8ce608..a14f74252 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ ## Next -- `optOut` wasn't respected in capture methods [#113](https://github.com/PostHog/posthog-ios/pull/113) +- `optOut` wasn't respected in capture methods [#114](https://github.com/PostHog/posthog-ios/pull/114) ## 3.2.2 - 2024-03-01