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.

This documentation is for version 7 of the Bugsnag JavaScript notifier. If you are using older versions, we recommend upgrading to the latest release using our Upgrade guide. Documentation for the previous release can be found on our legacy pages.

Declaring active feature flags and experiments

You should declare feature flag and experiment usage to Bugsnag 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.

You will usually want feature flag data to be specific to the request being served, not globally, so that it doesn’t affect requests from other users, who may have different treatments. It should therefore be added to the request-scoped Bugsnag client, ctx.bugsnag, that is added to the request context by Bugsnag middleware.

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.

ctx.bugsnag.addFeatureFlag('Checkout button color', 'Blue')
ctx.bugsnag.addFeatureFlag('New checkout flow')

Where ctx is the request context.

addFeatureFlags

Declare multiple feature flags or experiments.

ctx.bugsnag.addFeatureFlags([
  { name: 'Checkout button color', variant: 'Blue' },
  { name: 'Special offer', variant: 'Free Coffee' },
  { name: 'New checkout flow' },
])

Where ctx is the request context.

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.

ctx.clearFeatureFlag('Checkout button color')

Where ctx is the request context.

clearFeatureFlags

Remove all feature flags and experiments.

ctx.clearFeatureFlags()

Where ctx is the request context.

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)

// ctx is the HTTP request context 
if (featureEnabled) {
  ctx.bugsnag.addFeatureFlag('bool-flag-key')
} else {
  ctx.bugsnag.clearFeatureFlag('bool-flag-key')
}

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

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

For more information, please see the LaunchDarkly Node.js SDK documentation.

Split

To use Bugsnag with Split, feature flags should be added to the request-scoped Bugsnag client so that it doesn’t affect requests from other users, who may have different treatments:

const factory = SplitFactory(/* ... */)
const client = factory.client()

app.use(async (ctx, next) => {
  const treatment = client.getTreatment('key', 'SPLIT_NAME')

  ctx.bugsnag.addFeatureFlag('SPLIT_NAME', treatment)

  // ...
})

For more information, please see the Split Node.js SDK documentation.