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

86
Visualizações
Track Google Analytics from onclick and do not redirect

I want to track a click from a link. User clicks link and opens in new tab and GA tracks the event. The code I have opens in a new tab and tracks the events. However the first page is also redirect to the pdf. The first page should remain. Any ideas on how I can make this happen?

Here is my code.

js

function waitForLoad(id, callback){
    var timer = setInterval(function(){
        if(document.getElementById(id)){
            clearInterval(timer);
            callback();
        }
    }, 100);
}

    waitForLoad("trackbooks", function(){
    console.log("load successful, you can proceed!!");
    document.getElementById("trackbooks").onclick = function()
    {
       // alert("I got clicked");
        var that=this;_gaq.push(['_trackEvent','Download','PDF',this.href]);setTimeout(function(){location.href=that.href;},200); e.preventDefault(); ;return false; 
    }
});

html

<a id="trackbooks" class="fusion-button button-flat" rel="noopener noreferrer" href="http://testdomain.com/mycool.pdf" target="_blank">Download ></a>
about 4 years ago · Juan Pablo Isaza
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