Skip to content
Faisal Mahmud TusherDigital marketing, SEO & analytics
with Faisal Tusher

Marketing Analytics › Google Tag Manager

Tags, Triggers and Variables

How tags, triggers and variables work together in GTM: trigger types and exceptions, firing order, templates versus custom HTML, and maintainable patterns.
Guidebeginner10 min readUpdated

The three building blocks

BlockThe question it answersWhere its input comes fromExamples
TagWhat should run, and where should the data go?Its template or script, with fields filled from variablesGA4 event tag, Google Ads conversion tag, custom HTML for a vendor pixel
TriggerWhen should it run, and when must it not?The event the container is processing, filtered by conditions on variablesPage view on all pages; custom event generate_lead; a click on a link matching a pattern
VariableWhat value goes in this field or this condition?Built-in page and click data, the data layer, cookies, the DOM, lookup tables, your own JavaScriptPage path, click URL, the form's ID, a data layer value such as transaction_id

The container processes the page as a sequence of events. Some are built in and fire on every page in a fixed order: the container script loading, the document becoming ready, the window finishing loading. Others come from the site through the data layer, which the data layer guide covers, or from the container listening to the page for clicks, form submissions, scrolling and visibility. For every event, every trigger is evaluated. A tag fires when at least one of its firing triggers matches and none of its exception triggers does. As it fires, the fields in its configuration are filled by resolving the variables they reference, at that instant, so a variable's value is whatever the page and the data layer hold when the trigger matched. Variables are referenced by name inside double curly braces in any tag or trigger field, which is what lets one tag serve many events: its fields describe how to find a value, not the value itself.

Variables: built-in, data layer, lookup, custom JavaScript

Built-in variables are provided by the container and switched on individually in the variables pane. They cover the page (URL, hostname, path, referrer), the current event's name, the element that was clicked or the form that was submitted (its classes, ID, URL, text), the history change, and container metadata such as the container ID, whether preview mode is running and which environment is active. Enable only the ones a trigger or tag uses; a long list of enabled built-ins is noise for the next person.

User-defined variables are the ones you create, and they come in types. The most important is the data layer variable: it names a key, reads it from the container's data layer model, and can reach nested values with a dot-separated path. It has a default value for when the key is absent and two reading modes, of which the newer, default one merges nested objects across pushes so that page-level context pushed early is still readable later. A constant holds one fixed string, which is where a measurement ID belongs so it is defined once. A lookup table takes an input variable and returns an output for each matching value, with a default; a regex table does the same with pattern matching. URL variables return one component of the current address. First-party cookie and JavaScript variables read a named cookie or a global on the page. DOM element and element visibility variables read the page itself. Finally, a custom JavaScript variable is a function you write that returns a value, which can do anything and is therefore the last resort rather than the first.

A custom JavaScript variable that normalises the page path, trimming a trailing slash so that two spellings of one URL report as one, looks like this:

function () {
  var path = document.location.pathname;
  return path.replace(/\/$/, "") || "/";
}

The preference order is simple. Read from the data layer when the site can provide the value, because that is a contract the site keeps. Use built-ins for what the browser already knows. Reach for a lookup table before you write code. Write custom JavaScript only for what nothing else can do, and expect a reviewer to read it. Values scraped from the DOM break whenever a designer renames a class, which is precisely why the data layer exists.

Triggers: types, conditions, exceptions

Trigger types are grouped by what they listen for.

FamilyTypesFire onTypical use
Page viewConsent initialisation, initialisation, page view, DOM ready, window loadedThe built-in events during page load, in that orderConsent defaults first; the Google tag at initialisation; tags that need the DOM at DOM ready
ClickAll elements, just linksA click anywhere, or on a linkOutbound links, buttons that do not push to the data layer, downloads
User engagementElement visibility, form submission, scroll depth, YouTube videoThe container's own listeners on the pagePromotional banner seen; a form sent; a long article read; video engagement
OtherCustom event, history change, JavaScript error, timer, trigger groupData layer events, URL changes without a reload, errors, elapsed time, a set of triggers all having firedEvery event the site pushes; single-page app views; combining conditions across events

Two of these do most of the work in a well-built container. The custom event trigger matches the event key of a data layer push and is how every planned event reaches its tag; with pattern matching enabled, one trigger can cover a list of event names. The page view family controls what happens during load, and the order within it matters: consent initialisation fires before anything else, which is where a consent platform sets its defaults, and initialisation fires next, which is where Google recommends placing the Google tag so it runs before any event tag.

Conditions narrow a trigger. A trigger can fire on all events of its type or only when conditions on variables hold: page path matching a pattern, click URL containing a string, a data layer value equal to something. The form submission and click triggers also offer a wait-for-tags option that delays the page's own action briefly so that tags have time to send, and a validation option that only fires when the browser would actually submit the form; both are worth understanding because a form trigger that fires on every attempt, including failed validation, inflates the conversion rate (CVR) the business reads. The form tracking tutorial replaces that trigger with a push the site sends once the server has accepted the submission.

Exceptions are the other half. A tag can carry blocking triggers as well as firing triggers; if a blocking trigger matches the same event, the tag does not fire. Because an exception is just a trigger, one rule can be written once and reused across many tags: a trigger for internal traffic, a trigger for a staging hostname, a trigger for a consent state. Each tag also has a firing option that decides whether it may fire once per event, once per page or without limit, which is the simplest guard against double firing when two triggers overlap.

Tags: templates vs custom HTML, tag sequencing

A tag's type decides what it can do and how safe it is. Built-in templates cover Google's own products: the Google tag, GA4 events, Google Ads conversions and remarketing, Floodlight, and a few more. The community template gallery adds templates written by vendors and practitioners; a gallery template runs in a sandbox, declares the permissions it needs, such as reading cookies or sending requests to named domains, and shows those permissions before you add it. Custom HTML tags accept arbitrary script, with an option to let it use document.write; they can do anything, which means they must be read by someone who can judge what they do. Custom image tags request a single pixel URL and are the safe choice for a vendor that only needs a beacon.

