A variable is used to store information that can be accessed by Tag Manager. For example, a variable page URL
holds and passes information about the URL of the current page.
In Tag Manager, you can use built-in variables — like a page URL, page path, referrer — or, you can create a custom variable.
In this article, we’ll show you how to create a variable.
Before you start
Here are some things to know before you start working with variables:
- Variables can be used in tags and triggers.
- You can create a variable in Tag Manager or via dataLayer push in JavaScript. But, a variable created via dataLayer push can be used only in custom tags.
- A variable is written as a text wrapped in double curly braces
{{ variable }}
. - A variable is often used to store API keys to make them available to tags.
- Variables are also used to capture and pass additional information from your website. For example, the author of a blog post.
Available from 16.30.0
You can use a variable inside another variable. Just keep in mind that a variable can’t contain a circular dependency. Don’t create a closed loop. Example: A depends on B, B depends on C, C depends on A.
Create a variable
To create a variable, follow these steps:
- Go to Menu > Tag Manager.
- Navigate to Variables.
- Click Add a variable.
- Name the variable.
- Select the variable type.
Tip: For more about variable types, read this article.
- Set up your variable. Each variable has different settings:
- Custom JS variable: Write a JavaScript function that ends with a return statement. Read more
- Constant variable: Type in a constant value. Read more
- Cookie variable: Type in a cookie name. Read more
- Data layer variable: Type in a data layer variable name. Read more
- Document variable: Type in a document name. Read more
- DOM element variable: Type in an element ID, CSS selector, or XPath. Read more
Available from 16.33.0
Lookup table variable: Choose the input variable and map inputs to outputs. Read more- Random number: You don’t need to set anything here. This variable gives a random value between 0 and 1 (inclusive of 0, but not 1). Read more
- URL variable: Choose Get parameter, Query or Protocol. Read more
Note:
Available from 16.30.0
You can use a variable inside another variable. Just keep in mind that a variable can’t contain a circular dependency. Don’t create a closed loop. Example: A depends on B, B depends on C, C depends on A. - When you’re done, click OK for the variable.
- After creating a variable, you’ll be able to select it from the list of variables in tags and triggers.
Variable in a tag
Variable in a custom tag
Variable in a trigger