Tags, triggers and variables: how to build tracking logic in GTM

30 İyul 2026·Updated: ·⏱ ≈11 min·👁 151 views

Tags, triggers and variables: how to build tracking logic in GTM
Contents

Everyone learning Google Tag Manager gets stuck at the same point. The interface is clear, the account is set up and the container has been added to the site, but it is not yet clear how tags, triggers and variables relate to one another. As a result, people copy ready-made templates, then something stops working and they cannot fix it because they do not understand the logic. Understanding how tags, triggers and variables work together in GTM helps you understand how tracking works.

This article explains that logic. By the end, you will be able not only to create tags, but to break any tracking requirement down into three components with confidence.

👉 The GTM interface and naming conventions

The three components: action, condition and data

In the simplest terms:

  • A tag is the action. What happens: data is sent to a tool.
  • A trigger is the condition. It determines when the action happens.
  • A variable is the data. It provides values used to check conditions and enrich the information being sent.

In a real scenario, all three work together. Imagine a user clicks the “Add to basket” button on a product page:

  1. Trigger checks the conditions for that click and signals that it is time to act.
  2. Variablesprovide the required values: the text of the clicked element, the product ID, the page URL and the product price.
  3. Tag fires and sends this information to GA4 or Meta as an event.

If any one of the three is configured incorrectly, the result is compromised. If the trigger is too broad, the event is sent in places where it is not needed. If the wrong variable is selected, the event arrives but contains no useful detail: just “someone clicked something”. If the tag is configured incorrectly, the data does not arrive at all.

GTM trigger types: which one to choose and when

GTM trigger types are organised into four main groups. You will see this breakdown in the configuration window when you create a new trigger.

1. Page View group: page-related events

Triggers in this group differ according to the page loading stage. Consent Initialization and Initialization fire before the following three stages:

  • Page View: fires as the container loads. This stage follows Consent Initialization and Initialization. Use it for tags based on page views.
  • DOM Ready: fires once the page’s HTML structure has been fully built. Choose this stage when you need to read the content of page elements, such as retrieving a price.
  • Window Loaded: fires once all resources, including images, have loaded. This is the latest stage in the group.

A practical rule: if you are reading data from the DOM, wait for the appropriate loading stage. Waiting too long increases the risk of missing users who leave the page quickly. Initialization is usually selected for the Google tag.

2. Click group: tracking clicks

There are two options, and the difference between them matters:

  • All Elements: captures clicks on any element on the page: buttons, images, text blocks and icons. It is the broadest option.
  • Just Links: captures clicks only on links created with the `<a>` tag. It is useful for tracking external links, file downloads and navigation clicks.

In both cases, you will almost always need to narrow the trigger’s scope with a condition . For example, if you want to track clicks only on buttons, set a condition using the “Click Classes” or “Click Element” variable. If you attach a GA4 Event tag to an All Elements trigger without a condition, an event will be sent for every click. This creates unnecessary data and makes reports harder to analyse.

3. User Engagement group: behaviour events

This is one of the most commonly used groups for measuring user behaviour:

  • Scroll Depth: measures how far a user scrolls down a page. Vertical and horizontal options are available. Set thresholds as percentages or pixels, for example 25, 50, 75 or 90 per cent.
  • Form Submission: fires when a form is submitted. Check separately whether it is measuring successful submissions.
  • Element Visibility: detects when a specific element appears on screen. Use it to measure whether a banner, campaign block or contact section has been seen.
  • YouTube Video: tracks viewing milestones for YouTube videos embedded on the site.

4. Other triggers

  • Timer: fires after a set period of time. The interval is specified in milliseconds: `30000` for 30 seconds. The “Limit” field determines how many times the trigger repeats. Enter `1` for a one-off measurement.
  • Custom Event: used for custom events sent from the site’s code through the data layer. It is one of the key tools for e-commerce tracking.
  • JavaScript Error: fires when a JavaScript error occurs on the site.
  • History Change: fires when the URL fragment or browser history changes. Useful on single-page application (SPA) sites.

Adding a condition to a trigger: the key to accuracy

Choosing the trigger type is only half the job. The other half is narrowing its scope. Most triggers have an option in their configuration to fire only on certain events, where you can set a condition.

Commonly used condition examples:

Page URL contains /thank-you; Page Path equals /elaqe; Click Classes contains btn-primary; Page URL matches RegEx .* (entire site).

Pay particular attention to the RegEx option. When setting up a Timer trigger, you will often need it to run across the entire site. To do this, set the Page URL variable to the `matches RegEx` operator with the value `.*`. This expression matches any text and, in practice, covers all pages.

