/Docs

Custom Conditions

When the built-in condition types are not enough, custom conditions let you write your own targeting logic. You can evaluate any JavaScript expression, read from the data layer, or check for specific cookies. If the condition returns true, the visitor matches. If it returns false, they do not.

Custom condition types

JavaScript condition

A JavaScript condition is a snippet of code that the snippet evaluates at runtime. The code must return a boolean value — true to match, false to not match. You have full access to the global window object, so you can read any variable, property, or function that your application has set on the page.

Check a user property
javascript
1// Include only premium tier users
2return window.userTier === 'premium';
Check the cart value
javascript
1// Include visitors with cart value over $100
2return typeof window.cart !== 'undefined' && window.cart.total > 100;
Check if a feature flag is active
javascript
1// Include visitors with a specific feature flag enabled
2return window.featureFlags && window.featureFlags.newCheckout === true;
Check scroll position
javascript
1// Include visitors who have scrolled at least 200px
2return window.scrollY > 200;
The code runs synchronously
Custom JavaScript conditions must be synchronous. Do not use await, promises, or callbacks. The snippet evaluates the condition inline and expects an immediate boolean result. If your condition throws an error, it will be treated as false.

Data layer condition

Many sites use a data layer — an array on window.dataLayer — to pass structured data between the page and tag managers like Google Tag Manager. A vs B can read from this data layer directly.

In the Audience Builder, add a condition of type Data Layer and enter the key path you want to check. A vs B will search the data layer array from most recent to oldest and return the first matching value.

Data layer condition examples
javascript
1// Include visitors who are logged in (data layer key: userStatus)
2// Set condition key to: userStatus, value to: loggedIn
3
4// Include visitors with a specific page category
5// Set condition key to: pageCategory, value to: checkout
6
7// Include visitors in a specific customer segment
8// Set condition key to: customerSegment, value to: high-value

Your data layer push might look like this in your site code:

Typical data layer push
javascript
1window.dataLayer = window.dataLayer || [];
2window.dataLayer.push({
3 event: 'pageLoaded',
4 userStatus: 'loggedIn',
5 pageCategory: 'checkout',
6 customerSegment: 'high-value'
7});

Cookie conditions check whether a specific browser cookie exists and optionally whether its value matches a particular string. This is useful for targeting visitors who have a specific cookie set by your application — for example, a session cookie, an auth cookie, or a custom preference cookie.

In the Audience Builder, add a condition of type Cookie, then enter the cookie name and optionally a value to match against.

Cookie condition examples
javascript
1// Check if an auth cookie exists (any value)
2Cookie name: auth_token
3Operator: Exists
4
5// Check if a preference cookie has a specific value
6Cookie name: ui_theme
7Operator: Equals
8Value: dark
9
10// Exclude visitors with an opt-out cookie
11Cookie name: analytics_optout
12Operator: Does not exist
Cookie conditions check document.cookie
A vs B reads cookies using document.cookie. This means it can only access first-party cookies that are not marked as HttpOnly. Cookies with the HttpOnly flag are inaccessible to JavaScript and cannot be used in cookie conditions.

Combining custom conditions

Custom conditions can be mixed freely with built-in conditions in the Audience Builder. For example, you might combine a device condition (Mobile) with a custom JavaScript condition (user is logged in) to target logged-in mobile users.

Remember that conditions within an audience are combined with AND logic by default — the visitor must satisfy all conditions. If you add a group, conditions within the group can use OR logic.

Custom Attribute condition (Feature Flags)

Feature Flag projects support a Custom Attribute condition type that targets users based on properties passed to the SDK during flag evaluation. Unlike JavaScript or cookie conditions, custom attributes work in both client-side and server-side SDKs.

When you add a Custom Attribute condition, you select an attribute from the registered list (configured in Audiences > Attributes), choose an operator, and enter a comparison value. The available operators depend on the attribute's type:

  • String — equals, contains, starts with, regex, in list, and more
  • Number — equals, greater than, less than, and more
  • Boolean — equals true or false
  • JSON — contains, exists
Passing attributes to the SDK
typescript
1const client = new AvsbClient({
2 sdkKey: 'sdk_production_abc123',
3 attributes: {
4 userId: 'user_789',
5 plan: 'enterprise',
6 teamSize: 50,
7 },
8});
9
10// Attributes are used to evaluate audience conditions
11const showFeature = client.getFlag('new_dashboard', false);
Also available in Web Experimentation
The Custom Attribute condition also appears in the Web Experimentation audience builder (at the bottom of the conditions list). The snippet evaluator handles it the same way — any attributes set on the client are available for targeting.