/Docs

Custom Segments

Segments let you attach attributes to visitors so you can slice your experiment results by those attributes later. For example, you might tag visitors with their subscription plan, their account tier, or their region — then on the results page, filter to see how the experiment performed for premium users versus free users.

Method signature

typescript
1avsb.track.segment(segmentKey: string, segmentValue: string) => void

Parameters

NameDescription
segmentKeystringRequired

The name of the segment attribute. This must match a segment key defined in your organization settings. Segment keys are case-sensitive. Examples: "userTier", "plan", "accountType".

segmentValuestringRequired

The value to assign for this visitor. Must be a string. Examples: "premium", "enterprise", "free", "returning".

Defining segments first

Before you can use a segment key in avsb.track.segment(), you must define it in your organization settings. Calls with unknown segment keys are silently ignored — no error is thrown, but the data is not recorded.

To define a custom segment:

  1. Go to Settings in the left sidebar and click Segments.
  2. Click New segment and enter the segment key exactly as you will use it in code.
  3. Optionally add a description so your teammates know what the segment represents.
  4. Save the segment.

The segment key is now registered and calls to avsb.track.segment() using that key will be recorded.

When to call it

Call avsb.track.segment()as early in the page lifecycle as possible — ideally right after you know the visitor's attributes. A good pattern is to call it immediately after your authentication check or after loading the user's profile from your API. The earlier you call it, the more experiment exposures will have the segment attached.

If you call avsb.track.segment() after a visitor has already been counted in an experiment exposure, the segment will still be applied retroactively to that exposure record. However, for reliability, calling it before any experiment runs is best.

Code examples

Set user tier
javascript
1// Call this after you know the visitor's tier
2avsb.track.segment('userTier', 'premium');
Set subscription plan
javascript
1// Tag the visitor with their subscription plan
2avsb.track.segment('plan', 'enterprise');
Set multiple attributes
javascript
1// You can call track.segment() multiple times with different keys
2avsb.track.segment('plan', 'pro');
3avsb.track.segment('region', 'europe');
4avsb.track.segment('accountType', 'business');
Set segment after loading user profile
javascript
1// Load the user's profile from your API, then set the segment
2fetch('/api/me')
3 .then(function(response) { return response.json(); })
4 .then(function(user) {
5 avsb.track.segment('plan', user.plan);
6 avsb.track.segment('userTier', user.tier);
7 });

Auto-collected segments

A vs B automatically collects several segments for every visitor without you needing to call avsb.track.segment(). These are available on the results page as filter options out of the box:

  • Devicedesktop, mobile, or tablet, inferred from the User-Agent string.
  • Browserchrome, firefox,safari, edge, and others.
  • Platformwindows, mac,ios, android, linux.
  • Language— the browser's primary language setting, such as en-US or fr-FR.
  • User typenew for first-time visitors or returning for visitors who have been to the site before (based on whether the visitor cookie already existed).

These automatic segments are always present and do not require any setup. Custom segments you define are in addition to these.

Using segments to filter results

Once a segment is being collected, it appears as a filter in your experiment results. On the results page, look for the Segments panel. You can select any segment key and filter the results table to show only visitors with a specific value.

For example, if you tagged visitors with avsb.track.segment('plan', 'premium'), you can filter the results to see conversion rates specifically for premium subscribers. This can reveal whether your experiment had different effects on different user groups — a finding that is often more valuable than the overall result.

Segments are per-visitor, not per-experiment
Segment values are stored on the visitor record, not tied to a specific experiment. A segment you set for one experiment automatically applies to all other running experiments for the same visitor. This means if you consistently tag visitors with their plan, every experiment benefits from that segmentation data without any extra work.
Segment keys must be defined in org settings
Calling avsb.track.segment() with a key that has not been defined in your organization settings will not throw an error, but the data will be silently discarded. Always create the segment key in settings first before deploying code that uses it.