DocsTracking MethodsSDKsGoOpenFeature Provider (Go)

OpenFeature Provider (Go)

Overview

This guide covers using Mixpanel’s Feature Flags through the OpenFeature standard with the Mixpanel Go OpenFeature provider. OpenFeature provides a vendor-agnostic API for feature flag evaluation, allowing you to switch between providers without changing your application code.

For the native Mixpanel SDK approach, see the Feature Flags (Go) guide.

Prerequisites

Installation

go get github.com/mixpanel/mixpanel-go/openfeature
go get github.com/open-feature/go-sdk

Quick Start

package main
 
import (
    "context"
    "fmt"
 
    mixpanelopenfeature "github.com/mixpanel/mixpanel-go/openfeature"
    "github.com/mixpanel/mixpanel-go/v2/flags"
    of "github.com/open-feature/go-sdk/openfeature"
)
 
func main() {
    // 1. Create the Mixpanel OpenFeature provider with local evaluation
    provider, err := mixpanelopenfeature.NewProviderWithLocalConfig("YOUR_PROJECT_TOKEN", flags.LocalFlagsConfig{})
    if err != nil {
        panic(err)
    }
 
    // 2. Register the provider with OpenFeature
    of.SetProvider(provider)
    client := of.NewClient("my-app")
 
    // 3. Evaluate flags
    showNewFeature, _ := client.BooleanValue(context.Background(), "new-feature-flag", false, of.EvaluationContext{})
 
    if showNewFeature {
        fmt.Println("New feature is enabled!")
    }
}

Initialization

Flag definitions are fetched from Mixpanel and evaluated locally. This is faster and works offline after the initial fetch.

⚠️

Targeting by Mixpanel cohorts and sticky variants are not supported in Local Evaluation mode.

provider, err := mixpanelopenfeature.NewProviderWithLocalConfig("YOUR_PROJECT_TOKEN", flags.LocalFlagsConfig{})

Remote Evaluation

Each flag evaluation makes a request to Mixpanel’s servers.

provider, err := mixpanelopenfeature.NewProviderWithRemoteConfig("YOUR_PROJECT_TOKEN", flags.RemoteFlagsConfig{})

Using an Existing Mixpanel Instance

mp := mixpanel.NewApiClient("YOUR_TOKEN", mixpanel.WithLocalFlags(flags.LocalFlagsConfig{}))
mp.LocalFlags.StartPollingForDefinitions(context.Background())
 
provider, err := mixpanelopenfeature.NewProvider(mp.LocalFlags)

Usage

Flag Types and Evaluation Methods

Mixpanel Flag TypeVariant ValuesOpenFeature Method
Feature Gatetrue / falseBooleanValue()
Experimentboolean, string, number, or JSON objectBooleanValue(), StringValue(), FloatValue(), IntValue(), or ObjectValue()
Dynamic ConfigJSON objectObjectValue()
ctx := context.Background()
evalCtx := of.EvaluationContext{}
 
// Feature Gate
enabled, _ := client.BooleanValue(ctx, "new-checkout", false, evalCtx)
 
// Experiment with string variants
buttonColor, _ := client.StringValue(ctx, "button-color-test", "blue", evalCtx)
 
// Numeric flags
threshold, _ := client.FloatValue(ctx, "score-threshold", 0.5, evalCtx)
maxItems, _ := client.IntValue(ctx, "max-items", 10, evalCtx)
 
// Dynamic Config
config, _ := client.ObjectValue(ctx, "homepage-layout", map[string]any{
    "layout": "grid",
}, evalCtx)

Evaluation Context

evalCtx := of.NewEvaluationContext("user-123", map[string]any{
    "email": "user@example.com",
    "plan":  "premium",
})
 
enabled, _ := client.BooleanValue(ctx, "premium-feature", false, evalCtx)

Unlike some providers, targetingKey is not used as a special bucketing key. It is passed as another context property. Mixpanel’s server-side configuration determines which properties are used for targeting and bucketing.

Full Resolution Details

details, _ := client.BooleanValueDetails(ctx, "my-feature", false, of.EvaluationContext{})
 
fmt.Println(details.Value)
fmt.Println(details.Variant)
fmt.Println(details.Reason)
fmt.Println(details.ErrorCode)

Accessing the Underlying Mixpanel Client

provider, _ := mixpanelopenfeature.NewProviderWithLocalConfig("YOUR_TOKEN", flags.LocalFlagsConfig{})
 
provider.Mixpanel.Track(ctx, []*mixpanel.Event{
    {Name: "button_clicked", Properties: map[string]any{"color": "blue"}},
})

Shutdown

provider.Shutdown()

Error Handling

Error CodeWhen
PROVIDER_NOT_READYFlags evaluated before the local provider has finished fetching definitions
FLAG_NOT_FOUNDThe requested flag does not exist in Mixpanel
TYPE_MISMATCHThe flag value type does not match the requested type

Troubleshooting

Flags Always Return Default Values

  1. Provider not ready: Ensure polling has started and definitions have been received.
  2. Flag not configured: Verify the flag exists and is enabled in your Mixpanel project.
  3. Network issues: For remote evaluation, check that your application can reach Mixpanel’s API.

Type Mismatch Errors

The flag’s value type must match the evaluation method. IntValue() accepts whole-number float64 values. FloatValue() accepts any numeric type.

Was this page useful?