Every tag has advanced settings that shape how it runs. Tag firing priority orders tags that share a trigger, higher first, but it does not wait for one to finish before starting the next, so it is not a dependency mechanism. Tag sequencing is: a tag can name a setup tag to fire immediately before it and a cleanup tag to fire immediately after, with options to skip the main tag if the setup tag fails and to skip the cleanup if the main tag fails. Sequencing is how a container guarantees that a configuration or consent-related tag has run before a dependent one. Consent settings, covered in the consent mode guide, sit in the same panel. The GA4 through GTM tutorial shows the Google tag and GA4 event tag configured end to end, which is the most common use of all of this. Tags, triggers and patterns drafted by an AI tool need the same reading before they reach a workspace, and the tutorial on checking AI-written tracking code sets out that review against the tracking plan and both consent states.

Patterns: one tag many triggers, lookup tables, trigger groups

Composition is what separates a container that can be maintained from one that cannot. The patterns below recur in almost every well-run container.

One tag, many triggers; one trigger, many tags. A trigger is reusable and a tag can carry several, so the same "all pages" trigger serves every page-level tag, and the same custom event trigger for purchase serves the GA4 event tag and the ad platform conversion tags alike. Resist creating a near-duplicate trigger for each new tag.

The generic event tag. One GA4 event tag whose event name field reads the built-in event variable, attached to a custom event trigger that matches every planned event name by pattern, replaces a dozen one-event tags. Parameters come from an event settings variable or from data layer variables with the same names as the planned keys. The trade-off is visibility: the container shows one tag where a reviewer might expect many, so document the pattern where the next person will see it.

Lookup tables for mapping. A lookup table turns a page path into a content group, an event name into a Google Ads conversion label, or a hostname into a measurement ID. The mapping lives in one place and the tags read it, so adding a case means editing a table rather than cloning a tag.

Constants for IDs. Measurement IDs, conversion IDs and vendor account numbers belong in constant variables referenced by the tags, never typed into each tag by hand.

Trigger groups for combined conditions. A trigger group fires once all of its member triggers have fired at least once on the page, which is how "scrolled most of the article and stayed for a while" becomes one engagement event without custom code. The scroll half of that rule is a scroll depth trigger set at the threshold the team has chosen.

Exceptions for environments and internal traffic. One blocking trigger each, attached to every tag that sends data outward, so staging and office traffic never pollute reports.

Illustrative example — not a real result

A lead-generation site's trigger set and lookup table

A commercial insurance broker has 14 quote forms across 9 landing pages, each built from one template. Instead of 14 form triggers and 14 GA4 tags, the analyst uses a single custom event trigger on generate_lead, which the site pushes after the server confirms the submission, and one GA4 event tag. A lookup table maps the page path to a product line (fleet, liability, property and so on), with a default of "other", and that output is sent as a product_line parameter. A second lookup table maps the same input to the matching Google Ads conversion label, so one conversion tag covers all 14 forms. The office's static IP range feeds an exception trigger attached to both tags. In the first fortnight the GA4 count for generate_lead is 312 against 318 confirmed submissions in the CRM, and the 6 missing are traced to a product line added to the template without a row in the lookup table, which the default value had quietly labelled "other".

Common mistakes

  • One trigger per tag, always. Dozens of near-identical triggers nobody dares delete. Reuse triggers and name them by what they match, not by the tag they were made for.
  • Scraping the DOM instead of asking for a push. The trigger worked until the button's class changed. Anything the business depends on belongs in the data layer.
  • Custom HTML for a vendor that has a template. Unreviewed script in the container when a sandboxed template exists. Check the gallery first.
  • Priority as a dependency. Setting a higher priority on the configuration tag and assuming the event tag will wait. Use sequencing, or the initialisation trigger, when order matters.
  • Form triggers that fire on attempts. Validation errors counted as leads. Push a confirmed event from the site, or at least enable validation on the trigger.
  • Enabled built-ins and variables nobody uses. The variables pane becomes a graveyard. Prune on every review, as the containers guide suggests for versions.
  • Testing the change list instead of the page. A trigger with a typo fires nowhere. Prove every tag in preview, following the debugging tutorial, before the version goes live. The Tag Manager hub lists the rest of this section.

Frequently asked questions

What is the difference between a trigger exception and a trigger condition?

A condition narrows when a single trigger matches: a page view trigger with a condition on the page path only fires on those paths. An exception is a second trigger attached to the tag as a blocker; if it matches the same event, the tag does not fire even though its firing trigger did. Conditions shape one trigger, exceptions let one blocking rule, such as internal traffic or a consent state, be reused across many tags.

Should I use a Custom HTML tag or a template?

Use the built-in or gallery template whenever one exists for the vendor. Templates run in a sandbox, declare the permissions they need and are reviewed before they reach the gallery, so they are both safer and easier to audit than arbitrary script. Custom HTML is for the rare vendor without a template, and every one of those tags should be reviewed by someone who can read the code it contains.

Why does my tag fire but send empty values?

Almost always because the variable it reads is resolved at the moment the tag fires, and the value was not there yet. A data layer variable reads whatever has been pushed when the trigger's event is processed, so a push that arrives after the trigger has already matched is too late. Preview mode's variables view, opened on the exact event that fired the tag, shows what each variable held at that instant.

Terms used on this page

Where this page is used

Part of these learning paths

Also related

Sources

  1. Tag Manager overview — Google Tag Manager Help
  2. Google Tag Manager Help — Google Tag Manager Help

Related