Platform guides
How to add a cookie banner to any HTML website
Paste the banner script into the head of every page, change your tracking scripts and embeds so they wait for consent, and test the result in a private window. This guide walks through each step for hand-coded and static sites.
- Plain English
- Written for website owners
- Free to read
On this page
To add a cookie banner to an HTML website, paste the consent tool's script tag near the top of the <head> on every page, then change each analytics, advertising and embed tag so it only runs after the visitor allows it. Finish by adding a "Cookie settings" link to your footer and checking in a private browser window that nothing non-essential loads before a choice. The whole job takes under an hour on most small sites.
This is the generic method. It works for hand-written HTML, static site generators, PHP templates and any builder that lets you edit the page head. Some platforms have their own quirks, so if you use one, see the dedicated guides linked at the end.
Before you start: list what your pages load
You can't hold back what you don't know about. Spend ten minutes building a list of everything on your site that sets cookies or contacts a third party:
- Analytics: Google Analytics, Matomo, Microsoft Clarity, Hotjar and similar tools.
- Advertising and retargeting: Meta Pixel, Google Ads conversion tags, LinkedIn Insight Tag, TikTok Pixel.
- Embeds: YouTube or Vimeo videos, Google Maps, social media posts, booking or form widgets.
- Functional extras: live chat, font services, review widgets.
Search your HTML for <script and <iframe tags, then confirm in the browser. The guide to checking cookies in Chrome DevTools shows where to look, and a free cookie scan crawls several pages at once. For a fuller inventory with owners and purposes, follow the website cookie audit process.
Then assign each item a category. Most tools use four:
| Category | Typical contents | Needs consent in the EU/UK? |
|---|---|---|
| Necessary | Session, login, shopping cart, security and load-balancing cookies, the consent cookie itself | No |
| Functional | Chat widgets, saved preferences, some embeds | Yes |
| Analytics | Visitor statistics, heatmaps, session recording | Yes, in most countries |
| Marketing | Ad pixels, retargeting, most video and social embeds | Yes |
If an item is hard to place, cookie categories explained walks through borderline cases.
Step 1: Add the banner script to every page
Paste the snippet as high in the <head> as you can, above any analytics or advertising tags. With PrivacyQuick it looks like this:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://cdn.privacyquick.com/v1/privacy.js" data-site-id="pq_xxxxxxxxxx" async></script>
<title>Example</title>
<!-- analytics and other tags go below -->
</head>
The order matters because the consent tool has to be running before anything it controls. The script loads with async, so it doesn't hold up rendering.
On a site with many pages, add the tag once in the shared head file instead of on every page:
- PHP sites: the header include, often
header.phporincludes/head.php. - Static site generators: the base layout or head partial, for example
layouts/partials/head.htmlin Hugo or_includes/head.htmlin a typical Jekyll theme. - Plain HTML with no templates: every
.htmlfile. A project-wide search for</head>shows you how many files to change. Remember 404 pages and landing pages that live outside the main folder.
Step 2: Hold back scripts until consent
The banner on its own doesn't block anything. Browsers run every <script> tag they see, so you change the tags you want to wait. Set type="text/plain", which browsers don't execute, and add a data-category attribute. When the visitor allows that category, PrivacyQuick turns the tag back into a working script.
Here is the Google Analytics 4 snippet before the change:
<script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXXXX"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-XXXXXXXXXX');
</script>
And after:
<script type="text/plain" data-category="analytics" async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXXXX"></script>
<script type="text/plain" data-category="analytics">
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-XXXXXXXXXX');
</script>
Tag both parts. If you tag only the inline part, the library still loads and can set cookies. With gtag, tagging only the library would happen to work, because the inline part just queues commands. Many other vendors' inline snippets load their library themselves, though, and the Meta Pixel base code is one example. Tagging every part of a snippet is the habit that avoids mistakes. Do the same for every script on your list except the strictly necessary ones, which you leave alone.
Google tags have a second option. PrivacyQuick has Google Consent Mode v2 built in, so you can leave Google tags untouched and let them run in a restricted, cookieless mode until consent. Which approach suits you depends on your setup; the Consent Mode docs explain both.
Iframes and images
Embeds load as soon as the browser sees their src, so move the address into data-src and add a category:
<iframe data-src="https://www.youtube-nocookie.com/embed/VIDEO_ID"
data-category="marketing"
width="560" height="315"
title="Product demo"
allow="encrypted-media; picture-in-picture" allowfullscreen></iframe>
<img data-src="https://tracker.example.com/pixel.gif" data-category="marketing" width="1" height="1" alt="">
Until the visitor allows marketing, the iframe is an empty box. Keep the width and height attributes so the layout doesn't jump when it loads, and consider a short line of text near it explaining why the video isn't showing. The script blocking docs list the attributes in full.
Cookies your own server sets
Not every cookie comes from a script tag. A PHP, Node or Python backend can set cookies in its HTTP response headers before any JavaScript runs, and a banner can't intercept those. Look at the Set-Cookie headers in the Network tab for your page requests. A session cookie for a login or a shopping cart is usually strictly necessary. A cookie that a server-side analytics or A/B testing library sets on every visit is not, so change that code to check the visitor's consent cookie before setting anything. PrivacyQuick's pq_consent cookie is readable on the server for this purpose, as the JavaScript API docs describe.
Step 3: Let visitors change their mind
Under GDPR Art. 7(3), withdrawing consent must be as easy as giving it. Visitors need a way back to the preferences panel on every page. PrivacyQuick shows a floating cookie button by default, and you should also add a link to your footer:
<a href="#" onclick="window.PrivacyQuick && PrivacyQuick.open(); return false;">Cookie settings</a>
If a visitor turns off a category whose scripts already ran, the page reloads so those scripts stop. Scripts can't be "unloaded" from a page that's already running them, so a reload is the reliable way.
Step 4: Publish a cookie policy
A banner tells visitors what you'd like to do; your cookie policy explains it in detail: which cookies you use, who sets them, what they're for and how long they last. Link to it from the banner and from your footer next to the privacy policy. What to include in a cookie policy has a table layout you can copy. PrivacyQuick's policy generator can host one for you, with a cookie table that updates from your scans.
Step 5: Test it properly
Most broken cookie banners look fine. The banner appears, and trackers load anyway in the background. Test what the browser actually does:
- Start clean. Open a private window so no earlier choice or cookie is stored. Open DevTools before loading the page.
- Load a page and do nothing. In the Network tab, filter by the domains on your list, such as
google-analytics,facebookoryoutube. There should be no requests. In Application → Cookies, only necessary cookies should appear. - Click Reject all. Browse to two or three other pages. Still no third-party requests, and the banner shouldn't return.
- Open your footer link and accept one category. Only that category's scripts should load, without a page reload.
- Withdraw it again. The page should reload, and those scripts shouldn't load after the reload.
- Repeat on a phone-sized screen. Make sure the banner doesn't cover the buttons a visitor needs, and that Reject all is as easy to reach as Accept all.
- Check deeper pages. Blog posts, landing pages and old templates often carry their own copy of a tracking tag. Test a few.
Common problems and fixes
| Symptom | Likely cause | Fix |
|---|---|---|
| No banner appears | Wrong site ID, or a Content Security Policy blocks the script | Check the console. Allow the banner's domain in script-src and connect-src. |
| A tracker loads before consent | A tag was missed, or a different script injects it | Search all templates again. If a tag manager adds it, control it there, not in the HTML. |
| A script never loads, even after Accept all | A typo in data-category, or a category your banner doesn't offer | Use exactly analytics, functional or marketing, and give every part of a snippet the same value. |
| Two banners appear | An old consent plugin or widget is still installed | Remove the old one. Two tools fighting over the same scripts produce unpredictable results. |
| Changes don't show on the live site | A CDN or host is serving cached HTML | Purge the cache or wait for it to expire, then test again. |
If your site runs on a CMS or a framework, the steps are the same but the place you paste the snippet differs. See our guides for WordPress, React and Next.js, and HubSpot, Ghost and other site builders.
Platform-specific guides: WooCommerce, Drupal, Joomla, Magento, BigCommerce, Framer and Vue and Nuxt.
More platform guides: PrestaShop, Square Online, GoDaddy Website Builder, Carrd, Angular, SvelteKit and Astro.
Even more platforms: Jimdo, Weebly, Duda, Ecwid, Bubble, Hugo, Jekyll and Eleventy, Gatsby and Docusaurus.
Next steps
- Finish your list of scripts and embeds, with a category for each.
- Add the banner script to your shared head file and publish.
- Tag every non-essential script, iframe and tracking image.
- Add the "Cookie settings" footer link and a cookie policy.
- Run the seven test steps, then rescan whenever you add a new tool.
When you're done, the GDPR cookie consent checklist is a good final review. If you'd like to use PrivacyQuick for the banner, you can create a free account and copy your site's snippet from the dashboard.
Frequently asked questions
Do I need a cookie banner on a static HTML site with no tracking?
If the site sets no cookies and uses no other device storage beyond what's strictly necessary, the EU and UK cookie rules don't require a consent banner. Check third-party embeds, fonts and widgets carefully, because they are the usual source of cookies on otherwise plain sites.
Can I host the cookie banner script myself instead of loading it from a CDN?
With a hosted consent service, load the script from the provider's URL so you receive fixes and configuration updates automatically. A self-hosted copy can fall out of date and stop matching the settings in your dashboard.
Will the banner work on a site hosted on GitHub Pages or Netlify?
Yes. Static hosts serve your HTML as written, so a script tag in the head works the same way it does on any other server. If your host adds security headers, check that the Content Security Policy allows the banner's domain.
Where do I put the cookie banner code if my pages share a template?
Put it in the shared head file or partial that every page includes, so you only add it once. In static site generators that's usually a layout or head partial; in PHP sites it's often a header include.
Check your own website in 30 seconds
Our free scanner lists every cookie and tracker your site sets, sorted into categories, before you add a banner.
This guide is general information, not legal advice. Laws and regulator guidance change; check the current rules for your situation or ask a lawyer.