Platform guides
Cookie consent in Drupal: modules and manual setup
In Drupal you can add cookie consent with a contributed module, Klaro being the current recommendation, or by loading a hosted consent script through your theme. Either way, tracking scripts, embeds and analytics modules must wait for consent, and your consent logic has to run in the browser so Drupal's page caches don't get in the way.
- Plain English
- Written for website owners
- Free to read
On this page
To add cookie consent to a Drupal site, either install a contributed consent module or load a hosted consent platform's script in the <head> through your theme or a small custom module. As of late 2026, the Klaro module is the actively developed choice, while the long-standing EU Cookie Compliance module is in maintenance mode and points to Klaro as its successor. Whichever route you take, analytics and marketing scripts, embedded videos and tracking modules must wait for the visitor's choice, and that decision must happen in the browser so Drupal's Page Cache and Dynamic Page Cache don't serve one visitor's choice to everyone.
Your options at a glance
| Approach | Good for | Watch out for |
|---|---|---|
| Klaro module | Sites that want consent managed inside Drupal, with blocking of external resources and embeds | Configuring a service for each third party; consent records and scanning are up to you |
| EU Cookie Compliance module | Existing sites already using it | Maintenance fixes only; plan a move |
| Hosted consent platform | Agencies and teams running several CMSs, or needing scans, logs and regional rules | Tagging scripts yourself; an outside service to manage |
| Google Tag Manager consent settings | Sites where all tracking already runs through GTM | Still needs a banner that sends consent signals |
If you're unsure what a full consent setup involves beyond a banner, our explainer on consent management platforms lists the pieces: blocking, consent storage, logs and signals such as Google Consent Mode.
Contributed modules: Klaro and EU Cookie Compliance
Klaro Cookie & Consent Management
The Klaro module integrates the open-source Klaro! consent library into Drupal and adds an admin interface for it. As of late 2026 its stable branch is 3.1.x, which supports Drupal 10.2 and later and Drupal 11. According to its project page, it:
- organizes consent into "services" (each third party) and "purposes" (the categories visitors see), with editable texts and translations,
- adds the attributes Klaro needs to external resources automatically, and blocks external resources that have no configured service,
- includes placeholders for oEmbed and social media embeds, so they load only after consent,
- supports Google Consent Mode v2 since version 3.1, and
- stores the visitor's decision in a strictly necessary cookie named
klaroby default.
The EU Cookie Compliance project page also notes that Klaro was chosen for Drupal CMS, the packaged version of Drupal aimed at site builders. Klaro handles the banner and blocking; you still need to keep your own inventory of cookies, decide whether you need consent records, and set rules for different regions if you want them.
EU Cookie Compliance
EU Cookie Compliance has been the default Drupal consent module for many years, and its latest releases still support Drupal 10 and 11. Its project page now marks it as minimally maintained with maintenance fixes only and lists Klaro as the replacement. It offers a migration helper module, klaro_migrator, which the maintainers suggest trying on a non-production copy first. If your site runs EU Cookie Compliance today, it doesn't need to come off tomorrow, but plan the move instead of building new features on it.
Adding a hosted consent script to Drupal
If you use an external consent platform, its script has to appear in the <head> of every page, ahead of any tracking. There are three common ways to do it. The examples use PrivacyQuick's tag, which is the same one-line snippet on any platform:
<script src="https://cdn.privacyquick.com/v1/privacy.js" data-site-id="pq_xxxxxxxxxx" async></script>
Option A: a library in your theme
Define the script as an external library with header: true in your custom theme or sub-theme's *.libraries.yml, then attach it globally in the theme's *.info.yml. Replace mytheme with your theme's machine name:
# mytheme.libraries.yml
consent:
header: true
js:
https://cdn.privacyquick.com/v1/privacy.js:
type: external
attributes:
async: true
data-site-id: pq_xxxxxxxxxx
# mytheme.info.yml (add to the existing file)
libraries:
- mytheme/consent
Clear caches afterwards, for example with drush cr. If you use an admin theme too, decide whether the banner should appear there; usually it shouldn't.
Option B: a small custom module
A module keeps the tag working even if you switch themes. Use hook_page_attachments() to add the element to the head:
/**
* Implements hook_page_attachments().
*/
function mymodule_page_attachments(array &$attachments) {
$attachments['#attached']['html_head'][] = [
[
'#type' => 'html_tag',
'#tag' => 'script',
'#attributes' => [
'src' => 'https://cdn.privacyquick.com/v1/privacy.js',
'data-site-id' => 'pq_xxxxxxxxxx',
'async' => TRUE,
],
],
'privacyquick_consent',
];
}
Recent Drupal versions also support object-oriented hook classes, but the procedural form above still works. Check the rendered page source to confirm the tag appears and sits before your analytics tags.
Option C: override html.html.twig
Copy html.html.twig from your base theme into your theme's templates folder and paste the tag inside <head>, right after the <head-placeholder> line. It works, but you're now maintaining a copy of a template that may change in core or base theme updates, so options A and B are easier to keep tidy.
Blocking scripts until consent
Showing a banner doesn't stop anything from running. Each non-essential script has to be held back until its category is allowed. Our guide to blocking cookies before consent compares the techniques; here's how they map to Drupal.
Scripts you control
For tracking code you add in templates or your own libraries, change the type and add a category, so the consent tool can switch it on later:
<script type="text/plain" data-category="analytics" async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXXXX"></script>
In a library definition, you can add type: text/plain and data-category: analytics under attributes for an external script. Check the rendered HTML to make sure the attributes came through. The script blocking docs show the format for scripts, iframes and images.
Don't put tracking code in body fields. Drupal's text formats and the editor usually strip <script> tags from content, and code that slips through is hard to find later.
Analytics and tag modules
Contributed modules for Google Analytics, Google Tag Manager, Matomo, pixels and chat widgets each add their own scripts. For each one, check whether it has a consent setting, supports Google Consent Mode, or integrates with your consent module. If it has none of these, either replace it with a tagged script you control or run the tag through Google Tag Manager with consent checks, as described in our GTM consent guide.
Embedded media
Core's Media module renders remote videos, such as YouTube and Vimeo, through oEmbed in an iframe that loads the provider's player. That player can set cookies as soon as the page loads. Klaro's built-in oEmbed placeholders handle this. With a hosted consent tool, override the oEmbed template or field formatter to output the iframe URL in data-src with a data-category, or use a click-to-load preview. Our guide to YouTube embeds and cookies covers privacy-enhanced mode and its limits.
Page Cache, Dynamic Page Cache and aggregation
Drupal core ships two caching modules that matter here. The internal Page Cache module stores complete pages for anonymous visitors and serves the same HTML to all of them. The Dynamic Page Cache module caches pages for all users, with personalized parts replaced by placeholders. Many sites also have Varnish or a CDN in front.
This works well with consent tools that decide everything in the browser: the cached HTML contains blocked scripts, and the consent script activates them for visitors who agreed. It breaks when server-side code reads a consent cookie and prints different tags. The internal Page Cache keys pages by URL and ignores cache contexts, so the first visitor's choice ends up in the page everyone gets. Keep consent decisions client-side.
Also check JavaScript aggregation under Configuration → Development → Performance. External scripts aren't merged into aggregated files, so the consent script and external tracking tags keep their attributes. Local scripts that get aggregated lose their individual tags, so don't rely on adding consent attributes to local library files without checking the output. After any change, clear Drupal's caches and purge any reverse proxy or CDN.
Policies and a way to change choices
Visitors need to find your cookie information and change their decision at any time. Link your cookie or privacy policy from the banner and the footer, and make sure the policy lists what your modules and third parties set. Klaro and most hosted platforms provide a way to reopen the settings. PrivacyQuick shows a floating cookie button by default and offers PrivacyQuick.open() if you'd rather add a "Cookie settings" link to your footer template.
Testing and checklist
- Clear Drupal's caches, then open the site in a private window, logged out.
- Before clicking the banner, check DevTools: no analytics or ad cookies in the Application tab, no requests to tracking domains in the Network tab. Our Chrome DevTools guide shows where to look.
- Click Reject all, browse a node, a view and a page with embedded media. Nothing non-essential should load.
- Change your choice to Accept all and confirm the tags and embeds now load.
- Reload a second time to make sure the cached page behaves the same as the first load.
- A consent module or hosted script is in the head of every front-end page, before tracking.
- Every tracking module and template script is blocked until consent or integrated with Consent Mode.
- Remote video and social embeds are behind consent or click-to-load.
- No server-side code varies output on the consent cookie while Page Cache is on.
- Your cookie policy matches what a cookie scan finds, and you re-scan after adding modules.
Frequently asked questions
Does Drupal core include a cookie banner?
No. Drupal core sets a session cookie for logged-in users but has no consent banner. You need a contributed module such as Klaro, or an external consent platform added through your theme or a custom module.
Is the EU Cookie Compliance module still maintained?
As of late 2026 its project page lists it as minimally maintained, with maintenance fixes only, and points to the Klaro module as its replacement. Existing sites can keep running it, but new projects are better served by Klaro or another actively developed option.
Will Drupal's Page Cache break my cookie banner?
Not if the banner decides everything in the browser. The internal Page Cache serves the same HTML to all anonymous visitors, so any server-side code that prints or hides tags based on a consent cookie will show the first visitor's choice to everyone.
Where do I put a script tag in Drupal's head?
The cleanest ways are a library in your theme with header set to true, or a small custom module using hook_page_attachments. You can also override html.html.twig in your theme, but you then maintain a copy of a core template.
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.