Apologies in advance for my ignorance. I've searched the site, but haven't had any luck.
Rather than manually enter each hostname via GA's Admin Interface, utilize the following JS function in GTM to defines the list of exclusions (var referrals), create/compare incoming {{Referrer}} (.exec, .test methods), and then null the {{Referrer}} if it's on the list, or lets it pass unmodified to GA if no match is found:
function()
{
var referrals = [
'domain_one.com',
'domain_two.com',
'domain_three.refer.com',
'store.domain_three.refer.com'
];
var hname = new RegExp('https?://([^/:]+)').exec({{Referrer}});
if (hname) {
for (var i = referrals.length; i--;) {
if (new RegExp(referrals[i] + '$').test(hname[1])) {
return null;
}
}
}
return {{Referrer}};
}
I sent the code to a developer for feedback, and he suggested replacing the for loop with with this (a direct replacement for the loop):
if (referrals.find(function(referral) { return hname[1].includes(referral); })) { return null; } else { return {{ Referrer }};
I attempted to do so like this:
function()
{
var referrals = [
'domain_one.com',
'domain_two.com',
'domain_three.refer.com',
'store.domain_three.refer.com'
];
var hname = new RegExp('https?://([^/:]+)').exec({{ Referrer }});
if (hname) {
if (referrals.find(function(referral) { return hname[1].includes(referral); })) { return null; } else { return {{ Referrer }};
}
When attempting to publish this in GTM, I'm getting both parsing errors as well as unreferenced variable errors for {{Referrer}}.
If anyone has some feedback, I'd be super super grateful.
Uh... your developer doesn't know GTM's syntax. So now for this to work, it's either the developer needs to know GTM's variable syntax or for you to know JS, so I suggest you to use your old code. It's better to use the code you understand than the code you won't be able to maintain.
If you still wanna use it, try removing spaces from the variable reference.
And you forgot to close the else claus: else { return {{ Referrer }};}
And one more time to close the external if... else { return {{ Referrer }};}}
And now it looks like a mess, so here, try this:
function() {
var referrals = [
'domain_one.com',
'domain_two.com',
'domain_three.refer.com',
'store.domain_three.refer.com'
];
if (/https?:\/\/([^\/:]+)/i.test({{Referrer}})) {
if (referrals.find(function (referral) { return hname[1].includes(referral); })) { return null; } else { return {{Referrer}}; }
}
}