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-provider", from: "0.1.0"),
    .package(url: "https://github.com/open-feature/swift-sdk", from: "0.1.0"),
]

CocoaPods

pod 'MixpanelOpenFeatureProvider'
pod 'OpenFeature'

Usage

The provider wraps MixpanelFlags from the Mixpanel Swift SDK.

import Mixpanel
import MixpanelOpenFeatureProvider
import OpenFeature
 
// Initialize Mixpanel with feature flags enabled
Mixpanel.initialize(token: "YOUR_PROJECT_TOKEN", options: MixpanelOptions(
    featureFlagOptions: FeatureFlagOptions(enabled: true)
))
 
// Register the OpenFeature provider
let provider = MixpanelProvider(flags: Mixpanel.mainInstance().flags)
OpenFeatureAPI.shared.setProvider(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.

  • 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().

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

  • onClose() is a no-op. The Mixpanel SDK manages its own lifecycle.
  • The reason code for successful evaluations is STATIC.

Was this page useful?