How to insert code into specific pages for FB pixel event tracking

Hi,

I have already installed X Child theme and added FB pixel code in the functions.php file which seems to be working fine.

I also need to insert code into specific pages of the site to allow for event tracking. Please could you let me know where I would input this code for individual pages.

Thank you

Naomi

Hi Naomi,

Thank you for writing in! Please add this on your child theme functions.php file.

/*Pixel Event Code*/
function fb_print_pixel_event(){
 if (is_page(46) ) : ?>

<!-- FB EVENT CODE HERE -->

 <?php endif; }

 add_action( 'wp_head', 'fb_print_pixel_event' );

On the line: if (is_page(46) ) : ?> replace the 46 with the ID of your specific page. Refer here on how you can locate the page ID.

Make sure to add this pixel event code function below the base pixel code. Because base code must fire before the event code.

Facebook Pixel Implementation Guide

Hope it helps,
Cheers!

1 Like

Hi, I’m having a similar issue… Can we not just insert the pixel code into the CSS for each page? I’m not a developer so excuse me for my ignorance. Or does it have to go into the functions.php file to be inserted into the header?

Here’s what I’m using, but with the Facebook Pixel helper it’s showing that there’s an error in the code…

/Pixel Event Code/
function fb_print_pixel_event(){
if (is_page(202) ) : ?>

<?php endif; } add_action( 'wp_head', 'fb_print_pixel_event' );

Ok disregard request. I was able to bypass messing with the functions.php file by using a plugin (after testing several made for Facebook pixels) called, Wordpress HiFi.

It allows you to insert head and footer code injections for each page, not the whole site, which is what I wanted. PERFECTION. The app rocks in it’s simplicity.

Whichever works for you we’re glad you sorted things out.

Cheers!