Interface MixpanelAPI.Flags
- Enclosing class:
- MixpanelAPI
MixpanelAPI.getFlags()
(assuming such a method exists).
The Flags interface allows you to manage and retrieve feature flags defined in your Mixpanel project. Feature flags can be used to remotely configure your application's behavior, roll out new features gradually, or run A/B tests.
It's recommended to load flags early in your application's lifecycle, for example,
in your main Application class or main Activity's onCreate
method.
A typical use case for the Flags interface might look like this:
public class MainActivity extends Activity {
MixpanelAPI mMixpanel;
Flags mFlags;
public void onCreate(Bundle saved) {
super.onCreate(saved);
MixanelOptions mpOptions = new MixpanelOptions.Builder().featureFlagsEnabled(true).build();
mMixpanel = MixpanelAPI.getInstance(this, "YOUR MIXPANEL TOKEN", true, mpOptions);
mFlags = mMixpanel.getFlags();
// Asynchronously load flags
mFlags.loadFlags();
// Example of checking a flag asynchronously
mFlags.isFlagEnabled("new-checkout-flow", false, isEnabled -> {
if (isEnabled) {
// Show new checkout flow
} else {
// Show old checkout flow
}
});
// Example of getting a flag value synchronously after ensuring flags are ready
if (mFlags.areFlagsReady()) {
String buttonLabel = (String) mFlags.getVariantValueSync("home-button-label", "Default Label");
// Use buttonLabel
}
}
}
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionboolean
Returns true if flags have been successfully loaded from the server and are currently available for synchronous access.void
getVariant
(String featureName, MixpanelFlagVariant fallback, FlagCompletionCallback<MixpanelFlagVariant> completion) Asynchronously gets the complete feature flag data (key and value).getVariantSync
(String featureName, MixpanelFlagVariant fallback) Gets the complete feature flag data (key and value) synchronously.void
getVariantValue
(String featureName, Object fallbackValue, FlagCompletionCallback<Object> completion) Asynchronously gets the value of a specific feature flag.getVariantValueSync
(String featureName, Object fallbackValue) Gets the value of a specific feature flag synchronously.void
isEnabled
(String featureName, boolean fallbackValue, FlagCompletionCallback<Boolean> completion) Asynchronously checks if a specific feature flag is enabled.boolean
isEnabledSync
(String featureName, boolean fallbackValue) Checks if a specific feature flag is enabled synchronously.void
Asynchronously loads flags from the Mixpanel server if they haven't been loaded yet or if the cached flags have expired.
-
Method Details
-
loadFlags
void loadFlags()Asynchronously loads flags from the Mixpanel server if they haven't been loaded yet or if the cached flags have expired. This method will initiate a network request if necessary. Subsequent calls to get flag values (especially asynchronous ones) may trigger this load if flags are not yet available. -
areFlagsReady
boolean areFlagsReady()Returns true if flags have been successfully loaded from the server and are currently available for synchronous access. This is useful to check before calling synchronous flag retrieval methods likegetVariantSync(String, MixpanelFlagVariant)
to avoid them returning the fallback value immediately.- Returns:
- true if flags are loaded and ready, false otherwise.
-
getVariantSync
@NonNull MixpanelFlagVariant getVariantSync(@NonNull String featureName, @NonNull MixpanelFlagVariant fallback) Gets the complete feature flag data (key and value) synchronously.IMPORTANT: This method can block the calling thread if it needs to wait for flags to be loaded (though the provided implementation detail suggests it returns fallback immediately if not ready). It is strongly recommended NOT to call this from the main UI thread if
areFlagsReady()
is false, as it could lead to ANR (Application Not Responding) issues if blocking were to occur.If flags are not ready (i.e.,
areFlagsReady()
returns false), this method will return the providedfallback
value immediately without attempting to fetch flags or block.- Parameters:
featureName
- The unique name (key) of the feature flag to retrieve.fallback
- TheMixpanelFlagVariant
instance to return if the specified flag is not found in the loaded set, or if flags are not ready. This must not be null.- Returns:
- The
MixpanelFlagVariant
for the found feature flag, or thefallback
if the flag is not found or flags are not ready.
-
getVariantValueSync
Gets the value of a specific feature flag synchronously.IMPORTANT: Similar to
getVariantSync(String, MixpanelFlagVariant)
, this method may involve blocking behavior if flags are being loaded. It's advised to checkareFlagsReady()
first and avoid calling this on the main UI thread if flags might not be ready.If flags are not ready, or if the specified
featureName
is not found, this method returns thefallbackValue
immediately.- Parameters:
featureName
- The unique name (key) of the feature flag.fallbackValue
- The default value to return if the flag is not found, its value is null, or if flags are not ready. Can be null.- Returns:
- The value of the feature flag (which could be a String, Boolean, Number, etc.),
or the
fallbackValue
.
-
isEnabledSync
Checks if a specific feature flag is enabled synchronously. A flag is considered enabled if its value evaluates totrue
.- If the flag's value is a Boolean, it's returned directly.
- If the flag's value is a String, it's considered
true
if it equals (case-insensitive) "true" or "1". - If the flag's value is a Number, it's considered
true
if it's non-zero. - For other types, or if the flag is not found, it relies on the
fallbackValue
.
IMPORTANT: See warnings on
getVariantSync(String, MixpanelFlagVariant)
regarding potential blocking and the recommendation to checkareFlagsReady()
first, especially when calling from the main UI thread.Returns
fallbackValue
immediately if flags are not ready or the flag is not found.- Parameters:
featureName
- The unique name (key) of the feature flag.fallbackValue
- The default boolean value to return if the flag is not found, cannot be evaluated as a boolean, or if flags are not ready.- Returns:
true
if the flag is present and evaluates to true, otherwisefalse
(or thefallbackValue
).
-
getVariant
void getVariant(@NonNull String featureName, @NonNull MixpanelFlagVariant fallback, @NonNull FlagCompletionCallback<MixpanelFlagVariant> completion) Asynchronously gets the complete feature flag data (key and value).If flags are not currently loaded, this method will trigger a fetch from the Mixpanel server. The provided
completion
callback will be invoked on the main UI thread once the operation is complete.If the fetch fails or the specific flag is not found after a successful fetch, the
fallback
data will be provided to the completion callback.- Parameters:
featureName
- The unique name (key) of the feature flag to retrieve.fallback
- TheMixpanelFlagVariant
instance to return via the callback if the flag is not found or if the fetch operation fails. This must not be null.completion
- TheFlagCompletionCallback
that will be invoked on the main thread with the result (either the foundMixpanelFlagVariant
or thefallback
). This must not be null.
-
getVariantValue
void getVariantValue(@NonNull String featureName, @Nullable Object fallbackValue, @NonNull FlagCompletionCallback<Object> completion) Asynchronously gets the value of a specific feature flag.If flags are not currently loaded, this method will trigger a fetch. The
completion
callback is invoked on the main UI thread with the flag's value or thefallbackValue
.- Parameters:
featureName
- The unique name (key) of the feature flag.fallbackValue
- The default value to return via the callback if the flag is not found, its value is null, or if the fetch operation fails. Can be null.completion
- TheFlagCompletionCallback
that will be invoked on the main thread with the result (the flag's value or thefallbackValue
). This must not be null.
-
isEnabled
void isEnabled(@NonNull String featureName, boolean fallbackValue, @NonNull FlagCompletionCallback<Boolean> completion) Asynchronously checks if a specific feature flag is enabled. The evaluation of "enabled" follows the same rules asisEnabledSync(String, boolean)
.If flags are not currently loaded, this method will trigger a fetch. The
completion
callback is invoked on the main UI thread with the boolean result.- Parameters:
featureName
- The unique name (key) of the feature flag.fallbackValue
- The default boolean value to return via the callback if the flag is not found, cannot be evaluated as a boolean, or if the fetch operation fails.completion
- TheFlagCompletionCallback
that will be invoked on the main thread with the boolean result. This must not be null.
-