Search
Clear search
Close search
Google apps
Main menu

JavaScript variable targeting

Target pages based upon JavaScript variable values.

Use this type of targeting if you can find the value you're looking for in the source code of the webpage in the form of a JavaScript variable.

In this article:

When to use JavaScript variable targeting

Information such as product, cart and page details are often stored in JavaScript variables. Optimize can serve experiments based on these values using JavaScript variable targeting.

Before using JavaScript variable targeting, consider the following questions:

Does the information exist in the... Then use...
URL? URL targeting
URL query parameters? Query Parameter targeting
Data layer? Data Layer variable targeting

If the only way to retrieve the information you need is from a JavaScript variable, use JavaScript variable targeting. However, be aware that if the JavaScript variable you’re referencing is deleted, or if the name of the variable changes, your targeting condition will no longer work. For this reason, it’s best to use JavaScript variable targeting only when the other targeting rules will not suffice.

Note: All user-defined JavaScript must be declared above the Optimize container snippet, in the <HEAD> of the page. JavaScript declared after the Optimize snippet will not be available to target on page load. Learn more about the placement of the Optimize snippet.

Example: Target visitors whose cart value is between $90 and $100

You want to target customers with between $90 and $100 worth of merchandise in their cart with variations of a free shipping promotion. Assumptions:

  • The minimum purchase required to get free shipping is $100.
  • You store your customer's cart value in a variable on your checkout page.

To create this targeting rule, you'll first create a custom variable, then build a condition with it.

Step 1: Create a custom variable

  1. Create or edit an experiment.
  2. Click the TARGETING tab.
  3. Click AND to add a new targeting rule.
  4. Click JavaScript variable.
  5. Click Variable, then Create new...
    1. Optionally, click an existing variable to edit it.
  6. Enter a Data Layer Variable Name – for example, cartTotal.
  7. Name your variable – for example, Cart total value.
  8. Click CREATE VARIABLE.

Optimize JavaScript variable targeting - configuration.

Note: A variable name may refer to a variable nested in an object (e.g. "myApp.data.cartTotal")

Step 2: Build a condition with your custom variable

After creating your custom variable, Optimize will populate it in a new targeting condition which you can complete by adding a match type and value. For this example, build two JavaScript variable conditions joined by AND then click SAVE.

Condition 1:

Variable Match type Value
Cart total value greater than 90

Optimize JavaScript variable targeting - condition builder 1

Condition 2:

Variable Match type Value
Cart total value less than 100

Optimize JavaScript variable targeting - condition builder 2.

The cart_total variable returns the value of your visitor’s shopping cart. The targeting criteria above will evaluate as true when the user has between $90 and $100 worth of merchandise in their shopping cart.

This condition will evaluate true if:

  • the value of the Cart total value variable is greater than 90 AND less than 100.

This condition will evaluate false if:

  • the value of the Cart total value variable is not greater than 90 AND less than 100.

Match types

The following match types are available in JavaScript variable targeting:

Equals / does not equal

Every character, from beginning to end, must be an exact match of the entered value for the condition to evaluate as true. A condition using does not equal will evaluate as true when the variable does not equal the entered value.

Example:

Variable Match type Value
Cart total value equals 100

Will evaluate true when the Cart total value is 100.

 

Variable Match type Value
Cart total value does not equal 100

Will evaluate true when the Cart total value is anything other than 100.

Contains / does not contain

The contains match type (also known as a "substring match") allows you to target any occurrence of a substring with a longer string. Contains is useful when targeting a unique query string parameter that appears in multiple URLs.

Example:

Variable Match type Value
page contains 4

Will evaluate true for:

  • http://www.example.com/member.cgi?id=9&page=4
Variable Match type Value
page does not contain page=4

Will evaluate true for:

  • http://www.example.com/member.cgi?id=9&page=2

Starts with / does not start with

The starts with match type matches identical characters starting from the beginning up to and including the last character in the string you specify.

Example:

Variable Match type Value
page starts with 4

Will evaluate true for:

  • http://www.example.com/member.cgi?id=9&page=4
Variable Match type Value
page does not start with 4

Will evaluate true for:

  • http://www.example.com/member.cgi?id=9&page=2

Ends with / doesn't end with

An exact match of the entered value with the end of the variable. 

Example:

Variable Match type Value
page ends with 4

Will evaluate true for:

  • http://www.example.com/member.cgi?id=9&page=24
Variable Match type Value
page does not end with 4

Will evaluate true for:

  • http://www.example.com/member.cgi?id=9&page=42

Regex matches / does not regex match

A regular expression uses special characters to enable wildcard and flexible matching. Regex matches are useful when the stem, trailing parameters, or both, can vary in the URLs for the same webpage. If a user could be coming from one of many subdomains, and your URLs use session identifiers, you could use a regular expression to define the constant element of your URL.

Example:

Variable Match type Value
page regex matches checkout.cgi\?page=1

Will evaluate true for:

  • http://sports.example.com/checkout.cgi?page=1&id=123
  • http://fishing.example.com/checkout.cgi?page=1&lang=en&id=123
Variable Match type Value
page does not regex match checkout.cgi\?page=1

Will evaluate true for:

  • http://sports.example.com/checkout.cgi?page=2&id=123

Operators

AND

The AND operator is useful when you wish to target a variation based on multiple rules that all need to be true. Conditions using the AND operator will only evaluate as true when all of the values are met.

Example:

To target users searching for nexus, while browsing from a tablet, create two rules joined by the AND operator.

A query parameter targeting rule:

Variable              Match type Value
search query equals nexus

...and a technology targeting rule:

Variable Match type Value
Device Category equals tablet              

OR

The OR operator is useful for targeting one kind of page with multiple URL configurations. Conditions using the OR operator will evaluate as true when any of the values are met. You can use OR by adding additional values in a URL targeting rule. When targeting URLs, OR is automatically appended to your first URL after pressing return. Simply type additional URLs (followed by return) to continue building conditions with the OR operator.

Example:

To target searches on your website for either nexus or chromecast, create a rule with two site search in the Value field. You'll notice that OR is automatically added after you press enter.

Variable Match type Value
search query equals nexus or... chromecast

The OR operator is supported within individual conditions but you can't use it to combine multiple conditions. For example, you can build a condition like this: "path equals /store OR /shop" but you can't use OR across multiple conditions like this: "path equals /store OR host equals example.com"

Was this article helpful?
How can we improve it?