Skip to content

How-to

How to block cookies and scripts until visitors consent

A step-by-step guide to holding back analytics, ad and chat scripts until visitors agree: automatic blocking, your own rules, manual tagging and how to test it.

Key takeaways

  • A banner alone does not stop tracking: scripts must actually be held back until consent.
  • Load the consent script first in the <head>, without async or defer, so it runs before anything it needs to hold back.
  • Known trackers are held back automatically; add rules for others, or tag scripts by hand for the strictest result.
  • Test in a private window with ?pgcmp_debug=1 to see what is held back and released.

Step 2: let known trackers be blocked automatically

About 25 common vendors, including Google Analytics, Meta Pixel and Hotjar, are recognised by their address and held back until their category is allowed. You can switch any of them off under Cookie Manager, Scripts.

Step 3: add rules for everything else

Under Cookie Manager, Scripts, add an address pattern such as cdn.example-chat.com and choose its category. Scripts added by other scripts are then never requested before consent. Scripts written directly in your HTML may be downloaded by the browser, but they do not run.

Step 4: tag scripts by hand for zero requests

For the strictest result, change the script's type and add a category. The browser then makes no request at all until consent:

<!-- before -->
<script src="https://www.googletagmanager.com/gtag/js?id=G-XXXX"></script>

<!-- after: runs only once Analytics is allowed -->
<script type="text/plain" data-pgcmp-category="analytics"
        src="https://www.googletagmanager.com/gtag/js?id=G-XXXX"></script>

This works for inline scripts too. Use functional, analytics or marketing as the category.

Step 5: test it

  1. Open your site in a private window. The banner shows.
  2. Add ?pgcmp_debug=1 to the address and open the browser console: it lists what the script holds back and releases.
  3. In the Network tab, check that analytics and ad requests only appear after you accept.
  4. Make a choice, then check that the record appears in your consent log within a few seconds.

Questions

What about scripts from my own domain?

Copies of scripts served from your own domain, for example by a caching plugin, are not matched by address. Leave the original address out of the plugin's minify, combine and delay lists, or tag the script by hand.

What happens when a visitor withdraws consent?

The page reloads and the cookies on your cookie list for that category are deleted, so the scripts stop.

Add your cookie banner today.

Free for your first 10,000 page views. No card needed.