Google Optimize will no longer be available after September 30, 2023. Your experiments and personalizations can continue to run until that date. Learn more

Data Layer variable targeting

Target based on key values stored in the data layer.

Instead of referencing JavaScript variables in your targeting conditions, you can reference key-value pairs that are stored in the data layer. Your data layer must conform to the Google Tag Manager data layer specification.

In this article:

When to use Data Layer variable targeting

You may want to create a targeting condition that references shopping cart data or other information available on the page. For example, you might want to target users who are about to purchase a certain product or who just completed a purchase worth more than $100. Instead of retrieving the product ID or purchase amount from JavaScript variables, you could store this information in the data layer and retrieve it there.

Best practices

  1. The data layer should be defined above the Optimize snippet, in the <HEAD> of the page.
  2. If the data layer is called after the Optimize snippet, any variables declared within it will not be available for Optimize to target on page load.
  3. Don't override the data layer later in the page. Instead, ensure that initialization of data layer occurs before the anti-flicker snippet, otherwise the performance of your site will be negatively impacted.

Learn more data layer variables in Tag Manager developer website.

Example: Target high-value visitors

You want to show experiment variations to high-value visitors, defined as those who have orders greater than $100.

To build a targeting condition based on order value, you’ll need to have a key-value pair within the data layer that you can reference. In the following code sample, purchaseTotal contains the order value:

<script>
  dataLayer = [{
    'purchaseTotal': 451
  }];
</script>

To create a rule targeting the purchaseTotal in the data layer, you need to 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 Data Layer variable.
  5. Click Variable, then Create new...
    1. Optionally, click an existing variable to edit it.
  6. Enter a Data Layer variable name – The name of the variable that you want to read from. For example, purchaseTotal.
  7. Name your variable – For example, Purchase total.
  8. Click CREATE VARIABLE.

Optimize data layer variable targeting - configure

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 the following condition and click SAVE.

Variable Match type Value
Purchase total greater than 100

Optimize data layer variable targeting - purchase total > 100

The Purchase total variable returns the value listed in the purchaseTotal variable in the data layer, in the example above, 451.

This condition will evaluate true if:

  • the value of the purchaseTotal variable is greater than 100.

This condition will evaluate false if:

  • the value of the purchaseTotal variable is less than or equal to 100.

Note: Data layer variables are per-page only, not per-session. When used across multiple pages, you should either declare your data layer variables on every page, or use a cookie.

Match types

The following match types are available in data layer variable targeting:

Equals / does not equal

Every character in your query parameter, 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 query parameter does not equal any of the entered values.

Example:

Variable Match type Value
Visitor type equals high-value

Will evaluate true when the data layer variable visitorType is set to high-value.

 

Variable Match type Value
Visitor type does not equal high-value

Will evaluate true when the data layer variable visitorType is not set to high-value.

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 of the query string up to and including the last character in the string you specify. Use the starts with match type when your query parameters are generally unvarying but can include strings at the end that you want to exclude.

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 URL. You can target shopping cart pages that use /thankyou.html at the end of their URLs.

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 data layer variable targeting rule:

Variable              Match type Value
Visitor type equals high-value

...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 helpful?
How can we improve it?
Search
Clear search
Close search
Google apps
Main menu
Search Help Center
true
true
true
true
true
101337