One of the most common mistakes with conditions is choosing an overly specific match. For example, if you enter `Page URL equals https://misal.az/thank-you`, the condition will not be met as soon as UTM parameters are added to the URL, and your conversions will not be recorded. Instead, use `Page Path equals /thank-you`: the path matches exactly, and URL parameters do not affect the result.

Built-in variables: data ready to use

A variable is a component that returns a value read or calculated when needed. For example, the `Page URL` variable returns the address of the current page.

Google provides a range of variables ready to use. These are built-in variables: you do not need to create them, just enable them. In the Variables section, click “Configure” and select the ones you need from the list.

Only some built-in variables are enabled in a new container. You need to enable the others manually. The most commonly needed ones are:

Page variables

  • Page URL: the full URL, including parameters.
  • Page Path: the path only, without the domain or query parameters.
  • Page Hostname: the hostname, including the subdomain if there is one.
  • Referrer: the address of the previous page supplied by the browser; the full URL is not always available.

Click variables

  • Click Element: the DOM element that was clicked.
  • Click Classes: the CSS classes of that element. This is one of the most commonly used variables for identifying buttons.
  • Click ID, Click Text, Click URL: the identifier and text of the clicked element, and its link URL if there is one.

Scroll variables

  • Scroll Depth Threshold: the percentage or pixel threshold reached by the user, for example 25, 50, 75 or 90 per cent.
  • Scroll Depth Units: the unit of measurement, either percentage or pixels.
  • Scroll Direction: the direction, vertical or horizontal.

Visibility and error variables

  • Percent Visible: the percentage of the element visible on screen when the trigger fires.
  • On-Screen Duration: how long the element has been visible on screen when the trigger fires, in milliseconds.
  • Error Message, Error URL, Error Line: the JavaScript error message, the URL of the file where it occurred and the line of code.

Important rule: there is no need to enable built-in variables you do not use. Unnecessary variables make the list longer in Preview mode and harder to check.

User-defined variables: ones you create yourself

Built-in variables do not meet every need. You can create your own for specific cases. Let’s look at the types most commonly needed in practice.

Constant

Stores a fixed value. The most common use is a GA4 measurement ID. Instead of entering `G-XXXXXXX` each time you create one of dozens of GA4 tags, create a variable once named `constant: ga4 measurement id` and reference it in all your tags. If the ID changes, update it in one place and the change applies everywhere it is referenced. This simple approach saves a lot of time.

Data Layer Variable

Reads values sent from the site’s code through the data layer: product name, price, category and user ID. It is one of the main tools for e-commerce tracking and the subject of a separate article.

Custom JavaScript

When the ready-made options are not enough, you can write a small JavaScript function and use its result as the variable’s value. For example, to combine two values or put text into a particular format.

Auto-Event Variable

Lets you read the properties of a clicked element in more detail. It is used to track external links: if the link’s domain differs from yours, it leads to an external link.

URL Variable

Retrieves a specific part of a URL: a query parameter, hostname or fragment. Useful for reading the values of UTM parameters.

First-Party Cookie

Reads a value stored in a cookie belonging to the site’s domain and accessible to JavaScript, such as the ID of a signed-in user.

GTM tag types and advanced settings

Now that you understand triggers and variables, let’s look at a few important points about tags themselves.

When creating a tag in GTM, you can use Google’s own tags as well as other ready-made tag types and the community template gallery. The most common options are:

  • Google Tag: the base configuration tag for services such as GA4 and Google Ads; it uses a tag ID.
  • GA4 Event: sends specific events, such as scrolls, clicks, form submissions and video views.
  • Google Ads Conversion Tracking: uses a conversion ID and conversion label.
  • Google Ads Remarketing: sends data to build remarketing audiences.
  • Conversion Linker: a supporting tag that stores information about ad clicks; if the relevant Google tag loads on every page, Google Ads you do not need to create a separate one for conversion tracking.
  • Custom HTML: for custom HTML and scripts; it is also used to add code for Meta Pixel, TikTok Pixel and other tools.

Each tag also has an Advanced Settings section, where two settings are useful in practice.

Tag Firing Priority: determines the order in which tags start firing for the same event. The higher the number, the earlier the tag starts. As tags run asynchronously, this setting does not guarantee that one tag will finish loading before another.

Tag Firing Options: limits how many times a tag can fire. There are three options: “Once per event”, “Once per page” and “Unlimited” for tag sequencing. “Once per page” prevents repeat firing until the page is reloaded, but does not eliminate all duplicate conversions.

