Passing HubSpot Cookie Consent to Google Tag Manager

Hi,

Wondering if anyone can help solve my challenge.

I want to start using HubSpot tracking and the cookie consent mechanism. But it doesn’t support the management of third party cookies.

I’ve been told the Event API can be used to pass on consent outcome to trigger the Google Tag Manager script - which is what I use to deploy my third party cookies.

Has anyone successfully done this?

So far, my attempts to configure this have been unsuccessful. I’m relying on these resources, and while they each present solutions, none of the configurations I tried have allowed the GTM script to successfully fire:

Any help would be greatly appreciated.

Best regards

Ed

Hi @Ed-Jones ,

Let me get things clear, correct me if I’m wrong, so what you’re trying to do is by using HubSpot cookie banner, if consent is allowed, then trigger or fire a tag in Google Tag Manager?
If that’s the case, you can take a look at either Get privacy consent status or Cookies not by category section. You can try to fire the following code in your website with cookie banner/tracking code installed.

var _hsp = (window._hsp = window._hsp || []);
_hsp.push(["addPrivacyConsentListener", function (consent) {
 if (consent.allowed) {
 //add your code here that fires GTM certain tag.
 console.log('something')
 }
}])

Hi Linda,

Thanks for responding.

I think it was just my lack of implementation knowledge that was holding me back. I took that exact same code from the HubSpot knowledge base.

However, I only got this to function as expected after separating it from other codes and adding <script> tags.

This is what the code looks like (though i’ve added holding text for the GTM account number.

<script>
 var _hsp = (window._hsp = window._hsp || []);
 _hsp.push(['addPrivacyConsentListener', function (consent) {
 if (consent.allowed) { (function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
 new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
 j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src='https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
 })(window,document,'script','dataLayer','GTM-CODE-GOES-HERE');
 console.log('GTM Cookie Consent')
 }
 }]);</script>   

Hi, here is a solution to use HubSpot’s new cookie consent banners (Consent banner API - HubSpot docs) with Google Tag Managers consent settings API (Set up consent mode on websites  |  Tag Platform  |  Google for Developers) for much easier category consent triggering in GTM.

First, enable Consent Overview in your container. Go To your GTM account, navigate to Admin → Container Settings, and tick the Enable consent overview. With this, you will be able to test event consent states in the preview testing of your GTM account.

Second, add this code for the Initialization of GTM Consent Mode above your GTM script. This code checks if a saved consent mode exists in localStorage and sets the default consent mode for GTM accordingly. We check if savedConsent exists, if it does, an immediately-invoked function expression is used to try parsing the saved consent and validate its properties. If the parsing or validation fails, the defaultConsent is returned, and if savedConsent doesn’t exist, the defaultConsent is used directly.

window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}

var savedConsent = localStorage.getItem('consentMode');
var defaultConsent = {
 'ad_storage': 'denied',
 'analytics_storage': 'denied',
 'functionality_storage': 'denied',
};

var consentMode = savedConsent ? 
 (function() {
 try {
 var parsedConsent = JSON.parse(savedConsent);
 return (parsedConsent.ad_storage && parsedConsent.analytics_storage && parsedConsent.functionality_storage) ? 
 parsedConsent : defaultConsent;
 } catch (e) {
 return defaultConsent;
 }
 })() : 
 defaultConsent;

gtag('consent', 'default', consentMode);

And then you add this HubSpot Cookie Consent Listener after your GTM script which waits for any changes in user consent. When a change is detected, it updates the GTM consent mode and saves the updated mode to localStorage.

var _hsp = window._hsp = window._hsp || [];

_hsp.push(['addPrivacyConsentListener', function(consent) {
 var consentMode = {
 analytics_storage: consent.categories.analytics ? 'granted' : 'denied',
 ad_storage: consent.categories.advertisement ? 'granted' : 'denied',
 functionality_storage: consent.categories.functionality ? 'granted' : 'denied'
 };

 gtag('consent', 'update', consentMode);
 localStorage.setItem('consentMode', JSON.stringify(consentMode));
}]);

So now, inside GTM, on a tag level I can control which tags are fired when:

Hey Ed! I struggled with this myself so I’ve made a GTM template that aims to fix exactly what you are looking for. Check it out: Tag Manager Template Gallery

Thanks for sharing @avoytenko :raising_hands: — Jaycee

Hi! Thanks for the template, it works! :slightly_smiling_face:
Just missing setting ‘functionality_storage’ and I don’t see how to add it in the tag. But HS banner has it. So I’ve modified your template to have ‘functionality_storage’ denied at first, and then changed according to user’s preferences.

Hey! Glad to hear it worked for you! I left out functionality_storage because I assumed that it should be always on, but I can certainly update the template to include it!

Hi! I’ve already updated it for me, it’s easy. Just to fit functionality cookies on the HubSpot banner. But strangely there is nothing about it anywhere on HubSpot help. Thanks for the great template!

This community is amazing, great to see how people help each other. I have been searching desperately for a solution that does this the other way around.
We have a OneTrust cookie banner on our website and I want to push cookie consent (level) through to HubSpot so we can add it as a contact property.
Why?
This allows us to better connect LinkedIn ads and HubSpot so that I can better see our funnel in LinkedIn ads.
Does anyone have experience with this or know how to push contact properties into HubSpot via Google TagManager?