Using addPrivacyConsentListener to control third-party tracking code (Google analytics) (GDPR)

Highlighted
Occasional Contributor

Hello - I can't seem to find any direct examples of this or case studies:

 

We want to be able to turn GA tracking on/off based on HubSpot's in-built GDPR opt-in opt-out.

 

Can anyone see an issue with wrapping the GA tracking code in HubSpot's addPrivacyConsentListener function and running if 'consent.allowed' is true?

 

Site Footer:

 

_hsq.push(['addPrivacyConsentListener', function(consent) {
    if(consent.allowed){

     

      /* GA TRACKING */
      window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());
      gtag('config', 'XXXXXXXXX');

      /* GA TRACKING */


    }
}]);

 

I have tested this (I'm no analytic expert) but it seems to work fine with GA's real-time user tracking. I'm just confused as to why this isn't used as a solution - have a feeling there's some terrible issue here I'm not seeing?

 

Thanks in advance!

 

Al

5 Replies 5
HubSpot Moderator

Hey, @AlexGG.

 

This isn't raising any red flags for me!

Isaac Takushi

HubSpot Developer Support
New Contributor

I used this code right below <head>, but I'm not seeing the GA tracking cookies get added to my computer after I click Accept on the Hubspot banner. Am I doing something wrong?

 

<script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXX-1"></script>
<script>
//Add a listner to get the status of the consent banner
_hsq.push(['addPrivacyConsentListener', function(consent) {
if(consent.allowed) {
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());

gtag('config', 'UA-XXXXXXXX-1');

}
}]);</script>

Reply
0 Upvotes
Occasional Contributor

Hi Jackie,

 

How are you checking cookies? Have you checked whether an impact is made in the analytics real time platform itself?

 

I've noticed, weirdly, that looking at cookies in Chrome > Developer there seems to be a big delay, and at some point Google cookies magically materialise, but I can't seem to find any consistency as to why this happens.

 

Alex

New Contributor

I realized the problem. I needed to add that code below the Hubspot banner code. I was adding it in the header, where Google says to put the tracking code. Not sure how this will effect the stats.

Occasional Contributor

Duh sorry - I realise you said you put it in head originally. Yes I've found it only works in footer!

Reply
0 Upvotes