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

What is the best way to prepare for a System Design interview for FAANG Companies?

Planet X : Android Application Based CTF Challenge Walkthrough

Part 2 Modern Java/JDK Concurrency

Flutter Öğreniyorum -75-

Shell Sort

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

Delegation and Monitoring

Priority Reducing STP Election (PRSTP) Configuration using Packet Tracer

The golden rule for securing SAP solutions: embed it from the start

Experience DevOps for SAP Cloud, built on SAP cloud with ReleaseOwl