Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

148
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda