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
1avsb.track.segment(segmentKey: string, segmentValue: string) => voidParameters
segmentKeystringRequiredThe 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".
segmentValuestringRequiredThe 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:
- Go to Settings in the left sidebar and click Segments.
- Click New segment and enter the segment key exactly as you will use it in code.
- Optionally add a description so your teammates know what the segment represents.
- 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
1// Call this after you know the visitor's tier2avsb.track.segment('userTier', 'premium');1// Tag the visitor with their subscription plan2avsb.track.segment('plan', 'enterprise');1// You can call track.segment() multiple times with different keys2avsb.track.segment('plan', 'pro');3avsb.track.segment('region', 'europe');4avsb.track.segment('accountType', 'business');1// Load the user's profile from your API, then set the segment2fetch('/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:
- Device —
desktop,mobile, ortablet, inferred from the User-Agent string. - Browser —
chrome,firefox,safari,edge, and others. - Platform —
windows,mac,ios,android,linux. - Language— the browser's primary language setting, such as
en-USorfr-FR. - User type —
newfor first-time visitors orreturningfor 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.
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.