Feature flags & experiments

Monitor errors as you roll out features or run experiments and A/B tests by declaring your feature flag and experiment usage in the BugSnag client. You can use the Features dashboard to identify whether these features have introduced errors into your app.

Declaring active feature flags and experiments

You should declare feature flag and experiment usage to BugSnag – either in JavaScript or native code – at the time a feature or experiment is activated in your app. This ensures that subsequent errors will be associated with the feature flag or experiment usage accurately.

These operations are also available in configuration options. If feature or experiment usage is known at the time you initialize BugSnag, you may wish to declare it in configuration, then update it later in the same app session if required.

Features data is synchronized between the JavaScript and native layers and so reported in all events. However any feature data added in JavaScript will only be loaded when the JavaScript layer loads and so may not be present in native launch crashes.

You can declare the usage using the following methods on the BugSnag client:

addFeatureFlag

Declare a single feature flag or experiment with variant as an optional second parameter.

Bugsnag.addFeatureFlag('Checkout button color', 'Blue')
Bugsnag.addFeatureFlag('New checkout flow')
Bugsnag.addFeatureFlag("Checkout button color", "Blue");
Bugsnag.addFeatureFlag("New checkout flow");
[Bugsnag addFeatureFlagWithName:@"Checkout button color" variant:@"Blue"];
[Bugsnag addFeatureFlagWithName:@"New checkout flow"];

addFeatureFlags

Declare multiple feature flags or experiments.

Bugsnag.addFeatureFlags([
  { name: 'Checkout button color', variant: 'Blue' },
  { name: 'Special offer', variant: 'Free Coffee' },
  { name: 'New checkout flow' },
])
Bugsnag.addFeatureFlags(Arrays.asList(
  new FeatureFlag("Checkout button color", "Blue"),
  new FeatureFlag("Special offer", "Free Coffee"),
  new FeatureFlag("New checkout flow")
));
[Bugsnag addFeatureFlags:@[
    [BugsnagFeatureFlag flagWithName:@"Checkout button color" variant:@"Blue"],
    [BugsnagFeatureFlag flagWithName:@"Special offer" variant:@"Free Coffee"],
    [BugsnagFeatureFlag flagWithName:@"New checkout flow"]]];

If addFeatureFlags is called again, the new data will be merged with any existing feature flags with the newer variant values taking precedence.

clearFeatureFlag

Remove a single feature flag or experiment.

Bugsnag.clearFeatureFlag('Checkout button color')
Bugsnag.clearFeatureFlag("Checkout button color");
[Bugsnag clearFeatureFlagWithName:@"Checkout button color"];

clearFeatureFlags

Remove all feature flags and experiments.

Bugsnag.clearFeatureFlags()
Bugsnag.clearFeatureFlags();
[Bugsnag clearFeatureFlags];

Feature and experiment management tools

LaunchDarkly

To use BugSnag with LaunchDarkly, you need to declare the flag to BugSnag whenever you read it from LaunchDarkly:

const launchDarklyClient = // get LaunchDarkly client

// Boolean flag
const featureEnabled = launchDarklyClient.variation('bool-flag-key', false)

if (featureEnabled) {
  Bugsnag.addFeatureFlag('bool-flag-key')
} else {
  Bugsnag.clearFeatureFlag('bool-flag-key')
}

// String flag
const stringFlag = launchDarklyClient.variation('string-flag-key', null)
if (stringFlag !== null) {
  Bugsnag.addFeatureFlag('string-flag-key', stringFlag)
} else {
  Bugsnag.clearFeatureFlag('string-flag-key')
}

If your app reacts dynamically to flag changes using LaunchDarkly’s “on change” functionality, you should ensure that you keep BugSnag up-to-date with those changes too.

For more information, please see the LaunchDarkly client-side JavaScript SDK documentation.

Split

To use BugSnag with Split, we recommend adding an impression listener to keep BugSnag updated with the active experiments:

function bugsnagListener(impressionData) {
  Bugsnag.addFeatureFlag(
    impressionData.impression.feature,
    impressionData.impression.treatment
  )
}

const sdkConfig = {
  /* ... */
  impressionListener: {
    logImpression: bugsnagListener
  }
})

const App = () => (
  <SplitFactory config={sdkConfig}>
    <MyApp />
  </SplitFactory>
)
class MyImpressionListener implements ImpressionListener {
    @Override public void log(Impression impression) {
        String split = impression.split();
        if (split != null) {
            Bugsnag.addFeatureFlag(split, impression.treatment());
        }
    }
    @Override public void close() {
    }
}
SplitClientConfig config = SplitClientConfig.builder()
        .impressionListener(new MyImpressionListener())
        .build();
SplitClientConfig *splitConfig = [[SplitClientConfig alloc] init];
splitConfig.impressionListener = ^(Impression *impression) {
    if (impression.feature) {
        [Bugsnag addFeatureFlagWithName:impression.feature
                                variant:impression.treatment];
    }
};

For more information, please see the Split documentation: