How-to

How to Block Scripts Until Cookie Consent

Three ways to stop analytics, ad pixels and embeds from loading before visitors consent: automatic blocking, text/plain script tags and Tag Manager triggers.

By the allowedit team · · 8 min read

The short answer

  • A cookie banner that only shows a message doesn't stop anything. Scripts have to be held back until the visitor allows their category.
  • The simplest manual method: change a script's type to text/plain and add data-category, so the browser ignores it until the consent tool switches it back on.
  • Embeds like YouTube and Google Maps need the same treatment, with the URL moved to data-src.
  • Google tags can rely on Consent Mode, but every non-Google tag needs blocking, and the consent script must load first.

To block a script until consent, stop the browser from running it, then let your consent tool switch it on once the visitor allows its category. There are three ways to do that: automatic blocking of known trackers, marking scripts as type="text/plain" with a data-category, and firing tags from Tag Manager only after a consent event. Most sites use a mix, and all of them depend on the consent script loading first.

This guide covers each method with examples, plus the embeds and edge cases that are easy to miss.

Why isn't a banner enough?

A banner is a question. Blocking is what makes the answer count.

On many sites the banner appears, the visitor clicks Reject all, and the Meta Pixel, Google Analytics and a heatmap tool have already loaded and set their cookies. In the EU, the UK, Switzerland and Quebec, that's the most common way a banner fails: the rule is no non-essential cookies before consent, not after.

You can see whether your site does this in a minute. Open it in a private window, open your browser's developer tools, and look at the Network tab and the cookies before touching the banner. Or run our free cookie checker, which loads the page in a real browser and flags every cookie and tracker set before consent.

Whatever method you use, the consent tool must load before anything it controls. Put it as the first script in the <head>:

<head>
  <script src="https://allowedit.com/c/YOUR_SITE_ID.js"></script>
  <!-- then Tag Manager, analytics and everything else -->
</head>

If Tag Manager or a pixel loads above it, that tag has already run by the time the consent tool wakes up. No setting can undo a request the browser has already sent.

Method 1: automatic blocking

The least work is to let the consent tool recognise trackers by where they load from. allowedit's auto-blocking knows 40+ trackers, including the Meta Pixel, TikTok Pixel, LinkedIn Insight Tag, Hotjar, Microsoft Clarity, Bing UET, the Pinterest tag, and YouTube and Google Maps embeds. When a page tries to add one of them, the script holds it until the visitor allows its category, then lets it through.

Automatic blocking is good for:

  • Scripts added by plugins, themes, app stores and site builders, where you can't edit the tag.
  • Catching trackers someone adds later without telling you.

Its limits:

  • It only knows the trackers on its list. Anything else needs a manual rule.
  • It works best when the consent script is first in the <head>, so it's running before other scripts are added.

For tools it doesn't know, add your own URL rules: a piece of the script or iframe address (like widget.example-chat.com) and the category it belongs to. The auto-blocking docs show how.

Method 2: mark scripts as text/plain

When you control the HTML, this is the most reliable method. Browsers only run scripts with a JavaScript type, so changing the type to text/plain makes the browser ignore it. Add data-category to say which consent category it needs.

An external script, before:

<script async src="https://example-analytics.com/tag.js"></script>

After:

<script type="text/plain" data-category="analytics" async src="https://example-analytics.com/tag.js"></script>

An inline script works the same way:

<script type="text/plain" data-category="marketing">
  !function(f,b,e,v,n,t,s){ /* the pixel's own snippet */ }(window, document, "script");
  fbq("init", "YOUR_PIXEL_ID");
  fbq("track", "PageView");
</script>

When the visitor allows that category, the consent tool turns each matching script into a normal one, in page order. If they never allow it, it never runs.

The categories allowedit uses are preferences, analytics and marketing. Necessary scripts don't need marking: they should run for everyone.

Blocking embeds: YouTube, maps and widgets

Iframes set cookies too. A standard YouTube embed loads YouTube's player and its cookies as soon as the page loads. Google Maps does the same.

To hold an iframe, move its address from src to data-src and add data-category:

<iframe
  data-src="https://www.youtube.com/embed/VIDEO_ID"
  data-category="marketing"
  width="560" height="315"
  title="Product tour"
  allowfullscreen></iframe>

Until the visitor allows marketing, the frame stays empty, and allowedit shows a placeholder in its place with a short message and a button to allow it. Once allowed, data-src is copied to src and the video loads.

Which category? YouTube and Maps embeds are usually treated as marketing, because the providers use the data for advertising. Some sites put them under preferences. Pick one and describe it honestly in your cookie declaration.

Method 3: Google Tag Manager triggers

If your tags live in Google Tag Manager, block them there instead of in your HTML.

  1. Load the consent script before the GTM container.
  2. For Google tags (GA4, Google Ads, Floodlight), rely on Consent Mode v2. The consent script sets the defaults before GTM runs, and Google tags adapt on their own.
  3. For non-Google tags (Meta, TikTok, Hotjar and so on), add a trigger on the consent event. allowedit pushes an allowedit_consent event to the dataLayer after each choice, with the categories allowed, so you can fire a tag only when marketing is granted.
  4. Alternatively, use GTM's built-in consent settings: under each tag's Advanced settings, add "Require additional consent" for the matching consent type.

The Google Tag Manager guide has the step-by-step setup.

Which method should you use?

SituationBest method
Plugin, app or theme adds the trackerAutomatic blocking, or a URL rule
You paste the tag into your HTMLtype="text/plain" with data-category
YouTube, Vimeo, maps or other iframesdata-src with data-category
Tags are managed in Google Tag ManagerConsent Mode for Google tags, consent triggers for the rest
Something still loads before consentA URL rule for its host, then re-scan

What about Google Analytics specifically?

Google Analytics has a choice the others don't. With Consent Mode in basic mode, the GA4 tag waits until consent, like any other blocked script. In advanced mode, it loads straight away in a denied state, sets no cookies, and sends cookieless pings that Google uses for modelling.

That's why allowedit's auto-blocking leaves Google tags alone when Consent Mode is on in advanced mode: they're handled by Consent Mode instead. In basic mode, they wait like everything else.

How to check it worked

  1. Open your site in a private window.
  2. Before clicking anything, look at the cookies (in developer tools, under Application or Storage) and the Network tab. You should see only necessary cookies and no requests to ad or analytics hosts (Google requests in advanced mode are the exception).
  3. Click Reject all, then visit another page. Still nothing.
  4. Clear the site's data, reload and click Accept all. Now the trackers should load.

Then let a cookie scanner repeat the check across your pages. allowedit's scanner visits up to 25 pages a month on the free plan, or 100 pages a week on Pro and Agency, records what's set before and after consent, and emails you when a new cookie appears.

Common problems

  • The tracker loads anyway. It's probably above the consent script, or injected by a plugin before it. Move the consent script up, or add a URL rule.
  • The page breaks after blocking. Another script expects the blocked one to exist (for example, code calling fbq() before the pixel loads). Mark the dependent code with the same category.
  • Inline code runs twice. You've left an unmarked copy of the snippet in the page, often from a theme setting. Remove the duplicate.
  • Embeds show blank boxes. That's the placeholder working. Make sure the frame has a width and height so the placeholder has room.

The troubleshooting guide and the manual blocking docs cover more cases.

The short version

Load the consent script first. Let automatic blocking catch known trackers, mark your own tags with type="text/plain" and data-category, move embed addresses to data-src, and let Consent Mode handle Google tags. Then test in a private window, and scan regularly so new trackers don't slip in.

Frequently asked questions

Why doesn't my banner stop Google Analytics?

Usually because the analytics tag loads before the banner script, or because nothing tells it to wait. Put the consent script first in the head, and either block the tag or use Google Consent Mode so it loads in a denied state.

What does type text/plain do?

Browsers only run scripts with a JavaScript type. A script marked text/plain is treated as inert text, so it doesn't run. A consent tool can later copy it into a normal script once the visitor allows its category.

Can I block scripts added by a plugin or theme?

Often not by editing their tags, because you don't control the markup. That's where automatic blocking and URL rules help: the consent tool watches for scripts from known tracker hosts, or hosts you list, and holds them until consent.

Does blocking slow my site down?

It usually speeds up the first visit, because trackers don't load until someone agrees. The consent script itself should be small; allowedit's is under about 10 KB gzipped and served from the edge.