Variables in Google Tag Manager (GTM) are very similar to variables in JavaScript. Each event on a website has its own variables that contain information about that event. For example, when we click the “Add to Cart” button in an online store, the variables of this event will contain the values of the CSS class of this element or the text of the element (button) itself. When clicking on a link, the URL address will also be passed in a variable.
Variables can be used in both triggers and tags. In triggers, they are used as an activation filter, i.e., when to launch a particular tag.
More Context:
Events and Variables: In GTM, events are user interactions on your website, such as clicks, form submissions, or page views. Variables capture specific information about these events, like the clicked element, the URL of the page, or the value of a form field.
– Triggers: Triggers are conditions that determine when a tag should fire. You can use variables within triggers to define specific conditions, like firing a tag only when a certain button is clicked or when a user visits a specific page.
– Tags: Tags are snippets of code that send information to third-party platforms, such as Google Analytics or Facebook Pixel. You can use variables within tags to dynamically insert values into the code, like sending the product name or price to Google Analytics when a purchase is made.
Example:
Let’s say you want to track clicks on all “Add to Cart” buttons on your website. You could create a trigger that fires when the clicked element contains the text “Add to Cart”. Then, you could create a tag that sends an event to Google Analytics whenever this trigger fires.
Official Google Documentation:
For more information, you can refer to the official Google documentation on variables in GTM: