The Dynamic Variable (DVar) template allows you to collect additional information on the session that can be used to segment. It can be used to flag almost any type of user behavior, or page layout.
These instructions are to be used alongside the Contentsquare Tag Configurator article, which includes details on prerequisites and Snippet creation.
Commonly used to
- Segment and compare: user sessions in your analysis.
- A/B testing: When you have binary or multiple values of something, such as an AB test or a multivariate test, where the experiment name remains the same while the test and controls change name.
How to use the template
Once you have created your Snippet, use the steps below to configure this selected template and the chosen trigger.
Step 1: Complete the following template fields
- Iterable context: Enter the iterable context you wish to collect
- Key
- Value
- Type: Select your value type (String or Integer)
Defining a DVar
DVars consist of a key-value pair where the values can be either a string, or a number (integer).
- For values of type number, you can use operators (more, less than, etc)
- For values of type string, autocompletion and regular expressions are available
You can pass up to 20 DVars per page. However, note that if you pass more than 1 DVar with the same ‘Key’ value ‘Per pageview’, it will be overwritten, and only the last one will remain in the system.
If the value you want to collect as a DVar is in a global object (such as a data layer), you have two options:
1) If the value is inside a fixed location in the global object, simply pass that into the value field as is, using interpolation.
- For example, to grab the email from window.utag.data.customer_email, simply pass that into the ‘Value’ field as is, using interpolation: ${window.utag.data.customer_email}
2) If, however, the value location within the object is not fixed, then you will need to fill in the iterable context field as well.
- For example, if you want to grab the visitor’s country from their data layer, then set the fields below as follows:
- Iterable context: values(window.dataLayer)[?country]
- Value: ${$.country}
Step 2: Select a Trigger
Select a trigger and define the conditions that will fire the Snippet.
Available Triggers
- Before Pageview
- Form Submit
- Element Click
- History Change
- After Pageview
- Element Exists
- Hash Change
- Element Becomes Visible
- Element Removed
- Mouseover
- Element Added
Template and trigger guidelines
-
Element Click, Element Exist, Change in Element Visibility or Mouseover: If you are using one of these triggers, you can actually pass the target element's text into your DVar value, by putting the following expression into the ‘Value’ field:
- Value: ${element.textContent}
- If using the Change in Element Visibility trigger and your target is an error message that appears under a form field (when a visitor fails to submit it), your error text is "Please fill out your first and last name". Therefore, the resulting DVAR will be:
- Key: Validation error
- Value: Please fill out your first and last name
-
Behavior exception for interpolation: If Interpolated values are undefined at runtime, the template will retry until they resolve
- Retry 20 times each 100ms, after that we abort