APIs & Integrations

MichaelDev
Contributor | Diamond Partner
Contributor | Diamond Partner

Exclude traffic using API endpoint?

We're using the option to exclude traffic from reports & analytics tracking (as documented here) based on IP address for our employees.

 

The problem is, we're a remote based company with employees who use their own internet connection. That means, each employee has their own IP address which changes frequently (either due to their ISP rotating IPs or new IPs while traveling).

 

I'd like to automate the updating of the IP exclution list using a script but I can't find an API endpoint that can update the exclusion list. Does one exist?

 

If not, is there another way to identify traffic that shouldn't be tracked? For example, my JavaScript can identify employee traffic on our website. Maybe I could use JavaScript to tell _hsq to ignore those visits?

 

Thanks!

6 Replies 6
tjoyce
Recognized Expert | Elite Partner
Recognized Expert | Elite Partner

Exclude traffic using API endpoint?

@MichaelDev - I tried the usually suspects on finding hidden endpoints to no avail. I don't think you will have access to that endpoint for quite some time. 

That being said, if I had to "hack" a solution because this is a must-have by the marketing team. I would probably make a chrome extension that sets a variable on the window and then wrap the HS tracking codes into an if statement. Then, you could have your team install the chrome extension and browse freely.

 

Some things to note about the extension (If you have never made one before)

For your purposes, make sure you're using the "Content Script" approach, this will allow you to inject your own JS into any page on the web that you like. You can use wildcards to match your domain and only inject your window var on your site if you like or everywhere 

https://developer.chrome.com/docs/extensions/mv2/content_scripts/

 

Also, with your chrome extension, you don't have to publish it to the store, you can just ask your team to install from an offline version. 

 

Lastly, be mindful of the execution and callstack in your placement of the hsq embed code. The only issue I see you possibly running into, is that the hsq will execute before your chrome extension loads, so you might have to get creative with a solution for that. 

 

Let us know how it goes, if you head this direction

 

 

MichaelDev
Contributor | Diamond Partner
Contributor | Diamond Partner

Exclude traffic using API endpoint?

Thanks for the suggestion @tjoyce -

 

When you say "wrap the HS tracking codes into an if statement" which codes are you specifically referring to? Do you mean I should define _hsq and _hsp as my own objects before the injected hub spot code gets a chance to define it?

 

If that's what you mean, then should my objects define a push function that just throws away the values? Would that be enough to short circuit the tracking without breaking other aspects of the site?

 

Thanks!

0 Upvotes
tjoyce
Recognized Expert | Elite Partner
Recognized Expert | Elite Partner

Exclude traffic using API endpoint?

Theoretically (and that's a giant theoretical because I haven't tested) - 

instead of including the analytics script tag, you could include the contents of the file in your page and wrap it in an "if" statement

 

The HS embed code is a script loader, and I was thinking of, instead of using 

<script type='text/javascript' src='https://js.hs-scripts.com/[yourportalid].js?integration=WordPress' async defer id='hs-script-loader'></script>

 

you could do....

 

function HSScriptLoader(){
! function(t, e, r) {
    if (!document.getElementById(t)) {
        var n = document.createElement("script");
        for (var a in n.src="https://js.hsadspixel.net/fb.js", n.type = "text/javascript", n.id = t, r) r.hasOwnProperty(a) && n.setAttribute(a, r[a]);
        var i = document.getElementsByTagName("script")[0];
        i.parentNode.insertBefore(n, i)
    }
}("hs-ads-pixel-2660705", 0, {
    "data-ads-portal-id": 2660705,
    "data-ads-env": "prod",
    "data-loader": "hs-scriptloader",
    "data-hsjs-portal": 2660705,
    "data-hsjs-env": "prod",
    "data-hsjs-hublet": "na1"
});
var _hsp = window._hsp = window._hsp || [];
_hsp.push(['addEnabledFeatureGates', []]);
! function(t, e, r) {
    if (!document.getElementById(t)) {
        var n = document.createElement("script");
        for (var a in n.src="https://js.hs-banner.com/2660705.js", n.type = "text/javascript", n.id = t, r) r.hasOwnProperty(a) && n.setAttribute(a, r[a]);
        var i = document.getElementsByTagName("script")[0];
        i.parentNode.insertBefore(n, i)
    }
}("cookieBanner-2660705", 0, {
    "data-cookieconsent": "ignore",
    "data-loader": "hs-scriptloader",
    "data-hsjs-portal": 2660705,
    "data-hsjs-env": "prod",
    "data-hsjs-hublet": "na1"
});
! function(e, t) {
    if (!document.getElementById(e)) {
        var c = document.createElement("script");
        c.src="https://js.hs-analytics.net/analytics/1614634800000/2660705.js", c.type = "text/javascript", c.id = e;
        var n = document.getElementsByTagName("script")[0];
        n.parentNode.insertBefore(c, n)
    }
}("hs-analytics");
! function(t, e, r) {
    if (!document.getElementById(t)) {
        var n = document.createElement("script");
        for (var a in n.src="https://js.hsleadflows.net/leadflows.js", n.type = "text/javascript", n.id = t, r) r.hasOwnProperty(a) && n.setAttribute(a, r[a]);
        var i = document.getElementsByTagName("script")[0];
        i.parentNode.insertBefore(n, i)
    }
}("LeadFlows-2660705", 0, {
    "crossorigin": "anonymous",
    "data-leadin-portal-id": 2660705,
    "data-leadin-env": "prod",
    "data-loader": "hs-scriptloader",
    "data-hsjs-portal": 2660705,
    "data-hsjs-env": "prod",
    "data-hsjs-hublet": "na1"
});
}

if(window.chromeExtensionFlagSet){
  HSScriptLoader();
}

 

The js.hs-scripts.com script doesn't do any analytics tracking by itself. Instead, it loads the other embed codes relevant to your HubSpot subscription on your site.

https://knowledge.hubspot.com/reports/what-is-the-hs-scripts-embed-code-loading-on-my-website

MichaelDev
Contributor | Diamond Partner
Contributor | Diamond Partner

Exclude traffic using API endpoint?

Hi @dennisedson - Mike didn't have a chance to reply. Is there someone else who might be able to help us exclude internal traffic? Would be great if we could tell  _hsq to exclude the current page hit. Using that, I'd be able to have my own procedure that'll identify internal traffic and tell  _hsq to ignore it.

0 Upvotes
dennisedson
HubSpot Product Team
HubSpot Product Team

Exclude traffic using API endpoint?

@tjoyce ,

Think you could help out? 🙏

dennisedson
HubSpot Product Team
HubSpot Product Team

Exclude traffic using API endpoint?

Hey @MichaelDev ,

I do not believe there is a public endpoint for that, but I will ask around and confirm. 

The second option seems viable.

 

@Mike_Eastwood , have you thought about this before?