On this page

Segment

Connect Segment with Statsig to forward customer data platform events to Statsig for experiment analysis, metrics, and audience targeting.

Enabling the Segment integration for Statsig allows Statsig to pull in your Segment events. This allows you to run your experiment analysis on Statsig with all of your existing events from Segment without requiring any additional logging.

When Statsig receives events from Segment, the events are visible and aggregated in the Metrics tab in the Statsig console. These events are automatically included in your Pulse results for A/B tests with Statsig's feature gates and all your Experiment results.

Supported Segment event types

Identify calls are only supported for syncing Segment Engage Audiences with Statsig Segments

Benefits of using the Segment integration

The Segment integration has several benefits over other methods of event ingestion:

  • Customers ingesting data with Segment can quickly populate Statsig with metrics and can typically get started within a day.
    • Customers only need to use Statsig's assignment SDKs (gate/experiment allocation), which simplifies code and engineering workflows.
    • Additional logging can be done through the event logging SDKs, but requires additional code orchestration and a collection window.
  • With event filtering you can control which events are ingested and make billing more predictable.
  • If you have Segment Replay, you can forward up to 7 days of historical events to Statsig for analysis.

Configuring inbound events into Statsig

Ingestion with this integration is available only for Statsig Cloud. For Warehouse Native, create a metrics source that references this data in your warehouse.

To send events collected from Segment into Statsig, you must configure the Statsig Destination within your Segment account:

Using OAuth

The easiest way to connect Statsig to Segment is through OAuth.

  1. To get started, within the Statsig Console, go to project settings → integrations → Segment → Enable

  2. Click “Configure Segment OAuth”

    Statsig integration settings showing Segment OAuth button

  3. Select the workspace and source that will send data to Statsig. Click “Allow”:

    Segment OAuth consent screen selecting workspace and source

  4. After the integration is enabled, you can configure event filtering and map additional identifiers. This is useful for mapping device-level identifiers and anonymousIds generated from Segment. Statsig recommends creating a custom ID called segmentAnonymousId and mapping the anonymousId from Segment to it.

    Statsig Segment integration panel with identifier mapping options

Manual configuration

If you are unable to connect to Segment through OAuth, you can manually connect Statsig to Segment by configuring the following:

  1. Within the Segment App, navigate to your Destinations, and select "Add Destination"
  2. Search for “Statsig” and select the destination

    Segment destination catalog listing Statsig

  3. Select "Statsig" from the list of available integrations, and then select sources that will send data to Statsig.

    Segment destination setup selecting sources

  4. You must provide a Statsig Server SDK key. You can copy an existing server key or create a new one from the Statsig console settings.
    • Create or copy a server SDK key

      Statsig API keys page showing server key

      Segment destination configuration entering Statsig server key

    • Put your Server Secret Key in the “API Key” field in the Statsig Destination

      Segment destination API key input

  5. To set up mappings, you must enable the Segment Integration in Statsig. Go to the Statsig Console, then Project Settings, then the Integrations tab, then Segment, and click Enable:

    Statsig Segment integration enable toggle

    • After the integration is enabled, you can configure event filtering and map additional identifiers. This is useful for mapping device-level identifiers and anonymousIds generated from Segment.

      Statsig identifier mapping form

  6. Verify that Statsig is receiving events using the Segment Event Tester.
    • In the event tester, send a test event to verify any filtering and mapping you have set up:

      Segment integration panel showing outgoing toggle

    • In the your Statsig Console Events Explorer, click on the test event in the log stream and verify the event:

      Segment destination configuration for outgoing data

    • In this example, the anonymousId is mapped to Statsig’s segmentAnonymousId based on the above mapping. Statsig infers the User ID from the Segment userID field.

      Statsig integration for Segment Engage audience mapping

  7. As your Segment events flow into Statsig, you see a live Log Stream in the Metrics tab in the Statsig console. You can click one of these events to see the details logged as part of the event.

    Log details panel showing Segment event payload fields and metadata

Working with users

Statsig joins incoming user identifiers to whichever unit of randomization you choose. This allows you to be flexible with your experimentation and enables testing on known (userID) and unknown (anonymousID) traffic as well as any custom identifiers your team may have (deviceID, companyID, vehicleID, etc).

User IDs and custom IDs

Statsig automatically detects the event and userId fields logged through your Segment events (refer to track for an example). If you're running an experiment with the userId as your unit type, this userID should match the user identifier that you log with the Statsig SDK.If you're using a custom ID as the unit type for your experiment, you can provide this identifier using the key statsigCustomIDs as part of the Segment properties field as shown below.
bash
{
  ...
  properties: {
    "statsigCustomIDs": [ "companyID", "<this_company_id>"]
  }
}
The statsigCustomIDs field in properties should be an array, where the even index is the name of the user ID type and the odd index is the value of the previous element in the array. After you create this custom ID type on Statsig (under ID Type Settings in your Project Settings), Statsig automatically recognizes these custom identifiers to compute your experiment results.

Anonymous users

Mapping anonymous users

The Segment integration also allows the mapping of top-level fields to custom IDs you define in Statsig. To do this, visit the Segment panel on the Statsig Integrations page and look for the "Map Identifier" section. Here you can choose fields to map to a Custom ID. This is useful for working with the Segment anonymous ID, which is passed as an event field anonymousId. By defining a custom ID called segmentAnonymousId, Statsig can map your anonymous Segment traffic to your metric data.

