OpenFeature Provider (Swift)
Overview
The Mixpanel OpenFeature provider allows you to use Mixpanel Feature Flags through the standardized OpenFeature API. This provider wraps the Mixpanel Swift SDK’s MixpanelFlags protocol, letting you use the OpenFeature Swift SDK with Mixpanel as the backend.
For the core Feature Flags SDK guide, see Feature Flags (Swift).
Prerequisites
- You are on an Enterprise subscription plan
- You have the Mixpanel Swift SDK installed (minimum version
6.2.0) - You have your Project Token from your Mixpanel Project Settings
Installation
Swift Package Manager
Add the OpenFeature provider package to your Package.swift:
dependencies: [
.package(url: "https://github.com/mixpanel/mixpanel-swift-openfeature", from: "0.1.0"),
.package(url: "https://github.com/open-feature/swift-sdk", from: "0.5.0"),
]Usage
The provider wraps MixpanelFlags from the Mixpanel Swift SDK.
Using the Convenience Initializer
import Mixpanel
import MixpanelOpenFeature
import OpenFeature
// Create provider with a new Mixpanel instance
let provider = MixpanelOpenFeatureProvider(options: MixpanelOptions(
token: "YOUR_PROJECT_TOKEN",
featureFlagOptions: FeatureFlagOptions(enabled: true)
))
await OpenFeatureAPI.shared.setProviderAndWait(provider: provider)
let client = OpenFeatureAPI.shared.getClient()
let showNewUI = client.getBooleanValue(key: "new-ui", defaultValue: false)
// Access the underlying Mixpanel instance for identify/track
provider.mixpanel?.identify(distinctId: "user-123")Using an Existing Mixpanel Instance
import Mixpanel
import MixpanelOpenFeature
import OpenFeature
Mixpanel.initialize(token: "YOUR_PROJECT_TOKEN", options: MixpanelOptions(
featureFlagOptions: FeatureFlagOptions(enabled: true)
))
let provider = MixpanelOpenFeatureProvider(flags: Mixpanel.mainInstance().flags)
await OpenFeatureAPI.shared.setProviderAndWait(provider: provider)
let client = OpenFeatureAPI.shared.getClient()
let showNewUI = client.getBooleanValue(key: "new-ui", defaultValue: false)Supported Flag Types
| OpenFeature Method | Swift Type |
|---|---|
getBooleanValue / getBooleanDetails | Bool |
getStringValue / getStringDetails | String |
getIntegerValue / getIntegerDetails | Int64 |
getDoubleValue / getDoubleDetails | Double |
getObjectValue / getObjectDetails | Value |
Context and Identity
Context is set globally when registering the provider, not per-evaluation. Per-evaluation context passed to individual flag evaluations is ignored.
The provider forwards the global OpenFeature context to the Mixpanel SDK via setContext() during initialization and whenever the global context changes.
targetingKeyhas no special meaning in this provider. It is treated as a regular context property.- Identity should be managed through the Mixpanel SDK via
Mixpanel.mainInstance().identify()orprovider.mixpanel?.identify().
Error Handling
The provider returns the default value on all errors, with an error code indicating the cause:
| Error Code | Condition |
|---|---|
PROVIDER_NOT_READY | The provider has not been initialized |
FLAG_NOT_FOUND | The requested flag does not exist |
TYPE_MISMATCH | The flag value does not match the requested type |
Use getDetails() methods instead of getValue() to inspect error codes:
let details = client.getBooleanDetails(key: "my-flag", defaultValue: false)
if let errorCode = details.errorCode {
print("Flag error: \(errorCode) - \(details.errorMessage ?? "")")
}Lifecycle
shutdown()is a no-op. The Mixpanel SDK manages its own lifecycle.- The reason code for successful evaluations is
STATIC.
Was this page useful?