Setup Matomo Analytics with Drupal and respect “Do Not Track” header (GDPR compliant)

Drupal

Install the “EU Cookie Compliance” Module

  1. Make sure you’re using the EU Cookie Compliance module.
  2. Set the module up to your liking.
    This module will provide you with a customisable consent banner

Matomo Analytics

Adjust the Matomo tracking code

navigator.doNotTrack // Returns 1 if user doesn't want to be tracked
// If browser sends DNT, automatically decline
if (navigator.doNotTrack) {
Drupal.eu_cookie_compliance.declineAction();
}
// If consent is not given
if (!Drupal.eu_cookie_compliance.hasAgreed()) {
_paq.push(['disableCookies']);
}
// If consent is given
if (Drupal.eu_cookie_compliance.hasAgreed()) {
_paq.push(['setConsentGiven']);
_paq.push(['rememberCookieConsentGiven']);
}
_paq.push(['requireCookieConsent']);
var idSite = SITEID; // Most likely ID 1
var matomoTrackingApiUrl = 'https://YOURMATOMOHOST/matomo.php';
var _paq = window._paq = window._paq || [];

_paq.push(['setTrackerUrl', matomoTrackingApiUrl]);
_paq.push(['setSiteId', idSite]);
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
_paq.push(['requireCookieConsent']);
// Check if user has given consent or not
document.addEventListener('DOMContentLoaded', function() {
// If browser sends DNT, automatically decline
if (navigator.doNotTrack) {
Drupal.eu_cookie_compliance.declineAction();
}
// If consent is not given
if (!Drupal.eu_cookie_compliance.hasAgreed()) {
_paq.push(['disableCookies']);
}
// If consent is given
if (Drupal.eu_cookie_compliance.hasAgreed()) {
_paq.push(['setConsentGiven']);
_paq.push(['rememberCookieConsentGiven']);
}
}, false);

--

--

--

Sascha Eggenberger. Senior User Experience Designer @unic @Drupal Contributor. Drupal Design System, Claro, Gin. #OpenSource https://sascha.is/

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How to Install and Use Fira Code Font — VSCode on Ubuntu

Betting On Bits For Better Podcasting

DIDKit v0.2.1 Now Available on Cargo

Wiremock | How to Test a POST request file upload

Get a Crash Course in Offender Profiling

Multicluster Istio on GKE

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Sascha Eggenberger

Sascha Eggenberger

Sascha Eggenberger. Senior User Experience Designer @unic @Drupal Contributor. Drupal Design System, Claro, Gin. #OpenSource https://sascha.is/

More from Medium

ER Diagram An Entity Relationship Diagram (ER Diagram) pictorially explains the relationship…

Error Handling in Cloud Flow

How to Migrate your H2 Databases to the latest H2 version

Subscriptions in FHIR