Passing HubSpot Cookie Consent to Google Tag Manager
SOLVE
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:
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 Modeabove 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.
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.
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')
}
}])