Values passed in properties.statsigCustomIDs take precedence over mapped identifiers below.

Segment integration card with Map Identifiers form mapping anonymousId to custom ID

Experimenting on anonymous traffic

For example, if you're running experiments on anonymous users, you can use Segment's anonymousId as the unit of randomization. First, add a new customer identifier to Statsig. In this example, the new custom ID is called segmentAnonymousId. Then, when initializing the Statsig SDK, pass the Segment anonymousId to Statsig as a custom ID if it is available. For example, your Statsig initialization may look like this:
jsx
import { StatsigClient } from '@statsig/js-client';

await Statsig.initialize(
  "client-sdk-key",
  
);
const client = new StatsigClient(sdkKey,
  {
    userID: "some_user_id",
    customIDs: {
      segmentAnonymousId: analytics.user().anonymousId()
    }
  },
  { environment: { tier: "production" } }
);

You can access Segment's anonymousId using analytics.user().anonymousId() as outlined in the Segment docs here.

The Segment SDK may initialize after the Statsig SDK, and the anonymousId may not be available in that scenario. Check this in your specific implementation. If you're using vanilla JavaScript, wait for the anonymousId from the Segment SDK after initialization, then call the updateUser() method on the Statsig SDK to update this ID.

Example mapping flow

Refer to the following diagram for an overview of mapping anonymousIds in Segment to a custom ID representing anonymous users in Statsig:

Identifier mapping flowchart

  1. Initialize the Statsig SDK with your Statsig User, which contains an optional userID value and a customID that you've created in the Statsig UI - segmentAnonymousId in this example.
  2. As you orchestrate features/experiments, Statsig associates this user to a variant using the unit of randomization chosen. For anonymous users, Statsig uses segmentAnonymousId.
  3. Your existing Segment implementation tracks user traffic and associates anonymous users to the top-level field anonymousId.
  4. This anonymousId is mapped in Statsig (to segmentAnonymousId), properly associating the identifier used in experiment exposures to the same identifier used to track user actions.

Syncing Statsig Segment ID lists with Segment Engage Audiences

Using Segment Engage Audiences, you can maintain a list of users for targeting using Statsig Feature Gates. To configure this:
  1. Create a Statsig ID List Segment on the Statsig Console.
  2. Follow the Segment guide for Audiences to create a new Audience and choose Statsig as a Destination. The audience_key must match the ID of the Statsig ID List Segment created.

After you complete these steps, Statsig syncs your Segment Audience, and you can target those users for features you develop or experiments you run.

Custom properties

Passing custom properties to a Statsig User (see custom field) enables targeting on specific cohorts of users in feature gates and experimentation. Custom user properties also let you filter results to specific populations (for example: android/iOS, isVIP) when reading pulse results.If you're using custom fields to target users in your feature gates, provide these properties through Segment using the key statsigCustom as part of the Segment properties field, as an array of key-value pairs: [key1, value1, key2, value2, ...]. For example:
bash
{
  ...
  properties: {
    "statsigCustom": [ "isVIP", "true", "marketing_campaign", "abx343", ...]
  }
}

Configuring outbound events to Segment

To export your Statsig events to Segment,

  1. In your Segment app, add a new source → search for Statsig → click Next

Segment outbound configuration selecting Statsig destination

  1. Name your source → Create Source → click Done:

Segment workspace auth prompt for outbound integration

  1. Locate your Write Key and copy it.
  2. Log into the Statsig console and navigate to the Integrations page.
  3. Click on the Segment card and switch to the Outbound tab, paste the Write Key into the API Key text box shown below, and click Enable.

Segment outbound integration configuration interface

Outbound event schema

Statsig exports log events and exposure events to segment as track events:

plaintext
{
    type: 'track',
    userId: event.userID,
    timestamp: Number(event.timestamp),
    event: event.eventName,
    context: {
      user: event.user,
      value: event.value,
      metadata: event.metadata,
      library: {
        name: 'statsig',
        version: '1.0',
      },
      stableID: event.statsigMetadata.stableID // stableID, if you are relying on that for anonymous users
    },
    properties: {
      value: event.value,
      metadata: event.metadata,
    },
  };
}

Config Change events follow this schema:

plaintext
{
  userId: statsigUserID,
  timestamp: Date.now(),
  event: 'statsig::config_change',
  context: {
    library: {
      name: 'statsig',
      version: '1.0',
    },
  },
  properties: {
    author: author,
    configName: configName,
    description: changeDescription,
    environment: environment,
  },
},

Environments

By default, all events are treated as "production" events, but you can differentiate your event traffic by specifying the environment the events are coming from. This prevents non-production data from appearing in your production metrics.

To include the environment tier, add it to the properties object of your event. The required format is below:

json
{
  ...
  "properties": {
   "statsigEnvironment": {
        "tier": "staging"
    }
  }
}
To learn more about environments, refer to Using Environment.

Working with Segment metrics in the Statsig UI

Segment events are piped into Statsig and are accessible in the metrics console like any other event. These metrics are also available as monitoring metrics in your feature gates and experiments, so you can use your existing metric collection through Segment with Statsig's experimentation platform.

Segment events are prepended with segment:: so they can be easily distinguished from other sources

Segment destination configuration for first exposures

These metrics appear in pulse results among other monitoring metrics:

Segment HTTP endpoint settings

Filtering events

You can customize which events are sent and received through Segment using Event Filtering.

Was this helpful?