How to Exclude and Block ISP and Bots in Google Analytics
Google Analytics 4 has extremely limited possibilities to exclude and block visitors and bots unfortunately.
Through our IP API you can block visitors and bots by any of the data returned by our IP API, such as by Country, by ISP, by Timezone etc.
Below you'll find the code and filter options, see the eiplvars options at the top of the code to select exactly what you want blocked. You can also alter the code and change it within the code.
By default the 'exclude_big_clouds' option is used, this excludes all visits from the three big clouds, Amazon Google and Microsoft.
Excluding and Blocking in this case means; Google Analytics will be blocked, it will not run, if the code finds anything that matches the filters as setup by you.
Best is to add the code to the source of your pages before the Google Tag Manager code.
It's possible to add the code within GTM as well as a custom HTML tag, depending the GTM setup this may work great or not (it depends on when our code runs vs the Google Analytics tag).
<script>
var eiplvars=[];
//eiplvars.push({'exclude': 'Microsoft'}); //excludes if Microsoft matches in any of the IP data
//eiplvars.push({'exclude': 'Amazon', 'where': 'isp'}); //excludes if Amazon matches in the ISP data
//eiplvars.push({'exclude': 'Asia', 'where': 'timezone'}); //excludes if Asia matches in the Timezone data
eiplvars.push('exclude_big_clouds'); //excludes Amazon, Google Cloud, Microsoft Azure bot visits
//If the following doesn't work anymore your website is probably blocked because of usage, get the Pro/Ultra Package at extreme-ip-lookup.com
function GA4EiplExclude(json) {
if (typeof json === 'object' && typeof json.isp === 'string') {
var block=0;
for (var i=0; i < eiplvars.length; ++i) {
if (typeof eiplvars[0] === 'string') {
if (eiplvars[0] === 'exclude_big_clouds') {
if ( /Amazon|Google LLC|Microsoft Corporation/.test(json.isp) ) {
console.log('excluding because contains exclude_big_clouds '+json.isp);
block=1;
}
}
}
else if (typeof eiplvars[0] === 'object') {
if (typeof eiplvars[0].exclude === 'string' && typeof eiplvars[0].what === 'string') {
if (typeof json[eiplvars[0].what] === 'string') {
if (json[eiplvars[0].what].indexOf(eiplvars[0].exclude) > -1 ) {
console.log('excluding because '+eiplvars[0].what+' contains '+eiplvars[0].exclude+'');
block=1;
}
}
}
else if (typeof eiplvars[0].exclude === 'string') {
for (var n in json) {
if (json[n].indexOf(eiplvars[0].exclude) > -1) {
console.log('excluding because '+n+' contains '+eiplvars[0].exclude+'');
block=1;
}
}
}
}
}
if (block === 1) {
window.dataLayer = window.dataLayer || [];
dataLayer.push({'gtm.blocklist': ['gaawe', 'gaawc']});
window["_gaUserPrefs"] = { ioo : function() { return true; } }
}
}
}
</script>
<script src="//extreme-ip-lookup.com/json/?callback=GA4EiplExclude&key=demo" async defer></script>