This is a simple cookie consent management pop-up built using JavaScript to work alongside Google Tag Manager (GTM). It allows blocking GTM tags from firing unless a user has given a 'true' consent.
Not tested for production use.
The functionality of this pop-up is:
-
On first page load, fire ‘necessary’ tags only, and prompt user for consent to fire optional tags.
-
Once user gives consent, drop a cookie storing their consent and fire the optional tags.
-
If the consent cookie already exists, check if it is still valid. If yes, respect the past consent and not show the pop-up again.
-
Reset consent if the past consent is no longer valid (in case you’ve made changes to your cookie policy and want fresh consent), and display the pop-up again.
-
Create a new Custom HTML tag in GTM and load the JavaScript within
<script>
tags. -
Create a new 1st Party Cookie variable, name it “cookie-consent”, with the Cookie Name being our consent cookie’s name (default is
cookie-consent
). -
Set up a Custom Event trigger, name it “Cookie Consent – No” with:
-
event name name matching
.*
(tick “Use regex matching”) -
Fire on Some Custom Events, where variable
cookie-consent
does not contain"consent":true
.
-
-
Set up a Custom Event trigger, name it “Cookie Consent Set”, with the Event Name
cookie-consent
to fire on All Custom Events. -
Create a tag which shouldn’t fire when consent is false and:
- Add your regular triggers (e.g. All Pages).
- Add the new “Coookie Consent Set” trigger to it too.
- Under Exceptions, add the Add the “Cookie Consent – No” trigger.
Read more about this cookie consent pop-up on this blog post.