Skip to content

Instantly share code, notes, and snippets.

@savio777
Last active July 29, 2025 21:37
Show Gist options
  • Save savio777/1e3d42d010dc891369e7b68ed122560b to your computer and use it in GitHub Desktop.
Save savio777/1e3d42d010dc891369e7b68ed122560b to your computer and use it in GitHub Desktop.
Resolução problema ao clicar em botão com evento google ads e analytics, navega para outra página sem fazer a conversão
  • executar disparo evento analytics e ads com timeout
<script>
  ['id-element-example','id-element-example-2'].forEach(function (id) {
    document.addEventListener('DOMContentLoaded', function() {
      const botao = document.getElementById(id);
      if (botao) {
        botao.addEventListener('click', function(event) {
          event.preventDefault();
          if (window.dataLayer) {
            dataLayer.push({'event': id});
          }
          setTimeout(function() {
            window.location.href = botao.href;
          }, 400); // 400ms de delay
        });
      }
    });
  });
  
</script>
  • iniciar o google tag manager após o timeout para otimizar o load da página
<script>
      window.addEventListener("DOMContentLoaded", function () {
        setTimeout(function () {
          (function (w, d, s, l, i) {
            w[l] = w[l] || [];
            w[l].push({ "gtm.start": new Date().getTime(), event: "gtm.js" });
            var f = d.getElementsByTagName(s)[0],
              j = d.createElement(s),
              dl = l != "dataLayer" ? "&l=" + l : "";
            j.async = true;
            j.src = "https://www.googletagmanager.com/gtm.js?id=" + i + dl;
            f.parentNode.insertBefore(j, f);
          })(window, document, "script", "dataLayer", "GTM-NPXTCQG9");
        }, 4000); // 4000ms = 4 segundos de atraso
      });
    </script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment