Add the Privacy Consent cookie banner to every Drupal page
Add the TermsFeed Privacy Consent cookie banner to a Drupal 10 or Drupal 11 site by placing its snippet in the <head> of every page. Drupal offers 3 ways to do it: the theme template, a theme library, or a header script module. Setup takes about 20 minutes.
The install screen in the TermsFeed app has no Drupal tile, and Drupal takes the same snippet as a hand-coded site. Before you start, get access to your theme files (or permission to install modules), and list the modules that print tracking tags or show a consent banner. Create the consent banner and copy its snippet with Create Your First TermsFeed Privacy Consent Banner.
The Install page of your consent banner gives this snippet, with your own token in place of YOUR_TOKEN.
<!-- Privacy Consent by TermsFeed (https://www.termsfeed.com) -->
<script type="text/javascript" src="https://cdn.termsfeedtag.com/plugins/pc/v1/YOUR_TOKEN/plugin.js" async></script>
<!-- End Privacy Consent by TermsFeed (https://www.termsfeed.com) -->
Edit a custom theme or a subtheme, never a core or contributed theme, since an update of the parent theme replaces its files.
Method 1: Add the snippet to the Drupal theme template
Drupal prints the head of every page from html.html.twig, so one edit reaches the whole site.
- Copy
html.html.twigfrom the base theme into thetemplatesfolder of your subtheme, unless the subtheme already has one. - Open the copy and find the
<js-placeholder>line inside the head. - Paste the snippet after that line, directly before
</head>. - Deploy the file and run
drush cr.
Method 2: Load the snippet as a Drupal theme library
A theme library adds the script through the Drupal asset system, which suits a theme you keep in version control. Add this definition to mytheme.libraries.yml, with your token in the address and your theme name in place of mytheme.
privacy-consent:
header: true
js:
'https://cdn.termsfeedtag.com/plugins/pc/v1/YOUR_TOKEN/plugin.js':
type: external
attributes:
async: true
Next, attach the library to every page in mytheme.info.yml, and run drush cr.
libraries:
- mytheme/privacy-consent
The header: true line places the script in the head instead of at the end of the body. Drupal leaves an external file out of JavaScript aggregation, so the page keeps loading the script from cdn.termsfeedtag.com.
Method 3: Paste the snippet into a Drupal header script module
A header script module keeps the snippet in the Drupal configuration, which suits a site whose theme files you do not edit. Install the module, open its settings, and paste the snippet into the field for header scripts. Choose a module with a field for script tags, since a field that takes only JavaScript code rejects or breaks the snippet.
Turn off Drupal consent modules
Uninstall every cookie consent module before Privacy Consent goes live, such as EU Cookie Compliance, COOKiES, or Klaro, under Extend > Uninstall. A module left on shows its own consent banner and stores its own answer, so a visitor answers twice and you keep 2 separate records.
Privacy Consent needs no geolocation module, because the script learns the visitor location from its own settings request. The page HTML stays the same for every visitor, so the Drupal page cache and Varnish serve it unchanged.
Move Drupal module tracking into Privacy Consent vendors
The Google Analytics module and the Google Tag module print their tags on every page, and those tags run before the visitor answers. Move each tool into a vendor so it waits for consent.
- Note the tracking or container ID in the module settings.
- Uninstall the module, or clear its ID so it prints nothing.
- Add the tool in Vendors in the TermsFeed app, from its template where one exists.
- Run
drush crand search the page source forgoogletagmanager.comto confirm the old tag is gone.
Delete each tag only after its vendor exists. Set up each tool with Add a Vendor From a Template.
Add a Privacy Preferences link to the Drupal footer
Create a custom block in the footer region under Structure > Block layout, with the "Full HTML" text format, and enter this markup in the source view of the editor. A restricted format such as "Basic HTML" strips the id attribute, and the link then opens nothing.
<a href="#" id="open_privacy_preferences_element">Privacy Preferences</a>
A click on this element opens the Privacy Preferences Center. Compare the link with the floating button in Let Visitors Reopen Preferences.
Clear the Drupal cache after every change
Drupal caches rendered pages and compiled templates, so a change to the snippet, the template, the library, or a module stays invisible until the cache clears. Run drush cr, or click Clear all caches under Configuration > Development > Performance. Purge any cache in front of Drupal too, such as Varnish or a content delivery network, because it keeps the old head until it expires.
List your Drupal domains on the consent banner
List each Drupal domain that carries the snippet under "Additional domains" on the Settings page in the TermsFeed app. The consent banner loads only on the main website domain and the additional domains, so a domain missing from the list shows none. Follow the steps in Multiple Domains.
How to verify Privacy Consent on Drupal
Publish the change, clear the cache, and test while logged out, because Drupal serves logged-in users a page that skips the page cache.
- Open the site in a private window, open the DevTools Network tab, and filter for
termsfeedtag. - Reload the page and confirm 7 requests with status 200:
plugin.js,plugin-2.js,plugin-r2.js,preferences-r2.js,vendors-r2.js, andsettings.jsontwice. - Select the second
settings.jsonrequest, open Preview, and confirm"serviceable": true. - Open the Console tab and confirm that no line after
[termsfeed]holds[ee]or[ww]. - Change the Network filter to the host of a custom vendor, such as
hotjar.com, and confirm no request appears before you answer. - Filter for
events.push, click I Agree, and confirm a new request with status 201 and the vendor requests.
Test I Decline after closing every private window and opening a new one, since private windows share storage while any stays open. Run the remaining checks with Verify Your Installation.
Common mistakes when installing Privacy Consent on Drupal
- The cache was not cleared. Drupal serves the old template or library until you run
drush cr. - The edit sits in a contributed theme. A theme update replaces
html.html.twigand removes the snippet. - The library has no
header: trueline. Drupal places the script at the end of the body, which delays the consent banner. - A tracking module stays enabled. The tag it prints runs before consent, next to the vendor that runs the same tool after consent.
- The footer block uses a restricted text format. The format strips the
idattribute, so the link opens nothing.