Issues in Hubspot customized tracking code


Hi everyone,
[Need some help regarding Hubspot customized trackign code]
Since the General Data Protection Regulation (GDPR) came into force on 25th May we start taking consent from all the visitors that they are being tracked by Google Analytics and Hubspot tracking code. Now when someone landed on our website ( a pop up appear at the bottom with our updated privacy policy link along with "I agreed" button.

We have customized our Google Analytics and Hubspot tracking codes so that when someone clicks on "I agreed" button ONLY then GA and Hubspot tracking code should trigger.

Now even people are clicking on "I agreed" button, Google Analytics code is triggering fine BUT hubspot code is not triggering every time (As a result showing only 20-25% traffic).
I've generated ticket regarding this issue on Hubsot support but they are unable to resolve the issue and asked me to post the Query here in this developer community.

Hubspot support says that since we have done some customization in the Hubspot tracking code, as a result its not working.
We are looking for such a customized code which must trigger once visitor gives consent.
You can check the current customized hubspot code by viewing source of

Please note that must use our customized banner as consent taking pop up. We can't use Hubspot consent banner as it does not allow Google Analytics code to trigger also.

Looking for possible solution as sson as possible because this issue is becoming a blocker for us and we are not getting right stats from Hubspot.


Hi @osamanaseem,

By default, the HubSpot tracking code won't begin tracking visitors without consent. The default cookie banner collects that consent, which then allows the tracking code to place cookies and begin tracking. The Tracking Code API also has custom methods that allow you to check a visitor's consent status in order to allow/disallow 3rd party tracking codes from running; this is the preferred method for tracking consent on HubSpot pages / when using the HubSpot tracking code. I've included some docs below for more info.

With regard to your current situation; how exactly are you customizing the HubSpot tracking code? Outside of the Tracking Code API (see below), we don't support any customization of the tracking code itself. In order to understand what's wrong with the existing behavior I'll need to understand any changes made.


Hi @Derek_Gervais Thanks for the reply.

We are not using Hubspot cookie banner for consent Because it doesn't includes Google Analytics tracking. We want to get such a consent which allows both GA and hubspot codes to trigger at the same time.
Our Google analytics customized code is now working fine i.e. it triggers when visitor clicks on "I agreed" button But Hubspot customized code is not working fine.

Let me share the hubspot customized tracking code here which we are using. See bellow

var hsScript = document.createElement("script");
hsScript.type = "text/javascript"; = "hs-script-loader";
hsScript.src = "//";
hsScript.setAttribute("async", "");
hsScript.setAttribute("defer", "");

(Line 168 to 174 in page source of
Can you please take a look and guide about the changes which are required for this code to get triggered.
Thanks in advance.


Hi @Derek_Gervais
Just like to ask from you that did you get any chance to see the customized code which I shared above.
Looking forward for your expert advice.


Hi @osamanaseem,

When I navigate to your site and accept the cookie banner, the HubSpot tracking code fires just fine. What issue specifically are you seeing? I'm having trouble reproducing the issue you're describing.

It's also important to note that the tracking code does a lot more than just track webpage visits; the script manages the messages and lead flows tools as well. Visitors that opt out of cookies won't be able to use those features, since you're not loading the tracking script at all.

The recommended solution is to use the default HubSpot banner, and take advantage of the 'Get privacy consent status' method of the Tracking Code API to manage consent for your other tracking codes (i.e. GA). This would enable you to take full advantage of the HubSpot tracking code and related features, while still managing your consent for GA:


Hi @Derek_Gervais
Thanks for the reply.

Please see the attached screenshot for better understanding.

You will notice that after May (Once we start taking consent and make changes in trackign code) traffic dropped significantly. That means Hubpost is not showing all the traffic even visitor clicked on "I agreed" button.

Unfortunately I (and our dev) don't know how to implement your suggested solution. Is there any video or detailed article which can explain the above mentioned solution.

Kindly help us in implement it properly.



Hi @osamanaseem,

I don't have any tutorials or video walkthroughs, but the process can be broken down as follows:

  1. Call the 'Get privacy consent status' method mentioned above.
  2. Based on the response from the method above, either load your other tracking scripts as usual or otherwise opt the visitor out.
  3. The 'Get privacy consent status' method is available on all pages with the HubSpot tracking code, so you can use it anywhere your tracking code is present.

The reference doc above has more specific technical details on how exactly the 'Get privacy consent status' method works. You'll likely also find the following documentation helpful: