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

121
Visualizações
Eventlistener fires too many GA4 events

I used to have a eventlistener that looks something like this:

<script>document.querySelectorAll('.myclass').forEach(item => {
  item.addEventListener('click', event => {

gtag('event', 'phoneclick', {
  'event_category' : 'contact',
  'event_label' : 'phone'
});
});
})</script>

Somehow it fires the event two times. First I thought it was because the class was used more than once. But actually the class is only used for one element on the page.

I am new to this topic, so a beginners-guide would help me a big deal! Thank you very much.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

  1. Using gtag to send events is not the best practice. I would suggest getting rid of your whole event listener with whatever's inside and use GTM to properly track events.
  2. How did you check that the event fires twice? Show a network tab screenshot. As well as a dataLayer screenshot.
  3. Do some proper debugging: add a console.log to the callback, see if it fires twice too.
  4. Use preventDefault and stopPropagation. Also try looking for existing answers: Javascript click event firing twice, even with stopPropagation
about 4 years ago · Juan Pablo Isaza Relatório

0

If the element .myclass only has one at the page, maybe try add a event listener as this,

document.querySelector('.myclass').addEventListener("click", function() { 

gtag('event', 'phoneclick', {
  'event_category' : 'contact',
  'event_label' : 'phone'
});

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