Fire a Tracking Pixel onClick

I have this tracking pixel that would usually be placed in the body of a "installed successfully" page:

<img src="" width="1" height="1" border="0" />

But instead, I'd like it to fire when someone clicks a download button. Is this possible? I’m thinking the download button could call a javascript function onClick that would hit the adclick.php page?

Thanks James

0

4 Answers

Try the following:

<script>
  function fire() {
    var img = document.createElement("img");
    img.setAttribute("src", "");
    /* set other attributes here */
    document.body.appendChild(img);
  }
</script>
<button onClick="fire();">Click me</button>

This isn't much of a PHP question, but at least you're tagging.

<script src="//"></script>
<script type="text/javascript"> 
function iteration() { 
$.get("");
return false;
}
</script>
<input type="button" value="Tracking Text" onclick="iteration();" />

Another possible solution, if you are using jQuery:

<script type="text/javascript">
function downloadButtonClicked() {
    $('<img src="" width="1" height="1" border="0" />').appendTo('body');
}
</script>
<a href="/download" onclick="downloadButtonClicked();">Download</a>

Just create an image with

var trackingPxl = new Image();

and inside your download function set the src

trackingPxl.src = '

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

James H. Sterling

James H. Sterling

Environmental Science & Climate Journalist

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.

Share this article
Twitter Facebook Pinterest