Skip to content

Instantly share code, notes, and snippets.

@savio777
Last active October 4, 2025 07:14
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>

exemplo preload de imagens e cdn

<!DOCTYPE html>
<html lang="pt-BR">
  <head>
    <!-- preload imagem em img ou bg img css -->
    <link
      href="https://s3.automacoesbeo.xyz/logos-empresas/logo-branco-eboliveirabhorizontal-small.webp"
      rel="preload"
      as="image"
      fetchpriority="high"
    />
    <link
      href="https://s3.automacoesbeo.xyz/landpage-images/divorcio1%2Fbruna_santos_horizontal.webp"
      rel="preload"
      as="image"
      fetchpriority="high"
    />
    <!-- preload cdn -->
        <link
      rel="preload"
      href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css"
      as="style"
      onload="this.onload=null;this.rel='stylesheet'"
    />
    <noscript>
      <link
      rel="stylesheet"
      href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css"
      />
    </noscript>
    <!-- Google Tag Manager lazy load -->
    <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-SEU_ID");
        }, 4000); // 4000ms = 4 segundos de atraso
      });
    </script>
  </head>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment