Go Back

Variables Overview

January 29, 2018

Every software developer understands the importance of Variables in programming. Piwik PRO Tag Manager lets its users easily use them in their tags to serve better content, react to events and much more. Allow me to teach you a little bit about this killer-feature!

Variable is a placeholder for a value that will be provided when the tag is run. It can be defined in Tag Manager’s Variables or via dataLayer push in JavaScript. Tag Manager’s Variable can be used in tag templates, custom tags and triggers’ conditions, but dataLayer’s defined variable can only be used in custom tags. For it to be usable in conditions it needs to be passed to Tag Manager’s Data layer variable type.

When one wants to use a Variable it is syntactically a text wrapped in double curly braces, eg. {{ myVariable }} Our custom tag with Variable could look like this:

<script type="text/javascript">
console.log('value of my variable: ' + {{ strVariable }});

Tag Manager takes measure to properly replace this placeholder with the Variable value. That means if the value is string it is properly wrapped with string delimiter – apostrophes.

We could also write this snippet following way:

<script type="text/javascript">
console.log('value of my variable: {{ strVariable }}');

And that would work as well. Tag Manager is knowledgeable enough to not wrap our variable in the string delimiters.

And the output would be identical to previous one, in browsers console, we would see the value of my variable: testString, if we have set strVariable’s value to testString.

Why would you need Variables?

The simplest explanation is to store API keys in single place and use them in your Tags via Variables. More advanced usage would be to get even more information to your scripts – you could simply once get a GET parameter and reuse it in your tags. You can also pass some data from your CMS via dataLayer variables.

Couple other use cases that might profit from Variables are:

  • Serving the tag to the visitor based on his geographical location,
  • Reacting differently to a logged and not-logged users,
  • Setting the URL and PageTitle values in the Single Page Applications for Analytics purposes,
  • Passing values of purchased products to Piwik PRO E-commerce function.