Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

157
Views
Javascript - replace loop with native js code problem for Google Tag Manager

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.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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}}; }
  }
}

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!