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, most of it spent on module checks and caches.
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. 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 Preferences Center, the second layer of the consent banner, where visitors change their answer. 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
Enter your domain in Settings, under "What is your website domain?", and add any other domain the site answers on under "Additional domains". The app stores www.example.com as example.com, and that entry covers www and every subdomain. The consent banner loads only on the domains in the list. It decides which banner loads the Analytics page counts, and a hostname you did not list does not show there.
Redirect one form of the address to the other, since www.example.com and example.com each keep their own answer in the browser. Add more domains with Run One TermsFeed Privacy Consent Banner Across Several 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. Strict, the consent logic a new consent banner starts on, asks every visitor. Under Standard in Settings, a visitor in a US state does not see a consent banner, so pick a test location from Cookie Banner by Visitor Location.
- Open the site in a private window, open DevTools, select the Network tab, and type
termsfeedtagin the filter box. - 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 rest of the checks with Check That Your TermsFeed Privacy Consent Installation Works.
Common mistakes when installing Privacy Consent on Drupal
Most Drupal faults come from a cache that serves old pages or a module that still prints its tag.
- 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.