Skip to content
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
2 changes: 1 addition & 1 deletion .changeset/v4-major-release.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ PostHog iOS 4.0: see the [migration guide](https://github.com/PostHog/posthog-io
- **Breaking:** mark `reloadFeatureFlags(_:)`, `setBeforeSend(_:)`, `getAnonymousId`, `pushIdentityProvider` and `logSanitizer` callbacks `@Sendable`, fixing a crash in Swift 6 apps
- **Breaking:** rename `PostHogLogLevel` to `PostHogConsoleLogLevel` and `PostHogLogEntry` to `PostHogConsoleLogEntry`
- **Breaking:** return `nil` instead of the raw string for a malformed, empty or whitespace-only feature flag payload
- **Breaking:** treat a feature flag whose variant is an empty string as disabled in `isFeatureEnabled`, `getFeatureFlagResult(_:).enabled`, `PostHogFeatureFlagsLoaded` and `$active_feature_flags`
- **Breaking:** stop decoding string payloads in `PostHogBootstrapConfig.featureFlagPayloads` a second time, so a bootstrapped `"123"` stays a `String`
- **Breaking:** delete events still queued on disk by a 2.x SDK instead of sending them, and ignore a top-level `$set` and `message_id` in queued events
- **Breaking:** remove `version` and `build` from `Application Installed`, `Application Updated` and `Application Opened` events β€” use `$app_version` and `$app_build`
Expand Down Expand Up @@ -56,6 +57,5 @@ PostHog iOS 4.0: see the [migration guide](https://github.com/PostHog/posthog-io
- Use the current distinct ID for an empty or whitespace-only `distinctId` argument
- Give each capture its own UUID, even when `beforeSend` returns an event kept from an earlier call
- Print a warning, also without `debug`, when `setup` leaves the SDK disabled because `projectToken` is empty or a personal API key
- Fix `isFeatureEnabled` returning `true` for an empty variant string
- Fix session replay recording nothing for apps whose window root is a SwiftUI view
- Fix secure-entry text fields being unmasked inside a `postHogNoMask()` or `ph-no-mask` subtree
2 changes: 1 addition & 1 deletion MIGRATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ Secure-entry text fields now stay masked inside a `.postHogNoMask()` or `ph-no-m

- A malformed, empty or whitespace-only payload now reads as `nil` from `getFeatureFlagResult(_:)` and `getAllFeatureFlags()`. In 3.x you got the raw string. The flag's value and enabled state don't change.
- Bootstrapped string payloads in `PostHogBootstrapConfig.featureFlagPayloads` stay strings. In 3.x, a bootstrapped `"123"` or `"true"` was decoded a second time and read back as a number or `Bool`.
- `isFeatureEnabled(_:)` returns `false` for a flag whose variant is an empty string. In 3.x it returned `true`.
- A flag whose variant is an empty string reads as disabled. `isFeatureEnabled(_:)` returns `false`, with or without `defaultValue`, `enabled` is `false` in `getFeatureFlagResult(_:)` and `getAllFeatureFlags()`, `PostHogFeatureFlagsLoaded` leaves the flag out of `flags` and `variants`, and captured events leave it out of `$active_feature_flags`. In 3.x, `isFeatureEnabled(_:)` and `enabled` returned `true` and events listed the flag. `variant`, `getFeatureFlag(_:)` and the event's `$feature/<key>` property still return `""`.

If your code relied on getting the raw string back, make the payload valid JSON in PostHog.

Expand Down
2 changes: 1 addition & 1 deletion PostHog/Models/PostHogFeatureFlagResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class PostHogFeatureFlagResult: NSObject {
///
/// This is `true` if the flag value is:
/// - A boolean `true`
/// - A non-null string (variant)
/// - A non-empty string (variant)
@objc public let enabled: Bool

/// The variant name if this is a multivariate flag, otherwise `nil`.
Expand Down
2 changes: 1 addition & 1 deletion PostHog/Models/PostHogFeatureFlagsLoaded.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public final class PostHogFeatureFlagsLoaded: NSObject, @unchecked Sendable {
if bool {
enabled[key] = bool
}
} else if let string = value as? String {
} else if let string = value as? String, !string.isEmpty {
enabled[key] = string
}
}
Expand Down
2 changes: 1 addition & 1 deletion PostHog/PostHogRemoteConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1119,7 +1119,7 @@ class PostHogRemoteConfig {
let variant: String?

if let stringValue = flagValue as? String {
isEnabled = true
isEnabled = !stringValue.isEmpty
variant = stringValue
} else if let boolValue = flagValue as? Bool {
isEnabled = boolValue
Expand Down
9 changes: 2 additions & 7 deletions PostHog/PostHogSDK.swift
Original file line number Diff line number Diff line change
Expand Up @@ -610,13 +610,8 @@ let maxRetryDelay = 30.0
for (key, value) in flags {
properties["$feature/\(key)"] = value

var active = true
let boolValue = value as? Bool
if boolValue != nil {
active = boolValue!
} else {
active = true
}
// an empty variant reads as disabled
let active = (value as? Bool ?? true) && (value as? String) != ""

if active {
keys.append(key)
Expand Down
38 changes: 35 additions & 3 deletions PostHogTests/PostHogSDKTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -821,8 +821,8 @@ final class PostHogSDKTests {
sut.close()
}

@Test("isFeatureEnabled treats an empty variant as disabled and ignores the default", arguments: [true, false])
func isFeatureEnabledEmptyVariantIsDisabled(defaultValue: Bool) {
@Test("an empty variant reads as disabled, ignoring the isFeatureEnabled default", arguments: [true, false])
func emptyVariantIsDisabled(defaultValue: Bool) {
server.flagsResponseHandler = { _ in
HTTPStubsResponse(
jsonObject: ["flags": ["empty-variant": ["key": "empty-variant", "enabled": true, "variant": ""]]],
Expand All @@ -834,13 +834,45 @@ final class PostHogSDKTests {

let group = DispatchGroup()
group.enter()
sut.reloadFeatureFlags { _ in
var loaded: PostHogFeatureFlagsLoaded?
sut.reloadFeatureFlags { result in
loaded = result
group.leave()
}
group.wait()

#expect(sut.isFeatureEnabled("empty-variant") == false)
#expect(sut.isFeatureEnabled("empty-variant", defaultValue: defaultValue) == false)
let result = sut.getFeatureFlagResult("empty-variant")
#expect(result?.enabled == false)
#expect(result?.variant == "")
#expect(loaded?.flags.contains("empty-variant") == false)
#expect(loaded?.variants["empty-variant"] == nil)

sut.reset()
sut.close()
}

@Test("captured events leave a flag with an empty variant out of $active_feature_flags")
func emptyVariantIsNotActiveOnEvents() throws {
server.flagsResponseHandler = { _ in
HTTPStubsResponse(
jsonObject: ["flags": [
"on": ["key": "on", "enabled": true],
"empty-variant": ["key": "empty-variant", "enabled": true, "variant": ""],
]],
statusCode: 200,
headers: nil
)
}
let sut = getSut(preloadFeatureFlags: true)
waitForFeatureFlagsLoaded(server, sut)

sut.capture("event")

let event = try #require(getBatchedEvents(server).first)
#expect(event.properties["$feature/empty-variant"] as? String == "")
#expect(event.properties["$active_feature_flags"] as? [String] == ["on"])

sut.reset()
sut.close()
Expand Down
Loading