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, or check for specific cookies. If the condition returns true, the visitor matches. If it returns false, they do not.

  1. The JavaScript condition's code field: paste the lines that go inside the braces of the examples below.
  2. The Cookie condition's Cookie name and Cookie value fields.

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.

A vs B runs the field's contents as a function body (new Function(yourCode)()), which is why every example below ends in a return. The examples show that body inside its wrapper so the types line up; paste only the lines inside the braces into the condition field.

If you keep your site's globals in TypeScript, declare them once and every condition below is checked against the real shapes:

TypeScript
declare global {  interface Window {    userTier?: string    cart?: { total: number }    featureFlags?: { newCheckout?: boolean }  }}
TypeScript7 lines

Check a user property:

TypeScript
function condition(): boolean {  // Include only premium tier users  return window.userTier === 'premium'}
TypeScript4 lines

Check the cart value:

TypeScript
function condition(): boolean {  // Include visitors with cart value over $100  return typeof window.cart !== 'undefined' && window.cart.total > 100}
TypeScript4 lines

Check if a feature flag is active:

TypeScript
function condition(): boolean {  // Include visitors with a specific feature flag enabled  return window.featureFlags?.newCheckout === true}
TypeScript4 lines

Check scroll position:

TypeScript
function condition(): boolean {  // Include visitors who have scrolled at least 200px  return window.scrollY > 200}
TypeScript4 lines

Read a value from a data layer (many sites push structured data to window.dataLayer for tag managers like Google Tag Manager). There is no dedicated Data Layer condition type; a JavaScript condition reads it directly, since window.dataLayer is just a regular array on the page:

TypeScript
declare global {  interface Window {    dataLayer?: Record<string, unknown>[]  }}function condition(): boolean {  // Search the data layer from most recent entry to oldest, and match the  // first one that set userStatus  const layer = window.dataLayer ?? []  const entry = [...layer].reverse().find((e) => 'userStatus' in e)  return entry?.userStatus === 'loggedIn'}
TypeScript13 lines

A push your site code makes might look like this:

JavaScript
window.dataLayer = window.dataLayer || [];window.dataLayer.push({  event: 'pageLoaded',  userStatus: 'loggedIn',  pageCategory: 'checkout',  customerSegment: 'high-value'});
JavaScript7 lines
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.

Cookie conditions check whether a specific browser cookie exists, and optionally whether its value matches a string you give. Use it to target visitors who have a cookie your application set: a session cookie, an auth cookie, or a custom preference cookie.

In the Audience Builder (the drag-and-drop screen where you build a targeting rule from conditions), add a condition of type Cookie. Then enter the cookie name and, optionally, a value to match against.

Cookie condition examples:

Plain text
Check if an auth cookie exists (any value)  Cookie name: auth_token  Operator:    ExistsCheck if a preference cookie has a specific value  Cookie name: ui_theme  Operator:    Equals  Value:       darkExclude visitors with an opt-out cookie  Cookie name: analytics_optout  Operator:    Does not exist
Plain text12 lines
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. It targets users based on properties your own code passes to the SDK during flag evaluation. Unlike JavaScript or cookie conditions, custom attributes work in both client-side and server-side SDKs.

To add a Custom Attribute condition, first pick an attribute from the registered list (configured in Audiences > Attributes). Then 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

Attributes ride on the evaluation context: the bundle of facts about a visitor (their id, plan, or any other property) that your code hands to the SDK when it asks for a flag. Attributes sit alongside the kind and key that identify the user:

TypeScript
import { AvsbClient } from '@avsbhq/browser'const client = new AvsbClient({  sdkKey: 'sdk_production_xxxxxxxxxxxxxxxx',  context: {    kind: 'user',    key: 'user_789',    plan: 'enterprise',    teamSize: 50,  },})// Those attributes are what audience conditions are evaluated against.// getFlag returns a Flag object, so read `.value` for the variation value.const showFeature = client.getFlag('new_dashboard', false).value
TypeScript15 lines
Feature Flag projects only

Custom Attribute is not offered in Web Experimentation audiences, and it is no longer listed in that builder's sidebar. It matches data your own code hands to an SDK. The snippet that runs on your website is never given any of that data, so a rule of this kind never matches anyone on a website. To target website visitors on something of your own, use a JavaScript condition or a cookie condition instead. A custom segment records the same kind of data, but only for breaking down results after the fact: it cannot decide who enters an experiment.

Do you have a website audience saved earlier that still uses Custom Attribute? That rule never matches anyone, so the audience is not reaching the people it was set up for. Open the audience, replace the rule with one of the options above, and save.

Was this helpful?