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: feature_flag_called after reloadFeatureFlags #232

Merged
merged 8 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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

- fix $feature_flag_called not captured after reloading flags ([#232](https://github.com/PostHog/posthog-ios/pull/232))

## 3.14.1 - 2024-11-05

- recording: fix RN iOS masking ([#230](https://github.com/PostHog/posthog-ios/pull/230))
Expand Down
32 changes: 22 additions & 10 deletions PostHog/PostHogSDK.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ let maxRetryDelay = 30.0
private let setupLock = NSLock()
private let optOutLock = NSLock()
private let groupsLock = NSLock()
private let featureFlagsLock = NSLock()
ioannisj marked this conversation as resolved.
Show resolved Hide resolved
private let personPropsLock = NSLock()

private var queue: PostHogQueue?
Expand Down Expand Up @@ -345,7 +346,9 @@ let maxRetryDelay = 30.0
// storage also removes all feature flags
storage?.reset()
config.storageManager?.reset()
flagCallReported.removeAll()
featureFlagsLock.withLock {
flagCallReported.removeAll()
}
PostHogSessionManager.shared.endSession {
self.resetViews()
}
Expand Down Expand Up @@ -791,7 +794,12 @@ let maxRetryDelay = 30.0
distinctId: storageManager.getDistinctId(),
anonymousId: storageManager.getAnonymousId(),
groups: groups ?? [:],
callback: callback
callback: {
self.featureFlagsLock.withLock {
self.flagCallReported.removeAll()
}
callback()
}
)
}

Expand Down Expand Up @@ -844,15 +852,17 @@ let maxRetryDelay = 30.0
}

private func reportFeatureFlagCalled(flagKey: String, flagValue: Any?) {
if !flagCallReported.contains(flagKey) {
let properties: [String: Any] = [
"$feature_flag": flagKey,
"$feature_flag_response": flagValue ?? "",
]
featureFlagsLock.withLock {
if !flagCallReported.contains(flagKey) {
let properties: [String: Any] = [
"$feature_flag": flagKey,
"$feature_flag_response": flagValue ?? "",
]

flagCallReported.insert(flagKey)
flagCallReported.insert(flagKey)

capture("$feature_flag_called", properties: properties)
capture("$feature_flag_called", properties: properties)
ioannisj marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

Expand Down Expand Up @@ -926,7 +936,9 @@ let maxRetryDelay = 30.0
self.reachability?.stopNotifier()
reachability = nil
#endif
flagCallReported.removeAll()
featureFlagsLock.withLock {
flagCallReported.removeAll()
}
context = nil
PostHogSessionManager.shared.endSession {
self.resetViews()
Expand Down
48 changes: 48 additions & 0 deletions PostHogTests/PostHogSDKTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,21 @@ import Nimble
import Quick

@testable import PostHog
import XCTest
Copy link
Member

@marandaneto marandaneto Nov 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

was that missing before? i think its not needed when using Nimble

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😩 #234


class PostHogSDKTest: QuickSpec {
func getSut(preloadFeatureFlags: Bool = false,
sendFeatureFlagEvent: Bool = false,
captureApplicationLifecycleEvents: Bool = false,
flushAt: Int = 1,
maxBatchSize: Int = 50,
optOut: Bool = false,
propertiesSanitizer: PostHogPropertiesSanitizer? = nil,
personProfiles: PostHogPersonProfiles = .identifiedOnly) -> PostHogSDK
{
let config = PostHogConfig(apiKey: "123", host: "http://localhost:9001")
config.flushAt = flushAt
config.maxBatchSize = maxBatchSize
config.preloadFeatureFlags = preloadFeatureFlags
config.sendFeatureFlagEvent = sendFeatureFlagEvent
config.disableReachabilityForTesting = true
Expand Down Expand Up @@ -847,6 +850,51 @@ class PostHogSDKTest: QuickSpec {
sut.reset()
sut.close()
}

it("captures $feature_flag_called when getFeatureFlag is called") {
let sut = self.getSut(sendFeatureFlagEvent: true)
ioannisj marked this conversation as resolved.
Show resolved Hide resolved
let group = DispatchGroup()
group.enter()
ioannisj marked this conversation as resolved.
Show resolved Hide resolved

_ = sut.getFeatureFlag("some_key")

let event = getBatchedEvents(server)
expect(event.first!.event).to(equal("$feature_flag_called"))
}

it("does not capture $feature_flag_called when getFeatureFlag is called twice") {
let sut = self.getSut(
sendFeatureFlagEvent: true,
flushAt: 2
)

_ = sut.getFeatureFlag("some_key")
_ = sut.getFeatureFlag("some_key")
sut.capture("force_batch_flush")

let event = getBatchedEvents(server)
expect(event.count).to(equal(2))
expect(event[0].event).to(equal("$feature_flag_called"))
expect(event[1].event).to(equal("force_batch_flush"))
}

it("captures $feature_flag_called when getFeatureFlag called twice after reloading flags") {
let sut = self.getSut(
sendFeatureFlagEvent: true,
flushAt: 2
)

_ = sut.getFeatureFlag("some_key")

sut.reloadFeatureFlags {
_ = sut.getFeatureFlag("some_key")
}

let event = getBatchedEvents(server)
expect(event.count).to(equal(2))
expect(event[0].event).to(equal("$feature_flag_called"))
expect(event[1].event).to(equal("$feature_flag_called"))
}
}
}

Expand Down
Loading