DocsTracking MethodsSDKsJavaOpenFeature Provider (Java)

OpenFeature Provider (Java)

Overview

This guide covers using Mixpanel’s Feature Flags through the OpenFeature standard with the Mixpanel Java 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 (Java) guide.

Prerequisites

Installation

Maven

<dependency>
    <groupId>com.mixpanel</groupId>
    <artifactId>mixpanel-java-openfeature</artifactId>
    <version>0.1.0</version>
</dependency>
<dependency>
    <groupId>dev.openfeature</groupId>
    <artifactId>sdk</artifactId>
    <version>1.20.1</version>
</dependency>

Gradle

implementation 'com.mixpanel:mixpanel-java-openfeature:0.1.0'
implementation 'dev.openfeature:sdk:1.20.1'

Quick Start

import com.mixpanel.openfeature.MixpanelProvider;
import com.mixpanel.mixpanelapi.featureflags.config.LocalFlagsConfig;
import dev.openfeature.sdk.OpenFeatureAPI;
import dev.openfeature.sdk.Client;
 
// 1. Create and register the provider with local evaluation
MixpanelProvider provider = new MixpanelProvider(
    "YOUR_PROJECT_TOKEN",
    new LocalFlagsConfig("YOUR_PROJECT_TOKEN")
);
OpenFeatureAPI api = OpenFeatureAPI.getInstance();
api.setProvider(provider);
 
// 2. Get a client and evaluate flags
Client client = api.getClient();
boolean showNewFeature = client.getBooleanValue("new-feature-flag", false);
 
if (showNewFeature) {
    System.out.println("New feature is enabled!");
}

Initialization

⚠️

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

MixpanelProvider provider = new MixpanelProvider(
    "YOUR_PROJECT_TOKEN",
    new LocalFlagsConfig("YOUR_PROJECT_TOKEN")
);

Remote Evaluation

MixpanelProvider provider = new MixpanelProvider(
    "YOUR_PROJECT_TOKEN",
    new RemoteFlagsConfig("YOUR_PROJECT_TOKEN")
);

Using an Existing MixpanelAPI Instance

MixpanelAPI mixpanel = new MixpanelAPI(new LocalFlagsConfig("YOUR_PROJECT_TOKEN"));
LocalFlagsProvider localFlags = mixpanel.getLocalFlags();
localFlags.startPollingForDefinitions();
 
MixpanelProvider provider = new MixpanelProvider(localFlags);

Usage

Flag Types and Evaluation Methods

Mixpanel Flag TypeVariant ValuesOpenFeature Method
Feature Gatetrue / falsegetBooleanValue()
Experimentboolean, string, number, or JSON objectgetBooleanValue(), getStringValue(), getIntegerValue(), getDoubleValue(), or getObjectValue()
Dynamic ConfigJSON objectgetObjectValue()
Client client = api.getClient();
 
// Feature Gate
boolean isFeatureOn = client.getBooleanValue("new-checkout", false);
 
// Experiment with string variants
String buttonColor = client.getStringValue("button-color-test", "blue");
 
// Experiment with numeric variants
int maxItems = client.getIntegerValue("max-items", 10);
double threshold = client.getDoubleValue("score-threshold", 0.5);
 
// Dynamic Config
Value featureConfig = client.getObjectValue("homepage-layout", new Value("default"));

Evaluation Context

MutableContext context = new MutableContext();
context.setTargetingKey("user-123");
context.add("email", "user@example.com");
context.add("plan", "premium");
context.add("beta_tester", true);
 
boolean value = client.getBooleanValue("premium-feature", false, context);

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

FlagEvaluationDetails<Boolean> details = client.getBooleanDetails("my-feature", false);
 
System.out.println(details.getValue());
System.out.println(details.getVariant());
System.out.println(details.getReason());
System.out.println(details.getErrorCode());

Accessing the Underlying MixpanelAPI

MixpanelAPI mixpanel = provider.getMixpanel();

Shutdown

provider.shutdown();

Error Handling

Error CodeWhen
PROVIDER_NOT_READYFlags evaluated before the local provider has finished loading 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: Flag definitions are polled asynchronously. Allow time for the initial fetch.
  2. Invalid project token: Verify the token matches your Mixpanel project.
  3. Flag not configured: Verify the flag exists and is enabled.

Type Mismatch Errors

  1. Verify the flag’s value type matches your evaluation method.
  2. Use getObjectValue() for JSON objects.
  3. Integer evaluation accepts Long and whole-number Double values within Integer bounds. Double evaluation accepts any numeric type.

Was this page useful?