Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Overview

Table of Contents
minLevel1
maxLevel

...

4
outlinefalse

...

excludeOverview, Do you need further help?
typelist

...

printablefalse

HubSpot Essential Cookies

HubSpot does drop essential cookies that do not require consent on customers site pages. You can see a full list of these essential cookies here.

HubSpot Non-Essential Cookies

HubSpot can also drop non-essential analytic cookies that are controlled by a (cookie) consent banner. You can see a full list of these non-essential analytic cookies here.

Info

In order to block HubSpot analytic/tracking cookies, HubSpot provides us with the _hsq.push(['doNotTrack']); function which places the __hs_do_not_trackcookie in the visitors browser, which will prevent the HubSpot tracking code from sending any information for the visitor. More details can be found here.

How does it work with the Usercentrics dataLayer?

Standard Package

  1. Add the Data Layer Name e.g. dataLayer in the Usercentrics Admin Interface:

...

  1. Image Added
  2. Use the code below:

...

The full code needs to be added in the body or the footer of the page or directly into the template's body.

Code Block
 <script>
        dataLayer = window.dataLayer || [];

  

...

 

...

 

...

 

...

 

...

 

...

 function removeHsCookies() {
            _

...

hsp.push(['

...

revokeCookieConsent']);
  

...

 

...

 

...

    

...

 

...

 

...

 

...

 

...

console.log('Cookies consent removed');
  

...

 

...

 

...

     

...

 

...

 

...

 _hsq.push(['doNotTrack']);
   

...

 

...

   

...

 

...

  

...

 

...

 console.log("do not Track enabled");
       

...

 }

        function 

...

addHsCookies() {
       

...

 

...

 

...

   

...

_hsq.push(['doNotTrack', { track: true }]);
        

...

    

...

console.log(

...

'HubSpot 

...

has 

...

consent');
        

...

}


      

...

 

...

 

...

var waitForDataLayerObjHubSpot = function (dataLayer, callback) {
  

...

 

...

 

...

 

...

       

...

if 

...

(dataLayer.length > 0) {
        

...

        

...

callback();
        

...

    } else {
           

...

 

...

 

...

   

...

setTimeout(function () {
       

...

     

...

     

...

 

...

  

...

waitForDataLayerObjHubSpot(dataLayer, callback);
 

...

 

...

Business & Enterprise Package

  1. Add the Window Event Name e.g. ucEvents in the Usercentrics Admin Interface:

    Image Removed

  2. Use the code below:

Please keep in mind that this is just an example, you need to adjust it based on your needs.

...

 

...

         

...

 

...

 

...

 

...

 }, 1000);
        

...

 

...

 

...

 

...

 

...

}

...

 

...

 

...

      }
        waitForDataLayerObjHubSpot(dataLayer, 

...

function 

...

() 

...

{

...

         

...

 

...

 

...

 dataLayer.map(function (ele) {
       

...

 

...

 

...

       

...

if (

...

ele.event === 'consent_

...

status') {
              

...

 

...

     if (ele['HubSpot'] === true) 

...

{
  

...

             

...

         

...

addHsCookies();
          

...

 

...

 

...

        } else {
  

...

 

...

 

...

             

...

 

...

 

...

     

...

removeHsCookies();
            

...

        }
    

...

            }

...

 

...

 

...

              

...

return ele;
            

...

})
        

...

}

...

);

    

...

</script>

Business & Enterprise Package

  1. Add the Window Event Name e.g.

...

  1. ucEvents in the Usercentrics Admin Interface:

    Image Added

Below is an example tested for landing pages hosted via HubSpot

Info

We will use the do not track cookie _hsq.push(['doNotTrack']); javacript API and the revoke Cookie _hsp.push(['revokeCookieConsent']); javacript API - HubSpot Cookie banner.

First we add the removeHsCookies() function which does two things:

  1. revokeCookieConsent - Remove the hubSpot cookie consent as the page is a HubSpot hosted page and the tracking code is embedded on the page that’s why it gets automatically dropped. So we need to remove it.

  2. doNotTrack - When the cookie drops on the page with the do not track cookies data becomes anonymised and we don’t track the user

Code Block
 function removeHsCookies(){
  _hsp.push(['revokeCookieConsent']);
  console.log('

...

Cookies 

...

consent 

...

removed');
  

...

_hsq.push(['

...

doNotTrack']);
  console.log("do not Track enabled");
}

Then we create a second function addHsCookies() where we set the doNotTrack = true for the cookie to be removed and HubSpot tracking code to collect data again.

Code Block
function addHsCookies() {
   

...

_hsq.push(['doNotTrack', { track: true }]);

...


  console.log(

...

'HubSpot 

...

has consent');
}

Now we need to call these two functions based on the user consent. To do that we will use EventListener

Code Block
window.addEventListener("ucEvent", function (e) {
      if (e.detail.action == "onAcceptAllServices" ) {
     

...

 

...

 

...

   addHsCookies();
      } else if 

...

(e.detail.event == "consent_status")  {
          if (e.detail['HubSpot'] === true) { // check for consent 

...

status of service "HubSpot"
           

...

addHsCookies();
          }
     

...

 

...

 

...

   else {
            

...

removeHsCookies();
          

...

} 
      } else {
        

...

removeHsCookies();
      }
    });

The full code needs to be added in the body or the footer of the page or directly into the template's body.

Code Block
<script>

function 

...

removeHsCookies(){
  _hsp.push(['

...

revokeCookieConsent']);
  console.log('Cookies 

...

consent removed');
  

...

_hsq.push(['

...

doNotTrack']);
  console.log("do not Track enabled");
}

function addHsCookies() 

...

{
   

...

_hsq.push(['doNotTrack'

...

, { track: 

...

true }]);
  console.log('HubSpot 

...

has consent');
}

window.addEventListener("ucEvent", function (e) {
  if (e.detail.action == "onAcceptAllServices" ) {
    

...

addHsCookies();

...

  } else if (e.detail.event == "consent_status")  {
      

...

if (e.detail['HubSpot'] === true) { // check for consent status of service "HubSpot"
        

...

addHsCookies();
        } else {
          

...

removeHsCookies();

...


        

...

}

...

 
      

...

} else {
        

...

removeHsCookies();
      }

...


    

...

});

...

</script>
Image Added

How to test it:

Test page: Contact - Open your HubSpot page → Dev tools → Application → Cookies

  • You will see hubspotutk and few more cookies to drop there and that is expected behaviour as it is a hosted page.

  • Click Deny All → The hubspotutk cookie will no longer be there and the __hs_do_not_track cookie will drop in the visitors browser, which will prevent the HubSpot tracking code from sending any information for the visitor.

Image Added
  • Click Accept All or give consent to the HubSpot DPS and Save Services (It might need to reloading the page)

Image Added


Do you need further help?

How can I get help with technical questions?