Add a Facebook Tracking Pixel in WooCommerce Checkout

In this quick code snippet, we’ll demonstrate how you can add a Facebook tracking pixel in WooCommerce “Thank You” pages of your WooCommerce store, giving you new insight on how well your site converts Facebook users and a nice piece of information for your future marketing efforts.

What we’ll be doing is adding a custom function, so it would be worth taking a look into the article How to Safely Add Code to your Theme’s functions.php File. Additionally, you could use a great (free) plugin just for this sort of situation – My Custom Functions.

The Code

You will want to copy the entire code below, except for the beginning <?php on line 1.

Paste it into your custom code field, then head to your Facebook administration to grab the tracking pixel you’ve already generated. Enter it in between the notated lines.


That’s all there is to it! Now, the pixel will only fire once a user gets to the order confirmation “Thank You” page. Quick, easy, and handy – the best kind of code.


Have any questions or comments about this article, or ways you think it can be improved?
Join the conversation in the comments below, or sign up for my newsletter to recieve periodic updates!

Tags: , , ,

5 Responses to “Add a Facebook Tracking Pixel in WooCommerce Checkout”

  1. Noah Landsberg November 18, 2017 at 13:31 EST (1:31 PM) #

    Hey Daniel,

    Would I be able to use this code, add and track multiple pixels for other campaigns I’m running?

    Cheers,
    Noah

    • Danny Santoro November 20, 2017 at 12:41 EST (12:41 PM) #

      Hey Noah!

      You should be able to add any tracking code there that will apply to the Thank You page, so if you had a Google tracker for example then it can be added the same way. If you have multiple campaigns then that’s a little different – you’d need to set it up in your analytics tool to be more precise to see how the user came to the site with landing pages or something along those lines.

      I hope this helps/ 🙂

  2. bdmmoreira December 11, 2017 at 18:06 EST (6:06 PM) #

    Hi, Daniel. And if I want to only fire the pixel when A specific payment method is used?

    • Danny Santoro December 12, 2017 at 10:36 EST (10:36 AM) #

      That would be a little trickier. If you go to the Thank You template file here: you’ll see that there are conditional rules. On line 78, you can see that it uses get_payment_method. You could use that to define a PHP variable and then create if/else statements depending on what gateways you have enabled and how many different pixels you wanted to include.

      It’s likely not something I can whip up and put into a comment here, but if you’d like then reach out to me on my contact page then I’ll either be able to look into the customization or find someone who would be able to code that out for you. 🙂

  3. Thiago May 4, 2018 at 16:50 EDT (4:50 PM) #

    Works like a charm with divi theme + woocommerce 3.xx. Thanks for sharing.

Leave a Reply to Noah Landsberg Cancel reply

%d bloggers like this: