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

156
Visualizações
Parse error in custom javascript variable GTM

How can i create new variable based on 2 separate current variable. The main condition is to check the existance of array or widget selector, and then return it's value:

    function() {
      var array = Array.prototype.slice.apply(document.querySelectorAll("div.widget-message.active.active-target a"));
      var length = array.length;
      var x = array.map(function(cn, index) {
        var nameId = cn.href.split("?")[0].split("/").slice(-1)[0].replace("-", " ");
        var urlParams = {{cjs - utility - url params}}(cn.href);
        var name = urlParams.promo_name;
        var creative = urlParams.promo_content;
        return {
          "name": "Widget - Personal - " + name,
          "id": "Widget - Personal - " + nameId + "-" + index,
          "position": "" + (index + 1) + "/" + length,
          "creative" : creative,
          "metric3": 1
        }
      });
      return {
        'ecommerce': {
          'promoView': {
            'promotions': x
          }
        }
      }
    }

and the second one is

    function() {
       var array = document.querySelector('[class="btn-new btn-fullwidth popup-trigger"]');
           return {
           'ecommerce': {
          'promoView': {
          "name": "Widget - Personal - " + array.dataset.targetId
          }
        }};
    }
about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Combining both functions can be done by checking array.length in the result of the first selector.

function() {
  
  var array = Array.prototype.slice.apply(document.querySelectorAll("div.widget-message.active.active-target a"));
  var x = array.map(function(cn, index) {
    var nameId = cn.href.split("?")[0].split("/").slice(-1)[0].replace("-", " ");
    var urlParams = {{cjs - utility - url params}}(cn.href);
    return {
      "name": "Widget - Personal - " + urlParams.promo_name,
      "id": "Widget - Personal - " + nameId + "-" + index,
      "position": "" + (index + 1) + "/" + array.length,
      "creative" : urlParams.promo_content,
      "metric3": 1
    }
  });

  if (array.length > 0) {
    return {
      'ecommerce': {
        'promoView': {
          'promotions': x
        }
      }
    }
  }
  
  array = document.querySelector('[class="btn-new btn-fullwidth popup-trigger"]');
  return {
    'ecommerce': {
      'promoView': {
        "name": "Widget - Personal - " + array.dataset.targetId
      }
    }
  };
}

See checking for array.length > 0. If that selector is found it will return the promotions using x. Otherwise the code continues and finds the other selector.

about 4 years ago · Santiago Trujillo 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