Create a variable

Tag Manager

Needed permissions: owner, manage, edit & publish or edit

Video tutorial: watch on YouTube

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 for 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:

  1. Go to Menu > Tag Manager.
  2. Navigate to Variables.
  3. Click Add a variable.
  4. Name the variable.
  5. Select the variable type.
    Create a variable in Tag Manager in Piwik PRO

    Tip: For more about variable types, read this article.

  6. 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
    • 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.

  7. When you’re done, click OK for the variable.
  8. After creating a variable, you’ll be able to select it from the list of variables in tags and triggers.

    Variable in a tag

    Create a variable in Tag Manager in Piwik PRO

    Variable in a custom tag

    Create a variable in Tag Manager in Piwik PRO

    Variable in a trigger

    Create a variable in Tag Manager in Piwik PRO

Was this article helpful?

Technical support

If you still have some questions, visit our community.
There’s always someone ready to help!

Back to help center