Statsig for Browser JavaScript

Learn how to use Sentry with Statsig.

The Statsig integration tracks feature flag evaluations produced by the Statsig JavaScript Client SDK. These evaluations are held in memory and are sent to Sentry on error and transaction events. At the moment, we only support boolean flag evaluations from Statsig's checkGate method. Learn more about Statsig feature gates.

This integration is available in Sentry SDK versions 9.0.0 and higher, or versions 8.55.0 and higher for v8.

Import name: Sentry.statsigIntegration

Before using this integration, you need to install and instrument Statsig in your app. Learn more by reading Statsig's SDK reference and quickstart guide.

Copied
import * as Sentry from "___SDK_PACKAGE___";
import { StatsigClient } from "@statsig/js-client";

const statsigClient = new StatsigClient(
  YOUR_SDK_KEY,
  { userID: "my-user-id" },
  {},
); // see Statsig SDK reference.

Sentry.init({
  dsn: "___PUBLIC_DSN___",
  integrations: [
    Sentry.statsigIntegration({ featureFlagClient: statsigClient }),
  ],
});

await statsigClient.initializeAsync(); // or statsigClient.initializeSync();

const result = statsigClient.checkGate("my-feature-gate");
Sentry.captureException(new Error("something went wrong"));

Visit the Sentry website and confirm that your error event has recorded the feature flag "my-feature-gate" and its value "false".

The integration doesn't track Statsig experiments, because their assigned groups aren't boolean values. To see which experiment group a user was in when an error happened, listen to Statsig's experiment_evaluation event and record the group as a tag and an attribute:

Copied
statsigClient.on("experiment_evaluation", ({ experiment }) => {
  // `groupName` is null when the user isn't part of the experiment
  if (!experiment.groupName) {
    return;
  }

  const key = `statsig.experiment.${experiment.name}`;
  // Tags are added to error events
  Sentry.setTag(key, experiment.groupName);
  // Attributes are added to spans, logs, and metrics
  Sentry.setAttribute(key, experiment.groupName);
});

Register the listener before calling initializeAsync() so evaluations made during startup are captured. You can then search for errors from a group, for example statsig.experiment.my_experiment:Control, and filter spans, logs, and metrics by the same attribute.

Experiment names have to be valid tag keys. Sentry.setAttribute requires SDK version 10.61.0 or higher, see Attributes. These values don't show up in the feature flag section of an issue.

Was this helpful?
Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").