Using the new version of Google Tag Manager? Read the updated version of this article.
In web containers, the rule(s) attached to a tag govern when the tag is fired or not fired. In mobile app containers, certain macro types have rules associated with them that allow the mobile app to be configured conditionally based upon the runtime environment.
In web containers, a tag must have at least one rule in order to fire. Rules are evaluated during runtime and associated tags are fired (or not fired) when the rule conditions are met. For example, a tag with the following “Fire when” rule will be fired on the example.com home page:
- url equals example.com/index.html
A rule is composed of one or more conditions; each condition takes the form: [Macro] [Operator] [Value]
|Choose between url, referrer, event, etc||Choose between equals, contains, etc||You provide a value. Google Tag Manager compares the value you provide with the runtime value in the macro.|
In our example, the macro “url” is evaluated during runtime. The macro “url” has been defined such that it contains the current page URL . Thus, during runtime, the current page URL is compared to the value you’ve provided, “example.com/index.html”, to determine whether the rule condition has been met.
In web containers, three macros are pre-defined: url, referrer, and event. With these three macros, you’re able to construct the rules you’ll most commonly need. However, you can also define your own macros; read Macros to learn more.
You can provide multiple conditions ([Macro] [Operator] [Value]) in a single rule. The rule is met if all of the conditions in the rule are met.
Firing/Blocking Rules for Tags
A tag will fire whenever any one of its firing rules is met. For example, if a tag has two firing rules, one for "all pages" and another for "only page ABC", the tag will fire on all pages.
A rule that blocks a tag from firing always overrides a firing rule. For example, if a tag has a firing rule "all pages" and a blocking rule "URL equals thankyou.html", the tag will never fire on the page "thankyou.html".
Enabling/Disabling Rules for Macros
In mobile app containers, some macro types have rules associated with them that allow the mobile app to be configured conditionally based upon the runtime environment. A macro is available whenever any one of its enabling rules is met. For example, if a macro has two enabling rules, "Always", and "platform equals Android", the macro is always available. A disabling rule always overrides an enabling rule.
By default, tags fire once the gtm.js script returns to the browser. However, it's possible to define a tag such that it fires once the DOM is ready. For example, let's say that your tag needs to access the Trip ID (via a DOM element macro type) and the Trip ID value doesn't render until the bottom of the page. By default, the tag might fire before it's able to see the value of Trip ID in the page.
To define a tag such that it fires when the DOM is ready, add a "event equals gtm.dom" condition to the firing rule. So, your firing rule would have at least two conditions:
- url contains thankyou.html
- event equals gtm.dom
To define a tag such that it fires on window.onload, use a "event equals gtm.load" condition.