How to Track Button Click With Facebook Pixel?


You might have been stuck in a situation where you need to track some events that need some Javascript or basic coding knowledge. So today, I want to tell you how we can track "Button Clicks" with Facebook Pixel code.

Scenario - I have a submission form on my website's home page & whenever someone fills up that form & click on the "Submit" button, the URL is not changing. But I want to track the number of submissions I got or how many users clicked on that "Submit" button as I don't have access to Website's backend.

How we can do that?

First of all, we need to make sure that we have a basic pixel code installed on the homepage. It looks something like this:

  1. <script>
  2.   !function(f,b,e,v,n,t,s)
  3.   {if(f.fbq)return;n=f.fbq=function(){n.callMethod?
  4.   n.callMethod.apply(n,arguments):n.queue.push(arguments)};
  5.   if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';
  6.   n.queue=[];t=b.createElement(e);t.async=!0;
  7.   t.src=v;s=b.getElementsByTagName(e)[0];
  8.   s.parentNode.insertBefore(t,s)}(window, document,'script',
  9.   'https://connect.facebook.net/en_US/fbevents.js');
  10.   fbq('init', '246611512878205');
  11.   fbq('track', 'PageView');
  12. </script>
  13. <noscript><img height="1" width="1" style="display:none"
  14.   src="https://www.facebook.com/tr?id=246611512878205&ev=PageView&noscript=1"
  15. /></noscript>
If it is already installed, then we need to put a simple piece of code on the button click. Here is the code:
<button onClick="fbq('track', 'Purchase');">Button Text</button>
Just change the button text with your button's text & then you will be all good!

Post a Comment

0 Comments