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.
1// Include only premium tier users2return window.userTier === 'premium';1// Include visitors with cart value over $1002return typeof window.cart !== 'undefined' && window.cart.total > 100;1// Include visitors with a specific feature flag enabled2return window.featureFlags && window.featureFlags.newCheckout === true;1// Include visitors who have scrolled at least 200px2return window.scrollY > 200;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.
1// Include visitors who are logged in (data layer key: userStatus)2// Set condition key to: userStatus, value to: loggedIn3
4// Include visitors with a specific page category5// Set condition key to: pageCategory, value to: checkout6
7// Include visitors in a specific customer segment8// Set condition key to: customerSegment, value to: high-valueYour data layer push might look like this in your site code:
1window.dataLayer = window.dataLayer || [];2window.dataLayer.push({3 event: 'pageLoaded',4 userStatus: 'loggedIn',5 pageCategory: 'checkout',6 customerSegment: 'high-value'7});Cookie condition
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.
1// Check if an auth cookie exists (any value)2Cookie name: auth_token3Operator: Exists4
5// Check if a preference cookie has a specific value6Cookie name: ui_theme7Operator: Equals8Value: dark9
10// Exclude visitors with an opt-out cookie11Cookie name: analytics_optout12Operator: Does not existdocument.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
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 conditions11const showFeature = client.getFlag('new_dashboard', false);