Set Data Values Extension
The Set Data Values extension sets the value of a data layer variable using text, another data layer variable, or JavaScript code.
Prerequisites
- utag v4.38 or later. For more information about updating the
utag.js
template, see our knowledge base article Best Practices for Updating to the Latest Version of utag.js. - Familiarize yourself with how extensions work.
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.
- Add the Set Data Values extension.
- Set the Title to Set search as the page type.
- Select the variable page_type from the the drop-down menu.
- Set the value type to Text.
- In the text box, enter
search
. - Click Add Condition and set the following:
- Select the variable q (js).
- Set the condition to is defined.
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.
- Add the Set Data Values extension.
- Set the Title to
Set page_name
. - Select the variable page_name from the the drop-down menu.
- Set the value type to Variable. A variable drop-down list appears.
- Select document title from the drop-down menu.
- 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.
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.
- Add the Set Data Values extension.
- Set the Title to
Determine number of search results
. - Select the variable search_results from the drop-down menu.
- Set the value type to JS Code.
- 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;
. - Click Add Condition and set the following:
- Select the variable page_type.
- Set the condition to equals and enter
search
in the text field.
This page was last updated: December 28, 2023