4/11/2026
Are you tightening your site's security and seeing Google Tag Manager "blocked" errors in the console? This page is for marketing, SEO and technical teams who need a Content Security Policy (CSP) for Google Tag Manager to coexist with reliable GA4 measurement.
It helps you choose which directives to open, which strategy to use (allowlist, nonce or hash) and how to sequence the rollout without breaking data collection. Consent and cookies are covered in a dedicated guide.
How to Configure a Content Security Policy (CSP) for Google Tag Manager Without Breaking Tracking or Web Security
A CSP forces the browser to execute and load only explicitly authorised resources: it therefore blocks injected inline JavaScript and scripts from unlisted domains, both typical of an XSS attack. For the basics, see our Google Tag Manager guide; for the container's scripts, read the article on GTM scripts and tags.
The conflict stems from how GTM is designed: a loader (gtm.js) then injects other scripts. Under a strict policy, the browser refuses to load them ("Refused to load the script because it violates the Content Security Policy") if the sources are not allowed or if inline code is not handled via a nonce or hash.
Three decisions shape the work:
- The directives: which ones to open, and for which domains are actually called.
- The inline strategy: domain allowlist, nonce with
'strict-dynamic', or hash. - The rollout sequence: observation in Report-Only, then a switch to enforcement mode.
Understanding the Conflict Between CSP and GTM: Mechanisms, Risks and Attack Surface
Before writing a single rule, know what the CSP protects and what GTM loads.
Definition: Content Security Policy (CSP), Goals, Limitations and the Browser's Role
A CSP is a browser-enforced security layer, defined via the Content-Security-Policy HTTP header (less commonly via a <meta http-equiv="Content-Security-Policy"> tag). It restricts which origins are permitted for scripts, images, styles, connections and iframes. It does not fix vulnerabilities: it limits the impact of a successful injection.
How the Google Tag Manager Script Loads and What Your Container Actually Calls
The implementation starts with an inline snippet that initialises the dataLayer and loads gtm.js from a Google domain. Once loaded, GTM can:
- inject additional scripts (measurement, advertising) based on the published tags;
- trigger network requests to collection endpoints;
- load pixels (images) or iframes, notably for preview and debugging.
This combination of inline code, remote loading and dynamic injection causes the violations. So map what your container consumes:
- Scripts: the loader (
www.googletagmanager.com), then the domains for each triggered tag. - Network connections: collection endpoints (
*.googletagmanager.com,www.google-analytics.com,*.analytics.google.com). - Images: pixels and beacons from GTM and GA4 subdomains, or from other vendors.
Allowing GTM does not cover the third-party tags it loads: every new tag may require an update to the policy.
Preparing Before You Tighten CSP: Deployment Settings and a Safe Iteration Loop
Hardening happens in short iterations: observe, fix, then block.
Start in Report-Only and Read the Violation Reports
Start with Content-Security-Policy-Report-Only: the browser reports violations without blocking them. Once the directives are stable, switch to Content-Security-Policy. Three sources guide the diagnosis:
- the browser console, with "Refused to load…" messages naming the relevant directive;
- CSP reports, if a reporting endpoint aggregates errors across all traffic;
- the error line itself, which names
script-src,connect-src,img-srcorframe-src.
Pre-Production Checklist: Environments, Preview Mode and Debug Impact
Before touching the header, stabilise the container, then validate in staging:
- Inventory active tags, custom HTML tags and custom JavaScript variables.
- Test on a staging environment that reproduces exactly the final headers served in production, CSP included.
- Check Preview mode (Tag Assistant): missing panel, missing styles, CSP errors in the console.
- Check critical user journeys (landing page, form, checkout), where measurement is most sensitive.
Minimum CSP Rules for Tag Manager to Work: Directives and Trade-Offs
The table below summarises what each directive must allow for GTM and the risk of opening it too wide.
| Directive | What it allows for GTM, GA4, Ads, preview | Risk if too broad |
|---|---|---|
script-src | gtm.js loader, nonced inline snippet, tag scripts | 'unsafe-inline' cancels XSS protection |
connect-src | GA4, GTM, Google Ads and DoubleClick requests | A * wildcard lets data leave to anywhere |
img-src | GA4 and Ads pixels and beacons, preview images | data: or a wildcard widens the surface |
frame-src | Google iframes, including the container's <noscript> | Third-party content framed without control |
font-src | Preview fonts (fonts.gstatic.com) | Low, but unnecessary domains in production |
style-src | Preview panel styles (fonts.googleapis.com) | Permanent 'unsafe-inline' allows style injection |
script-src: Allow the Loader, Control Inline Script Policy and Avoid Unnecessary Openings
For GTM, script-src is the most critical directive: it covers the loader and inline code. A minimum such as script-src 'nonce-{nonce}'; is then extended with the domains actually required; adding a domain is not enough if the inline snippet is not authorised.
When allowing GTM requires a nonce. The most robust setup combines a nonce (a random value per HTTP response) with 'strict-dynamic': script-src 'nonce-{nonce}' 'strict-dynamic';. Scripts loaded by an approved script inherit its trust, with no list of hashes to maintain.
unsafe-inline and unsafe-eval. These two options, to be added only as a last resort, significantly relax the policy:
'unsafe-inline'permits all inline JavaScript, which is precisely what a CSP aims to prevent.'unsafe-eval'permits evaluating strings (eval(),new Function); GTM requires it as soon as you use custom JavaScript variables.- Custom HTML tags do not fall under
'unsafe-eval': they are handled with the nonce.
connect-src and img-src: Network Calls, Pixels and Beacons Without Over-Allowlisting
connect-src governs JavaScript network requests (fetch, XHR, beacons); img-src covers pixels, which are sent as image requests. The domains to allow depend on the published tags:
- GTM:
*.googletagmanager.com, inconnect-srcandimg-src. - GA4:
*.google-analytics.comand*.analytics.google.com, in both directives. - Google Ads: the Ads and DoubleClick domains reported by the console, in both directives.
Only broaden the policy after observing the actual calls in Report-Only. The data: scheme in img-src is sometimes requested, but it widens the surface: decide based on the violations you observe.
frame-src, font-src and style-src: The Preview Domains
If production works but the preview panel stays empty or unstyled, the Tag Assistant domains are almost always missing. At the time of writing:
fonts.googleapis.com, instyle-src;fonts.gstatic.com, infont-src;www.gstatic.com, inimg-src.
The panel also relies on inline styles: where possible, restrict 'unsafe-inline' in style-src to debug environments. frame-src must cover the Google iframes your flows use.
Three Ways to Allow GTM With a Strict CSP: Domains, Nonce and Hash
The choice depends on how often your container changes and whether you can generate a value server-side:
| Approach | When to choose it | Limitation |
|---|---|---|
| Domain allowlist | Quick start, stable container | Brittle as soon as new tags appear |
Nonce + 'strict-dynamic' | Evolving container, server-side rendering | Nonce to generate for every HTTP response |
| Hash | Fixed inline snippet, static pages | Must be recalculated after the slightest change |
| Report-Only | Before every hardening step | Reports without protecting |
Approach 1: Start Quickly With an Allowlist of Domains
The simplest strategy allows the GTM domains, for example script-src 'self' https://www.googletagmanager.com;, extended with *.googletagmanager.com where needed. It is quick, but it becomes brittle if the container changes often.
Approach 2: A Server-Side Nonce for a Strict Policy With Dynamic Loading
A nonce is the most sustainable option against dynamic injection. Three steps:
- generate a nonce server-side, one per page load;
- declare it in the header (
script-src 'nonce-…'); - add the
nonce="…"attribute to the inline GTM snippet.
Google provides a nonce-aware version of the snippet, which passes the nonce on to the gtm.js script it loads. The value must be identical in the header and in the script tag.
For custom HTML tags, pass the nonce through (for example, via an application-exposed variable) and only permit nonced scripts, rather than opening 'unsafe-inline'. Avoid workarounds that make GTM function by weakening security.
Approach 3: A Hash for a Fixed Inline Snippet (and When It Breaks)
A hash allows one precise inline payload via sha256-… (or sha384, sha512) inside script-src, with no nonce to generate. But as soon as the snippet changes, even by a single space, the hash must be recalculated and redeployed: more maintenance than a nonce.
CSP Header Examples: Keeping "Allow googletagmanager" Tight Without Over-Permissioning
A readable structure (strict base, necessary exceptions, then hardening) prevents syntax errors. Example, one directive per line, to adapt:
Content-Security-Policy:
default-src 'self';
script-src 'nonce-{nonce}' 'strict-dynamic';
connect-src https://*.googletagmanager.com https://*.google-analytics.com https://*.analytics.google.com;
img-src https://*.googletagmanager.com https://*.google-analytics.com https://www.gstatic.com;
frame-src https://*.google.com;
style-src 'self' 'unsafe-inline' https://fonts.googleapis.com;
font-src https://fonts.gstatic.com;
In the real header, these lines form a single value.
Deploy in One Place and Verify the Header Is Active
Define the CSP in a single place, your source of truth, to avoid duplicate headers and inconsistent rules; version the per-environment variants. Then check that it is being served:
- in the browser, Network tab,
Content-Security-Policyresponse header; - on the command line, with
curl -I https://your-domain.tld/page; - in functional QA, on the conversion journeys where sensitive tags fire.
Fixing a CSP-Related "Blocked" Error Quickly: A Diagnostic Method
Start with the console: the error names the directive and the blocked URL. That is your starting point, not a guess; then allow the minimal origin, in Report-Only first.
Common Cases: Preview Mode, Third-Party Tags, Custom HTML, Redirects and iFrames
Most errors fall into a handful of recurring situations:
| Symptom | Directive involved | Fix |
|---|---|---|
| Preview missing or unstyled | style-src, font-src, img-src | Add the three preview domains |
| GA4 requests missing from DebugView | connect-src | Add *.google-analytics.com and *.analytics.google.com |
| Recently added third-party pixel refused | img-src, connect-src | Allow the exact origin observed |
| Custom JavaScript variable failing | script-src ('unsafe-eval') | Replace the variable or accept the option |
| Custom HTML tag blocked | script-src (inline) | Pass the nonce to the tag |
| Empty Google iframe | frame-src | Allow the relevant Google domain |
Always read the URL the console names, not the one you expected.
When Consent Mode Changes Script Loading Behaviour
Some tags fire only after the user accepts: certain violations appear only in that scenario. Test each case:
- reject all trackers, where only necessary resources load;
- partial accept (measurement only, for example);
- full accept, which loads all published tags.
Hardening Beyond CSP: GTM Governance, Trusted Types and Injection Prevention
GTM centralises the ability to inject code. Without governance (permissions, review, staging, versioning), a tag can force an emergency CSP exception. If every marketing change requires 'unsafe-inline', the problem is as much organisational as it is technical.
A strict CSP demands maintenance discipline:
- regular reviews of tags and publishing permissions;
- documentation of the external resources the container calls, directive by directive;
- systematic testing after every version release, with CSP reporting enabled continuously.
Trusted Types: Reduce Risky DOM Sinks and Complement CSP in Modern Applications
Trusted Types complements CSP by reducing exposure to DOM XSS on certain sinks such as innerHTML. In highly dynamic applications, it adds a barrier alongside script-src, without replacing CSP.
When CSP Exposes a Wider Issue: What to Audit on the Technical Side
When violations multiply, the problem often goes beyond GTM. Typical symptoms:
- two competing GTM implementations (hard-coded snippet and module);
- tags added without documentation, calling unknown domains;
- dependencies loaded differently across templates;
- different headers between staging and production.
Reset the chain rather than stacking exceptions, and harden template by template, high-stakes pages first.
Depending on Your Situation
Depending on where you are, the most useful article differs:
- You are placing the nonced snippet: check its placement in the guide to install Google Tag Manager.
- Your variables require
'unsafe-eval': identify which ones to replace with our guide to GTM variables. - You are publishing a new container version: before switching the CSP to enforcement mode, follow the procedure to test Google Tag Manager on the version to be published.
- Your violations depend on consent: set up the CMP chain in the article on cookies and consent in GTM.
Before and after switching to enforcement mode, an audit with the Incremys 360 GEO and SEO audit module helps you spot, template by template, the pages whose technical health has deteriorated.
FAQ: CSP and Google Tag Manager
Is GTM Compatible With a Strict Content Security Policy?
Yes, provided you handle inline code via a nonce or hash rather than a simple allowlist. A strict CSP works with GTM if you also allow the network calls (connect-src) and pixels (img-src) actually in use. A nonce combined with 'strict-dynamic' is the most robust option for the dynamic loading of tags.
Which CSP Directives Should I Adjust First (script-src, connect-src, img-src)?
Adjust script-src first, as it governs the loading of GTM and its inline snippet. Then handle connect-src for collection requests, followed by img-src for pixels. Finally, complete frame-src, font-src and style-src if Google iframes or the preview are blocked.
What Errors Occur If Allowing Google Tag Manager Remains Incomplete?
The console displays "Refused to load…" messages naming the failing directive. An incomplete script-src blocks the loader, an incomplete connect-src blocks measurement requests, and an incomplete img-src refuses pixels. Allowing www.googletagmanager.com without its subdomains often leaves residual blocks in place.
Which Nonce Should I Use With GTM, and Where Do I Inject It?
Use a random, server-generated nonce that is unique per page load. Inject it in the header (script-src 'nonce-…') and in the nonce attribute of the inline script that initialises GTM. The two values must match exactly, otherwise the browser blocks the snippet.
What Should I Do If Preview Mode Stops Working After Hardening and Shows "Blocked"?
Add the preview domains: fonts.googleapis.com, fonts.gstatic.com and www.gstatic.com. Their absence is the most common cause of this block. Then replay the preview in Report-Only to spot the remaining violations, especially in style-src, which the panel depends on for its styling.
How Can I Avoid unsafe-inline and unsafe-eval While Keeping Tags Functional?
Replace 'unsafe-inline' with a nonce, optionally with 'strict-dynamic', and pass that nonce to custom HTML tags. 'unsafe-eval' is only required by custom JavaScript variables: replacing them with data layer variables or constants often lets you do without it.
Is CSP Enough for XSS Protection, or Should I Enable Trusted Types as Well?
A CSP significantly reduces XSS risk, but it does not replace fixing application-level vulnerabilities. Trusted Types complements it in modern applications by locking down risky DOM sinks. The most coherent defence combines a strict CSP, Trusted Types and sound code hygiene.
.png)
.jpeg)

.jpeg)
%2520-%2520blue.jpeg)
.avif)