Google Tag Manager is a JavaScript runtime on your page that people outside your deploy process can program from a web interface, with no code change. If its snippet is on your site, you should know what it can run and what that costs you in speed, security and money. This is part 1 of a three-part series, and part 2 builds on it with Set up Google Analytics 4 with Tag Manager to track signups and sales.
A vendor tag on the pricing table, added without a deploy
Suppose a marketer at your company wants a heatmap vendor’s script to load when visitors reach the pricing table. Your page already has the GTM snippet and a <section id="pricing">. The marketer, the vendor and its script URL below are invented.
The marketer opens the GTM web interface and adds three things. A trigger is a rule that says when something runs. A variable is a named value GTM reads from the page, and a tag is the code that runs.
Trigger "Pricing visible"
type: Element Visibility
selector: #pricing (ID)
minimum percent visible: 50
Variable "Page path"
type: URL, component: Path
Tag "Heatmap vendor"
type: Custom HTML
html: <script src="https://cdn.heatmap-vendor.example/h.js"
data-page="{{Page path}}"></script>
fires on: Pricing visible
Then the marketer clicks Publish. The set of tags, triggers and variables that your snippet loads is a container, and publishing makes this new version of it live for every visitor. Nobody opened your repository, and nothing went through your code review or your CI.
On the next page load, GTM sees a sequence of messages like this one. The first three come from GTM itself on every page; the last one comes from the Element Visibility trigger.
1. event: gtm.js Consent Initialization, Initialization, then Page View triggers
2. event: gtm.dom DOM Ready triggers
3. event: gtm.load Window Loaded triggers
4. #pricing reaches 50% visible
"Pricing visible" fires, so "Heatmap vendor" runs
GTM evaluates {{Page path}} when the tag fires, writes the HTML into the page, and the browser loads the vendor’s script.
These messages travel through one array on the page, dataLayer. You can push your own messages into it. Open DevTools on a page that loads GTM and suppose its container has a Custom Event trigger for pricing_viewed and a tag that reads plan. Predict which of these two pushes fires the tag, and what value of plan it sees.
// Run in the DevTools console on a page that loads GTM.
dataLayer.push({ plan: 'pro' });
dataLayer.push({ event: 'pricing_viewed' });
The first push fires nothing. A push without an event key only sets values, so it stores plan. The second push carries an event, the trigger matches it, and the tag fires once with plan set to 'pro' from the earlier push (the data layer).
Rule: In GTM, only a message with an
eventkey can fire a tag; every other push only stores values for later.
How Google Tag Manager decides what runs
This section follows one message through GTM, then looks at what else a container can hold, and where it can run besides the browser.
One queue of dataLayer messages
The snippet you paste into the page pushes {event: 'gtm.js', 'gtm.start': …} and loads the container. From then on, GTM handles dataLayer messages first in, first out. For each message, it fires every tag whose trigger matches before it moves to the next message (the data layer).
On a normal page load, the built-in triggers fire in a fixed order: Consent Initialization, Initialization, Page View (gtm.js), DOM Ready (gtm.dom) and Window Loaded (gtm.load) (trigger types). So a tag set to Window Loaded waits until the page and its resources have loaded.
Two settings change the order inside one message. A tag’s priority defaults to 0, and a higher number fires first, but tags still fire asynchronously, so priority does not mean one tag waits for another (tag firing priority). When one tag must finish first, tag sequencing names a setup tag and a cleanup tag. It can also skip the main tag if the setup tag fails (tag sequencing).
Each tag also has a firing option: Unlimited, Once per event or Once per page (tag firing options).
What GTM triggers listen to
Most triggers listen to the browser, not to your code. GTM adds the listeners, turns what they hear into dataLayer messages, and runs them through the same queue. The trigger index lists them all; this table shows the ones that matter most for what can run on your page.
| Trigger | Fires when | Limit worth knowing |
|---|---|---|
| Click | A visitor clicks any element, or only links | Wait for Tags and Check Validation work only for Just Links |
| Element Visibility | An element matched by ID or CSS selector is on screen | Default is 50% visible; matching many elements hurts performance |
| Form Submission | A form is submitted | Has Wait for Tags and Check Validation |
| Scroll Depth | The page scrolls past a percent or pixel threshold | Each threshold fires once per page |
| YouTube Video | An embedded video starts, completes, pauses, seeks, buffers or reaches a progress point | Adding enablejsapi=1 reloads a video that is playing |
| History Change | The URL fragment changes or the page calls pushState |
Since 2026-11-09, page views from it also record back/forward cache restores (release notes) |
| JavaScript Error | window.onerror sees an uncaught error |
Misses errors thrown before GTM loads |
| Timer | An interval passes, pushing gtm.timer |
Stops at the limit you set |
| Trigger Group | Every selected trigger has fired at least once | Fires once all of them have fired |
| Custom Event | A message’s event matches a name or regex |
A broad regex also matches GTM’s own events |
The Element Visibility trigger also has an on-screen duration and an “Observe DOM changes” option, for content that appears after load (Element Visibility trigger).
Variables are read again on every push
A variable can read almost anything on the page. GTM’s built-in and user-defined types include the URL, the referrer, a first-party cookie, a value in dataLayer, a JavaScript global, a DOM element, and a Custom JavaScript function (variable types). Lookup Table and RegEx Table variables map one value to another, and in a RegEx Table the first match wins.
Since 2025-12-11 there are also built-in Client ID, Session ID and Session Number variables, and an Analytics Storage variable (release notes).
GTM evaluates variables again on every dataLayer push (web.dev, tag best practices, 2021). So a Custom JavaScript variable is a function that runs many times per page, not a value computed once.
Tags: built-in types, gallery templates and Custom HTML
GTM’s supported tags page lists about 54 web tag types. Besides Google Ads and Floodlight, they include vendors such as AB Tasty, AdRoll, Criteo, LinkedIn, Pinterest, Hotjar and Microsoft Ads. The Community Template Gallery adds more tag types written by vendors and other developers.
For anything else, there are the Custom HTML and Custom Image tags. A Custom HTML tag is any HTML and JavaScript that GTM writes into your page, as the heatmap tag in the example did (custom tags). On the same page, Google advises using the built-in tag templates to avoid performance and malware problems.
Custom templates run in a sandbox with declared permissions
A custom template is a tag or variable type written in sandboxed JavaScript, which is what the gallery’s types are. The sandbox is based on ES5.1, has no window, new or this, and gives the code its APIs only through require (sandboxed JavaScript).
// Excerpt: the code of a custom tag template.
const injectScript = require('injectScript');
injectScript('https://cdn.heatmap-vendor.example/h.js',
data.gtmOnSuccess, data.gtmOnFailure);
Notice that the template cannot add a <script> element by itself. It asks for injectScript, and that API works only if the template declares the inject_script permission (template APIs).
The same holds for other APIs. sendPixel needs send_pixel, setCookie needs set_cookies, callInWindow needs access_globals, and the consent APIs need access_consent. So whoever adds a template to a container can read, before it runs, what it is allowed to do. Templates can also carry tests written with runCode, mock, assertApi and assertThat (template tests).
The gallery is fed from public GitHub repositories that hold a template.tpl, a metadata.yaml and an Apache 2.0 license. Updates reach the gallery within two to three days, and Google’s page describes no security review (gallery docs). So the permissions list is your review.
Workspaces, versions and environments
GTM has its own small deploy pipeline, separate from yours. A workspace is a draft copy of the container that one person or team edits. A standard account has the default workspace plus two custom ones, and GTM 360 has no limit (workspaces).
Publishing a workspace creates a version, a saved state of the container that you can roll back to (versions). An environment is a named target such as staging, with its own snippet, so you can load a version on a staging site before you publish it to everyone (environments).
Container permissions have five levels: No access, Read, Edit, Approve and Publish (user permissions). You can export a container as JSON and import it with overwrite or merge (import and export). The Tag Manager API v2 covers accounts, containers, workspaces, tags, triggers, variables, versions (create and publish), environments and user permissions, so a script or CI job can publish too.
Server-side tagging moves the container to your server
Server-side tagging runs a second container on a server you control, between the browser and the vendors. The browser sends its data to that server instead of to each vendor, and the server container decides what to forward (server-side tagging intro).
browser server container vendors
Google tag ── HTTP request ──> clients (by priority)
(server_container_url) first client that recognizes it claims it
└─> events ─> triggers ─> tags ──────> vendor endpoints
<── response sent by the claiming client
A client is the part of a server container that receives a request. Clients run by priority, and the first one that recognizes the request claims it and turns it into events. Then triggers, variables and tags run on those events as they do in the browser, and the client sends the response. Server tags are sandboxed JavaScript too (intro).
Server tags get APIs that a browser tag never has, such as sendHttpRequest, Firestore.read and Firestore.write, BigQuery.insert, setCookie, getRemoteAddress and sha256 (server-side APIs). So a tag can enrich an event with data from Firestore or BigQuery before it forwards it. A transformation allows, augments or excludes event parameters per tag or tag type, which is how you strip fields a vendor should not get (transformations).
The server is a Node.js application in a Docker image, gcr.io/cloud-tagging-10302018/gtm-cloud-image:stable, which you can run on any platform (manual setup). Image 4.0.0 moved to Node 24 on 2025-11-10, and the latest image is 4.7.0, from 2026-11-17 (release notes).
What Google Tag Manager costs you
Every feature above has a price, in risk, speed, money or consent work.
Anyone with Publish can run any script on your site
Publish permission lets a person make a version live (user permissions), and a Custom HTML tag can hold any script (custom tags). Put those two pages together, and anyone with Publish can run any JavaScript on every page that loads the container. Google does not state it in those words; it is our reading of the two pages.
That script runs with your origin’s access to the DOM, cookies that JavaScript can read, and form fields. So the list of people with Publish, and the security of their Google accounts, belongs in your threat model next to your deploy keys.
A Content Security Policy does not fix this by itself. Google’s CSP guide gives a nonce-aware snippet, but Custom JavaScript variables evaluate to undefined unless your policy allows 'unsafe-eval' (CSP guide). So you choose between a stricter policy and those variables.
Tags through GTM load later and use your main thread
web.dev’s 2021 figures put the GTM library at about 33 KB compressed and the median container at about 50 KB (web.dev, tag best practices, 2021). The same article says tags loaded through GTM typically load later than tags in the HTML. It also reports that the size of a site’s tag manager correlates with worse INP. That is a correlation across sites, not a measurement of your page.
The same article gives a 300 KB container cap with a warning at 70%. Current Google help shows only the 70% indicator, so treat 300 KB as web.dev’s 2021 figure (container size).
Every Custom HTML tag and Custom JavaScript variable runs on the main thread, between your own code’s tasks. Because variables run again on every push, a variable that reads layout can force it many times per page. Why reading offsetHeight can freeze your page shows what that does to a page.
Approvals and zones cost a GTM 360 contract
The standard account has Approve as a permission level, but approval requests for versions exist only in GTM 360 (versions). Zones are also GTM 360 only. A zone links other containers to yours and restricts which tag and variable types they may run (zones). On a standard account, the review step before a version goes live is whatever your team agrees to do by hand.
Server-side tagging costs a server bill and still needs a browser tag
Google recommends Cloud Run, and its guide says each server there costs about $45 per month (USD). A server has 1 vCPU, 0.5 GB of memory and CPU always allocated (Cloud Run setup). The minimum is 2 instances, so about $90 a month before traffic, and 2 to 10 servers handle 35 to 350 requests per second.
Preview mode needs a second deployment (preview server). App Engine is still documented at about $40 per server with a minimum of 3, but Google recommends Cloud Run (App Engine setup).
Server-side tagging does not remove the browser side. The browser still runs a Google tag with server_container_url set, and your CSP must allow the container URL in img-src, connect-src and frame-src (send data). So you now run two containers.
You may have read that a server container gets around ad blockers. We found no primary source for or against that claim, so this article does not make it.
Server-side tagging does not make tracking consent-free
The server container has no banner of its own. The consent state passes from the web container to the server, so the browser still needs your consent setup (server-side consent mode). In the web container, a Consent Initialization trigger runs first by default, and each tag can check consent (consent in GTM).
Some vendor tags still send data without consent. Since 2025-07-28, server-side Floodlight sends unconsented requests server to server, for modeling (release notes). Your privacy page has to describe what that tag does.
Alternatives to GTM, and what changed in 2025 and 2026
When hard-coded tags or the Google tag alone are the better choice
GTM pays off when people who don’t ship code need to add or change tags often. If tags change twice a year, or only developers change them, that benefit is small. The costs above stay the same.
The first alternative is to hard-code each vendor’s script in your templates. Then every change goes through your code review, your CSP and your deploy, and you see the script’s URL in a diff. The cost is that every marketing change waits for a release.
The second alternative is the Google tag alone, if you only send data to Google products. Since 2026-11-17 every Google tag is a Tag Manager container (announcement), so the line between the two has moved.
The third choice is GTM locked down. You keep GTM’s workflow, but your page limits which tag types the container may run, as the last section shows. The cost is a list that a developer must update when the marketing team needs a new tag type.
What changed, with dates
These dates come from the GTM release notes, except where another source is linked.
- 2025-05-08: First-party mode was renamed Google tag gateway for advertisers, which serves Google tags from your own CDN, load balancer or web server. Cloudflare support came in 2024; Akamai, Fastly, Google Cloud (generally available 2026-06-01) and CloudFront (2026-06-03) followed. Google recommends the gateway together with server-side tagging (gateway docs).
- 2026-10-08: All snippets initialize on load.
- 2026-10-12: Destination tags let a container send data to Google products directly, without loading gtag.js again (destination tags).
- 2026-11-17: Every Google tag became a Tag Manager container, as announced on 2026-08-20 (announcement).
One rule outside Google matters for server-side setups. Safari’s tracking prevention caps cookies set in HTTP responses at 7 days when they come from a third-party CNAME or an IP-cloaked subdomain (WebKit tracking prevention, 2023-04-27). Cookies set by JavaScript are deleted after 7 days without interaction.
So where you put the server container matters. Google calls the same origin under a path such as /metrics best practice, and a subdomain also works. A *.run.app address can only set JavaScript cookies (custom domain).
A safer GTM setup
If you keep GTM, these five steps limit what it can run and what it costs.
- Roles. Give Publish to as few people as you can, and Read or Edit to everyone else (user permissions). Use an environment for staging before a version goes live.
- Allowlist, blocklist or policies. Your page can limit the container from outside it, as shown below.
- Templates over Custom HTML. Prefer a built-in or gallery template, and read its permissions before you add it.
- CSP nonce. Use the nonce-aware snippet from the CSP guide, and decide whether you need Custom JavaScript variables at all.
- Window Loaded for non-essential tags. Fire tags that nothing on the page depends on after Window Loaded, as web.dev advises (tag best practices).
For step 2, gtm.allowlist and gtm.blocklist in dataLayer override what the container contains, and when both match, the blocklist wins. Their classes include customScripts, html, nonGoogleScripts and sandboxedScripts (restrict tag deployment).
<!-- Excerpt: push before any tag fires, so before the GTM snippet. -->
<script>
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
'gtm.blocklist': ['html', 'customScripts']
});
</script>
This push tells GTM to refuse anything that runs JavaScript a user wrote: customScripts covers that code, and html is its alias for the Custom HTML tag. It holds whatever someone publishes later. But any blocklist also blocks sandboxed templates, including the gallery templates that step 3 recommends (restrict tag deployment).
To get them back, you add sandboxedScripts to gtm.allowlist. Once an allowlist exists, only the types on it run, so you must list every tag and variable type the container uses. That bookkeeping is one reason Google prefers policies.
Google recommends template policies over the allowlist and blocklist. A policy is a function your page registers for one permission, and when it returns false, GTM rejects that call (policies).
<!-- Excerpt: place before the GTM snippet. -->
<script>
window.dataLayer = window.dataLayer || [];
function gtag() { dataLayer.push(arguments); }
gtag('policy', 'inject_script', function (containerId, permissionId, data) {
return data.url.startsWith('https://cdn.heatmap-vendor.example/');
});
</script>
Notice the policy checks the permission, not the tag type, so it also covers templates added to the container later. The cost is the same as the blocklist: a developer must change the page when marketing needs a new script host.
Next, build a real container with part 2, Set up Google Analytics 4 with Tag Manager to track signups and sales.
For further reading, these are the primary sources:
- The data layer, the trigger index and variable types
- Sandboxed JavaScript, template APIs and template policies
- Restrict tag deployment and the CSP guide
- The server-side tagging docs, starting with the intro and the Cloud Run setup guide
- web.dev, tag best practices (2021)
- The GTM release notes for every dated change above
If you want part 3 and the next articles like this one when they come out, subscribe to the newsletter.
Comments
No comments yet. Ask a question or share what worked for you.