在外部链接上执行跟踪脚本

I am curious on the best way to execute a database query and set a cookie when a user clicks an external link, without having that script passed to the browser history so when the user goes back from the external site they will land on the same page they clicked the link and not the page that executes the tracking script.

I noticed google tracks on an external click, not sure how but I see there is a mousedown event that possibly changes the href location of the link to their tracking script?

How can I achieve a similar function?

Here is a solution for tracking clicks I posted earlier today:

counter for number of clicks on links in php