DocsTracking MethodsSDKsiOS (Swift)OpenFeature Provider (Swift)

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

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 MethodSwift Type
getBooleanValue / getBooleanDetailsBool
getStringValue / getStringDetailsString
getIntegerValue / getIntegerDetailsInt64
getDoubleValue / getDoubleDetailsDouble
getObjectValue / getObjectDetailsValue

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.

  • targetingKey has 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() or provider.mixpanel?.identify().

Error Handling

The provider returns the default value on all errors, with an error code indicating the cause:

Error CodeCondition
PROVIDER_NOT_READYThe provider has not been initialized
FLAG_NOT_FOUNDThe requested flag does not exist
TYPE_MISMATCHThe 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?