Variables

Variables are name-value pairs for which the value is populated during runtime. For example, the predefined variable named "url" has been defined such that its value is the current page URL.

Variables are used in triggers and in tags. In triggers, they are used to define filters that specify when a particular should be executed (e.g.: to execute a pageview trigger when the url variable is “example.com/index.html”). In tags, variables are used to capture dynamic values (e.g.: passing the transaction value and products purchased to a conversion tracking tag).

Google Tag Manager provides a set of predefined variables in each Web or Mobile App container that you create. With these variables, you’re able to create the most commonly needed tags and triggers. However, you can create additional variables to suit your specific requirements.

Note: Built-in variables are a special category of variables that are pre-configured by Google Tag Manager. They replace the variables that used to be generated when creating a new container. Once a selection of built-in variables have been enabled, you can use them just like any other type of variable. Built-in variables cover many of the basic and common variables such as page URL, referrer, click ID,  random numbers, or events. Learn more about built-in variables.

Web Example

Let’s say that you want to fire a GDN Remarketing tag whenever a visitor spends more than $100 on your site. To implement this, you’d create a pageview trigger and add this as trigger condition:

Transaction Amount greater than $100

Since the variable “Transaction Amount” doesn’t exist, you would need to define it. You would tell Google Tag Manager to look for the value of Transaction Amount in a JavaScript variable transactionAmt (assuming this Javascript variable is present on the purchase confirmation page of your website).

During runtime, Google Tag Manager would get the value of Transaction Amount from the specified JavaScript variable. Then, the above trigger would evaluate whether the value is greater than $100.

In addition to using Transaction Amount in a trigger, you could also use it to pass the transaction amount to any conversion tracking tag, such as a DoubleClick Floodlight tag, thereby making it possible for DoubleClick reports to display transaction revenue.

Variable Types for Web

1st party cookie: The value is set to the 1st party cookie with the matching name for domain that the user is currently on. In the case that a cookie with same name is deployed on multiple paths or multiple levels of domain within the same domain, the first value will be chosen. This is the same is if you had called document.cookie from within a page and chosen the first result.

Built-In Variables: These are a special set of commonly-used, pre-created, non-customizable variables that you can select to make available to Google Tag Manager. Learn more.

Constant String: The value is set to the string you provide. Since this string will always be the same, and is simply the string that you provide here, the Constant String variable type is limited in usefulness. However, if you want to set a standard company name across your site, for example, you could define it as a Constant String type variable. This would allow you to easily update the string in Google Tag Manager and see it reflected across all the tags that use this variable.

Container Version Number: When the container is in preview mode, the container version variable returns the container's preview version number. Otherwise, this variable returns the container's live version number.

Custom JavaScript: The value is set to the result of a JavaScript function. The JavaScript must take the form of an anonymous function that returns a value. For example, you could write a custom JavaScript variable called "lowerUrl" that operates on the predefined {{url}} variable:

function () {
  return {{url}}.toLowerCase();
}

Data Layer Variable: The value is set to ‘value’ when the following code on your website is executed: dataLayer.push({'Data Layer Name': 'value'})

You can specify, in Google Tag Manager, how dots ('.') are to be interpreted in the data layer variable name:

  • Version 1: allow dots in key names. For example, for dataLayer.push('a.b.c': 'value'), interpret the name of the key as "a.b.c" (i.e. {'a.b.c': 'value'}).
  • Version 2: interpret dots as nested values. For example, interpret dataLayer.push({'a.b.c': 'value'}) as three nested levels: {a: {b: {c: 'value'}}}. This allows you to read nested values; you could set the variable name to 'a.b' and it would return the object {c: 'value'} (according to standard JavaScript rules). Nested pushing also allows you to directly edit nested values, so executing:
    dataLayer.push({'a.b.c': 'value'});
    dataLayer.push({'a.b.d': 4});

    on your page would result in a dataLayer that looks like {a: {b: {c: 'value', d: 4}}}.

Debug Mode: The value is set to true if the container is being viewed in debug mode.

DOM Element: The value is set to the text of the DOM (Document Object Model) element or the value of the specified DOM element attribute. If the value you're looking for was not set up in the data layer (see Data Layer variable type, above), it's possible that the value can be retrieved from the DOM. Use this variable type if you can find the value you're looking for in the DOM by entering the value of the element's ID attribute.

If the optional attribute name is set, the variable's value will return the value specified from that attribute (e.g. data-food="cupcakes"); otherwise, the variable's value will be the text within the DOM element.

HTTP Referrer: The value is set to the HTTP referrer, the previous page that the person visited. For example, if a person navigates to one of your product pages from the home page, the referrer will be the home page.  An instance of this variable type is automatically created by Google Tag Manager but you can create additional instances if you would like to have expose different part(s) of the referrer URL.

JavaScript Variable: The value is set to that of the global variable you specify. If the value you're looking for was not set up in the data layer (see Data Layer variable type), and it's not visible in the DOM (see DOM Element variable types), it's possible that the value can be retrieved from a JavaScript variable. Use this variable type if you can find the value you're looking for in the source of the page in the form of a JavaScript variable.

Lookup Table: The value is set according to the instructions in the lookup table. The lookup table contains two columns (Table empty to illustrate how data is used later):

When [select variable] equals

Set [this variable] to

   
 

The Lookup Table type allows you to create a variable for which the value varies according to the value in another variable. This is useful if your website is set up in such a way that the appropriate value (for example, a conversion tracking ID) can be mapped to the URL or another aspect of the page. In this example, a variable named Conversion ID is being created. If the URL is “/thanks/buy1.html”, the value is set to “12345”; if the URL is “thanks/buy2.html”, the value is set to “34567”. There is no limit to the number of rows in the lookup table. Fields are case sensitive.

When {{url}} equals

Set {{Conversion ID}} to

http://example.com/thanks/buy1.html

12345

http://example.com/thanks/buy2.html

34567

http://example.com/thanks/buy3.html

56789

Random number: The value is set to a random number between 0 and 2147483647.

Built-in variables: These variables are populated automatically by Google Tag Manager when certain on-page events occur such as link click, element click, form submit, etc. Learn more.

URL: This type of variable allows you to parse and expose URL components. Google Tag Manager automatically creates 3 instances of this variable type (full url, hostname and path). You can create additional instances to expose different parts of the URL. The URL components you can choose from are: Protocol, Hostname, Port, Path, Query, and Fragment. The input value set for variables of this type is the url of the current page the user is on (retrieved from document.location). By adjusting the URL Source setting, it is possible to tell Google Tag Manager to use another variable as the source of the url value.

Variable Types for Mobile Apps

Application Name: The value is set to the currently running application. A predefined variable of this type is provided in mobile app containers; you don't need to define a new variable of this type.

Application Version: The value is set to the version of the currently running application. A predefined variable of this type is provided in mobile app containers; you don't need to define a new variable of this type.

Constant String: The value is set to the string you provide.

Device Name: The value is set to the device name of the currently running application (e.g., "Samsung Android", "Android SDK built for x86"). A predefined variable of this type is provided in mobile app containers; you don't need to define a new variable of this type.

Function Call: The value is set to the return value of a call to a pre-registered function. To learn more, refer to the SDK documentation (Android or iOS).

Language: The value is set to the two letter language code representing the user-set device language. A predefined variable of this type is provided in mobile app containers; you don't need to define a new variable of this type.

Operating System Version: The value is set to the version of the operating system in which the application is installed.

Platform: The value is set to the platform of the currently running application (one of "Android" or "iOS"). A predefined variable of this type is provided in mobile app containers; you don't need to define a new variable of this type.

Random Number: The value is set to a random number between 0 and 2147483647.

Screen Resolution: The value is set to the screen resolution of the device of the currently running application. The format is "width x height", e.g., "1024x768". A predefined variable of this type is provided in mobile app containers; you don't need to define a new variable of this type.

SDK Version: The value is set to the SDK version of the operating system in which the application is installed. A predefined variable of this type is provided in mobile app containers; you don't need to define a new variable of this type.

Value Collection: This variable contains a set of key-value pairs expressed in JSON format. You use a Value Collection to set the configuration values for your application. For a race driving game app, for example, you might define an "App settings" variable of type Value Collection with the following:

{
  "max-fuel": 200,
  "starting-fuel": 100,
  "fuel-burn-rate": 20
}

Your mobile app can retrieve a value in the variable by providing the key. For example:

public class MainActivity {

  // Add your public container ID.
  private static final String CONTAINER_ID = "XXX-YYY"; 
  
  // Container configuration value keys, used later 
  // for retrieving values.
  private static final String MAX_FUEL_KEY = "max-fuel";
  private static final String INIT_FUEL_KEY = "init-fuel";
  private static final String FUEL_BURN_KEY = "fuel-burn-rate";

  // Rest of your onCreate code.
  }
}

/*
* Method to update game configuration values using a
* Google Tag Manager container.
*/
public void updateConfigurationFromContainer(Container container) {

  // Get reference to the container.
  Container container = mFutureContainer.get();

  // Update game settings using Container 
  // configuration value keys.
  maxFuel = mContainer.getDoubleValue(MAX_FUEL_KEY);
  startingFuel = mContainer.getDoubleValue(INIT_FUEL_KEY);
  burnRate = mContainer.getDoubleValue(FUEL_BURN_KEY);
}

A Value Collection variable has triggers associated with it. For configuration values that apply to all instances and versions of your app, set the enabling trigger to the predefined Always. Refer to the developer documentation (Android or iOS) for details on how to use the Value Collection variable. 

Was this article helpful?