Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

85
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!