Back

The Set Data Values extension sets the value of a data layer variable using text, another data layer variable, or JavaScript code.

See the full list of available extensions.

In this article:

Table of Contents Placeholder

Prerequisites

How it Works

Use this extension to set the value of a data layer variable using text, another data layer variable, or JavaScript code. The data layer variable is specified in the Set configuration option, and the new value assigned is specified in the To field. Multiple variables can be assigned in one instance of the extension. An optional condition may be added to control when the value is assigned.

Using the Extension

Once the extension is added, the following configuration options are available:

  • Set: The data layer variable to set. Select from a drop-down list of variables. Create a new variable by clicking the + button next to the drop-down.
  • To: The value to assign to the variable.  Click the + button to set another variable, or the - button to remove a variable from the extension.
    The value can be one of these options:
    • Text: set the value to a text value that you type.
    • Variable: set to the value of another data layer variable.
    • JS Code: set to the value of a JavaScript code statement.
  • Condition: (Optional) A condition is added to control when variables are assigned. If a condition is added, it must evaluate to true for the variables to be assigned new values.

Examples

Set a Text Value

This example shows how to set a variable to a text value. In this scenario a condition is used to detect if a query string parameter named q is populated, then the data layer variable page_type is set to the text value "search" to indicate that this is a search results page.

  1. Add the Set Data Values extension.
  2. Set the Title to "Set search as the page type".
  3. Select the variable page_type from the the drop-down menu.
  4. Set the value type to Text.
  5. In the text box enter "search".
  6. Click Add Condition. and set the following:
    • Select the variable q (js)
    • Set the condition to is defined.

setDataValues_1.png

Set a Variable Value

This example shows how to set a variable to the value of another variable. In this scenario, if the variable page_name does not have a value it is set to the value of the document.title variable.

  1. Add the Set Data Values extension.
  2. Set the Title to "Set page_name".
  3. Select the variable page_name from the the drop-down menu.
  4. Set the value type to Variable. A variable drop-down list appears.
  5. Select document title from the drop-down menu.
  6. Click Add Condition and set the following:
    • Select the variable page_name .
    • Set the condition to is not defined.
    • Click + to add an Or condition.
    • Select the variable page_name .
    • Set the condition to is defined.
    • Click + to add an And condition.
    • Select the variable page_name .
    • Set the condition to is not populated.

setDataValues_1.png

Set a JavaScript Code Value

This example shows how to set a variable to the value returned by a JavaScript statement. In this scenario, the number of search results on a search page is determined by using jQuery to select the number of search result link elements on the page. The variable search_results is set to the value returned by the JavaScript code.

  1. Add the Set Data Values extension.
  2. Set the Title to "Determine number of search results".
  3. Select the variable search_results from the drop-down menu.
  4. Set the value type to JS Code.
  5. In the text box enter JavaScript code, exactly how it would appear in the page (or a browser console). This example uses a simple jQuery selector and the .length property: $('.search-results a').length;.
  6. Click Add Condition and set the following:
    • Select the variable page_type .
    • Set the condition to equals and enter "search" in the text field.

setDataValues_3.png