What is a DataVariable?

Variables are used for rules evaluation. When you set up a variable, there are a number of options, such as:

  • URL: Lets you match the current page URL (or its parts) against some condition, like checking if the domain ends with .example.com:

    {{URL.hostname}} ends with ".example.com"

  • Variable: A global JavaScript variable in your page, including the predefined one like browser.userAgent.

DataVariable is a way to avoid using global variables. In order to do this, you need to send a named variable to the Tag Manager private namespace with JavaScript code like this:

__mtm_data.push({ variableName : 'variableData' });
// Replace variableName and variableData with respective values

 

Then you can refer to it with a DataVariable, specifying variableName in it.

New DataVariable

 

Here's another way of looking at the difference between Variable and DataVariable:

Imagine you set a JavaScript variable "foo" to "article" at article pages, and to "static" for some static pages. You could set this up as:

<script>
    foo = "article";
</script>

 

And then use a "Variable" variable with name set to "foo" to compare it against the "article" or "static" value in rules. However, this approach creates global JavaScript variable namespace pollution, which is difficult to maintain. The alternative is the "DataVariable" approach, where instead of defining the variable in the global namespace you send it to the container execution script namespace. So instead of the above script you do:

<script>
__mtm_data.push({ foo : "article" });
</script>

 

And then use DataVariable in place of Variable.

Incidentally, the same method is used to trigger user-defined events, sending a specially-named Event variable:

<script>
__mtm_data.push({ event : "myUserDefinedEventName" });
</script>
Have more questions? Submit a request

0 Comments

Please sign in to leave a comment.