Feature Flags

A feature flag is an experimentation tool for controlling the availability of content or functionality in your app or website. A flag’s Configurations determine the audience, schedule, and property values to apply when the flag is enabled. Flag properties enable making immediate code updates, bypassing the need for traditional code changes and release processes.

View as Markdown

Accessing flags

The Airship SDK will refresh feature flags when the app is brought to the foreground. If a feature flag is accessed before the foreground refresh completes, or after the foreground refresh has failed, feature flags will be refreshed during flag access. Feature flags will only be updated once per session and will persist for the duration of each session.

Once defined in the dashboard, a feature flag can be accessed by its name in the SDK after takeOff.

The SDK provides asynchronous access to feature flags using Kotlin suspend functions, which is intended to be called from a coroutine. For more information, see Coroutines Overview guide .

Accessing a feature flag

// Get the FeatureFlag result
val result: Result<FeatureFlag> = FeatureFlagManager.shared().flag("YOUR_FLAG_NAME")

// Check if the app is eligible or not
if (result.getOrNull()?.isEligible == true) {
    // Do something with the flag
} else {
    // Disable feature or use default behavior
}
// Get the FeatureFlag 
FeatureFlag featureFlag = FeatureFlagManager.shared().flagAsPendingResult("YOUR_FLAG_NAME").getResult();

// Check if the app is eligible or not
if (featureFlag != null && featureFlag.isEligible()) {
    // Do something with the flag
} else {
    // Disable feature or use default behavior
}

Tracking interaction

To generate the feature flag interaction event, you must manually call trackInteraction with the feature flag. Analytics must be enabled. See: Data Collection: Privacy Manager.

Tracking an interaction

FeatureFlagManager.shared().trackInteraction(featureFlag)
FeatureFlagManager.shared().trackInteraction(featureFlag);

Error handling

If a feature flag allows evaluation with stale data, the SDK evaluates the flag if a definition for the flag is found. Otherwise, feature flag evaluation depends on updated local state. If the SDK cannot evaluate a flag because data cannot be fetched, the SDK returns or raises an error. The app can either treat the error as the flag being ineligible or retry at a later time.

Handling errors

FeatureFlagManager.shared().flag("YOUR_FLAG_NAME").fold(
    onSuccess = { flag -> 
        // do something with the flag
    },
    onFailure = { error ->
        // do something with the error
    }
)
FeatureFlag featureFlag = FeatureFlagManager.shared().flagAsPendingResult("YOUR_FLAG_NAME").getResult();
if (featureFlag == null) {
    // error
} else if (featureFlag.isEligible()) {
    // Do something with the flag
}

Checking flag data freshness

status reports whether the local feature flag rules are current (UP_TO_DATE), still usable but due for a refresh (STALE), or definitely out of date (OUT_OF_DATE). To make sure a flag evaluates against the latest rules, such as right after takeOff or before evaluating a flag that just changed in the dashboard, wait for a refresh instead of evaluating immediately:

Waiting for a refresh

// Check the current status
val status = FeatureFlagManager.shared().status

// Wait for a refresh, with an optional timeout
FeatureFlagManager.shared().waitRefresh(maxTimeMillis = 5000)
val result: Result<FeatureFlag> = FeatureFlagManager.shared().flag("YOUR_FLAG_NAME")

// Or observe status changes
scope.launch {
    FeatureFlagManager.shared().statusUpdates.collect { status ->
        Log.d("Airship", "Feature flag status: $status")
    }
}
// Check the current status
FeatureFlagRemoteDataStatus status = FeatureFlagManager.shared().getStatus();

// Wait for a refresh, with an optional timeout
FeatureFlagManager.shared().waitRefreshPendingResult(5000L).addResultCallback(result -> {
    FeatureFlag featureFlag = FeatureFlagManager.shared().flagAsPendingResult("YOUR_FLAG_NAME").getResult();
    if (featureFlag != null && featureFlag.isEligible()) {
        // Do something with the flag
    }
});