There is also Tag Sequencing : you can specify that one tag fires before or after another. Useful when configuring tags that depend on one another.

Two practical examples

Example 1: measuring scroll depth on blog articles

Goal: measure how far users get through an article, but only in the blog section.

  1. Go to Variables → Configure → enable the `Scroll Depth Threshold` variable.
  2. Create a new trigger: type Scroll Depth, vertical, with percentage thresholds `25, 50, 75, 90`. Condition: `Page Path contains /blog`.
  3. Create a new tag: GA4 Event, event name `scroll_depth`. Add a parameter: name `percent`, value the `Scroll Depth Threshold` variable.
  4. Link the trigger to the tag, check it in Preview, then publish.

You will then be able to see in GA4 which articles users scroll further down. This does not confirm that they actually read the text.

Example 2: clicks on buttons only

Goal: track clicks only on CTA buttons, not every click.

  1. Enable the `Click Classes` built-in variable.
  2. Right-click a button on the site, select “Inspect” and find its CSS class, for example `btn-primary`.
  3. Create a new trigger: Click: All Elements, condition `Click Classes contains btn-primary`.
  4. Create a GA4 Event tag with the event name `button_click` and the parameter name `button_text`, using `Click Text` as the value. This also tells you which button was clicked.

Check it in Preview mode: the tag should fire when you click the button, its text or an icon inside it. It should not fire when you click an empty area of the page. This helps confirm that the condition is configured correctly.

👉 Setting up GA4 and Google Ads tags

Conclusion

Every tracking requirement in GTM comes down to answering the same three questions: What is being sent? (tag); When is it sent? (trigger); What additional information is sent with it? (variable).

Make a habit of asking these three questions and you will rely less on ready-made instructions. When a new requirement comes in (“we want to measure users who watch 50% of a product video on YouTube”), you can easily break it down into components: the YouTube Video trigger, the video title and percentage variables, and a GA4 Event tag.

In the next article, we put this knowledge into practice and set up GA4, Google Ads conversion tracking, remarketing and Meta Pixel step by step.

Frequently asked questions

Can one tag work with multiple triggers?

Yes. You can add several triggers to one tag. The tag fires when the conditions for any one of them are met. You can also set an exception trigger: the tag will not fire if that exception’s conditions are met for the same event. This can be used, for example, to exclude internal traffic from measurement.

Why does a built-in variable return an empty value?

First check that the variable is enabled and that a value exists for the selected event. For example, `Click Classes` is populated on click events; it has no value on a page view before a click. In Preview mode, check the Variables section to see which values are available for each event.

Does scroll tracking slow down the site?

It usually has no noticeable effect, but the result depends on the number of tags and the data being sent. Do not set too many percentage thresholds: 25/50/75/90 is enough. Sending an event for every 5 per cent creates unnecessary data and makes reports harder to analyse.

What is the difference between a Custom Event trigger and a Click trigger?

A Click trigger detects a click in the browser. A Custom Event trigger waits for a matching event sent to the data layer. The latter may be more resilient to design changes, but it requires the data layer integration to be configured and maintained correctly.

What should I do if a tag fires multiple times?

First, identify the cause. There are two typical cases. First, several triggers are attached to the tag and fire on separate events, for example, both Page View and DOM Ready on the same page. Second, the trigger type itself is based on a recurring event.

Check the setting in the tag’s Advanced Settings: Tag Firing Options “Once per page” ensures the tag fires only once until the page is reloaded. Use this option only if you will not miss valid conversions that happen more than once on that page.

Should I use “contains” or “equals” in a trigger condition?

It depends on what you need: contains is for a partial match, while equals is for an exact match. Additional parameters in a Page URL can cause an `equals` condition to fail; Page Path does not include query parameters. `contains` may also match similar paths you did not intend to include. Account for language prefixes and a trailing `/` separately.

How many variables is it reasonable to create?

There is no fixed number, but the rule is simple: make sure every variable is used in at least one tag, trigger or another variable. Remove unused variables during your quarterly audit.

Topic: Google Tag Manager: posts in this topic →

Tural Rəhimov

About the author

Tural Rəhimov

Digital Marketing Manager: UM Azerbaijan

Digital marketing manager at Universal McCann (UM Azerbaijan). Experienced in Google Ads, Meta Ads, TikTok Ads and media planning. I help brands grow online.

More about me →

Need digital marketing support for your business?

Let's talk: I can help with everything from strategy to ad management.

Message me