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

fix: opt out wasnt respected in capture method #114

Merged
merged 3 commits into from
Mar 5, 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
8 changes: 5 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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

Expand Down
32 changes: 32 additions & 0 deletions PostHog/PostHogSDK.swift
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,10 @@ private let sessionChangeThreshold: TimeInterval = 60 * 30
return
}

if isOptOutState() {
return
}

guard let queue = queue, let sessionManager = sessionManager else {
return
}
Expand Down Expand Up @@ -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,
Expand All @@ -383,6 +395,10 @@ private let sessionChangeThreshold: TimeInterval = 60 * 30
return
}

if isOptOutState() {
return
}

guard let queue = queue else {
return
}
Expand Down Expand Up @@ -418,6 +434,10 @@ private let sessionChangeThreshold: TimeInterval = 60 * 30
return
}

if isOptOutState() {
return
}

guard let queue = queue else {
return
}
Expand All @@ -438,6 +458,10 @@ private let sessionChangeThreshold: TimeInterval = 60 * 30
return
}

if isOptOutState() {
return
}

guard let queue = queue else {
return
}
Expand Down Expand Up @@ -484,6 +508,10 @@ private let sessionChangeThreshold: TimeInterval = 60 * 30
return
}

if isOptOutState() {
return
}

guard let queue = queue else {
return
}
Expand Down Expand Up @@ -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))
Expand Down
15 changes: 15 additions & 0 deletions PostHogTests/PostHogSDKTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
6 changes: 3 additions & 3 deletions PostHogTests/TestUtils/TestPostHog.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down