Piwik Consent Manager integration with GTM and Google Consent Mode

May 15, 2022 in Google Tag Manager

Lately, I’ve been working on the integration of Piwik Consent Manager with Google Tag Manager and Google Consent Mode API (using Simo Ahava’s template).

It hasn’t been a straightforward process also because Piwik’s documentation is a bit outdated 😉 and that’s why I’m writing this post where I want to describe my own solution.

Disclaimer – If you want to fully understand this post, you have to know Consent Mode API and how they work. If you never used them, you should read the documentation I linked above.

How does Piwik’s Consent Banner work?

First of all, let’s analyze the Piwik’s consent banner (in this post, I won’t describe how to set up and implement Piwik Consent Manager). Every time a new user lands on the website a cookie ppms_privacy_YOUR_WEBSITE_ID is created. Inside the cookie, Piwik stores the user consent choices using 3 digits:

  • -1 = the user has not yet given consent
  • 0 = the user has not given consent
  • 1 = the user has given consent

In order to use them, you have to create a variable which stores the information and then pass them to the Consent Mode API.

1. Store Consent Choices in a 1st party Cookie variable

In GTM, you have to create a new 1st-Party Cookie variable and paste your ppms_privacy_YOUR_WEBSITE_ID. Also, tick the option URI-decode cookie.

2. Read the cookie with Custom JavaScript variables

Google Consent Mode API doesn’t work with -1, 0, 1 but the words granted and denied. You have to create 3 custom JavaScript variables, one for each consent.

In the script below, parse the Cookie variable you created earlier, read the status of a specific consent (the example reads analytics consent) and returns granted or denied according to the number stored inside the cookie.

  • -1 = denied
  • 0 = denied
  • 1 = granted

3. Build a trigger that fires when the user makes/updates his consent choices

If you want to use Consent Mode, you have to be sure to update the consent choices every time the user changes his consent.

Luckily, you can use an event called stg.consentsWereSent that Piwik Consent Manager pushes to the dataLayer when the user makes his choice.

You have to create a Custom Event which will fires whenever the user interacts with the consent banner or the “Change Consent Options” button.

4. Update Consent Mode API using Simo Ahava’s template

At this point, you are ready to activate and set up Simo Ahava’s Consent Mode template. Go to the Template Gallery and search for Consent Mode (Google tags).

Then you should create two Consent Mode tags, one with the Default consent command that will be fired on every page. And a second tag with the Update consent command that is fired on the dataLayer event stg.consentsWereSent (when the user makes his choice).

Inside the tags, you also have to use the custom JavaScript we created earlier. They’ll pass the user choice to the Consent Mode API.

And that’s it! You are done with this tutorial.

If you want, you can also combine the custom JavaScript variables with the dataLayer event to fire tags right after the user gave consent to analytics or advertising, this is quite important if you don’t want to lose the first pageview 😉

Write a Comment


  1. Hey Ruben,
    thank you very much for this great guide. It is the most concise and helpful guide for handling Piwik Consent that I could find on the Internet. It was a huge help for me.

    I have an observation for my setup. For me, firing the Consent Mode Update tag only on the submission of the content decision (stg.consentsWereSent) makes it so that once users have opted into analytics on the landing page and navigate to another page (which doesn’t show the opt-in banner again), all consent on all other pages will default to the default values, essentially opting out of analytics, because the update event never triggers again after the initial opt-in.

    Is this also the case in your setup or is that something that has

    • Hi Malte, sorry for the late reply. I use consent variables in the default consent mode push, so that I keep user consents across all the pages. I only trigger the consent update when the user change his decisions