Eu Román
Eu Román

Reputation: 323

Facebook Pixel to track a click on a link

I want to track each time someone clicks on a link on my web (this click makes a file to be downloaded) to optimise my Facebook Ads (pay per conversion). After including the Facebook pixel in the head section of my web, I have to track this event, for example as a "lead", so I'm using this piece of code at the beginning of the body:

<script type="text/javascript">
$( '#link' ).click(function() {
fbq('track', 'Lead');
});
</script>

Please note that "link" is the id I've set for the link:

<a id="link" href="/content/file.zip">press here</a>

However, I'm tracking a "PageView" (because of the first code in the head section), but I'm not tracking the lead event (the click on the link).

I've also tried this one:

<script type="text/javascript">
$("#link").on('click', function() {
fbq('track', 'Lead');
});
</script>

And I've also tried with an onclick event in the link, like this:

<a href="#link" onclick="return formDownloaded();">press here</a>

<script type="text/javascript">
function fileDownloaded() {
fbq('track', 'Lead');
return true;
}
</script>

Nothing works for me (I've also put the code of the event at the end of the body section). What am I doing wrong?

Thanks a lot.

Upvotes: 2

Views: 11202

Answers (3)

Mas Niam
Mas Niam

Reputation: 1

I use this with image;

<a href="https://downloadurl">
<img src="target image" alt="xxx" onclick="fbq('track','Lead')"/>
</a>

this works if you have installed the fb pixel code in the website header.

Upvotes: 0

Matthew
Matthew

Reputation: 131

If you put the URL inside javascript, the link will be "dead" for any visitor that doesn't have scripts enabled in their browser.

You can just put fbq() inside a function inside separate script tags like so:

//pixel code above
  fbq('track', 'PageView');
</script>
<script>
  function Lead(){ fbq('track','Lead'); }
</script>
<body>
  <a href="#" onclick="Lead();">Click tracked by FB!</a>

Upvotes: 1

Eu Rom&#225;n
Eu Rom&#225;n

Reputation: 323

I've found a solution that works for me, in case is useful for someone else:

  1. Instead of putting the link inside an href, I've moved the link to the javascript function.
  2. I've used an onclick method to call the javascript function in which I first call the Facebook event to track, and then the download of the file starts.

The result is something like this for the HTML (the text I want to link):

<div onclick="fileDownloaded()">press here</div>

And something like this for the javascript function I want to track with Facebook pixel when someone clicks on the link (at the end of the body section):

<script>
function fileDownloaded() {
fbq('track', 'Lead');
window.open("/content/file.zip","_self")
}
</script>

Best regards.

Upvotes: 2

Related Questions