Conditional Facebook Conversion Tracking for WordPress

WordPress CMS logo

When you want to add conditional pixel event tracking / conversion tracking for your WordPress sites, the easiest option is actually to add it to WordPress template file called header.php

To locate header.php file you have 2 options, however, you should make sure that you are actually logged in to your web hosting account to locate header.php

Option 1 to Add the Facebook Conversion Tracking Code to WordPress

Web Hosting > File Manager > public_html > wp-content > themes > yourCurrentThemeName > header.php

When there, first right click and Download a backup copy. Then, Right click and choose Edit file

Locate the <head> portion before the closing of </head> portion and add any of the two sample codes below

Option 2 to Add the Facebook Conversion Tracking Code to WordPress

Although you could add the conditional conversion tracking code using the front-end of your website (its not suggested to follow this method because if something goes wrong, you’ll break your site).

Login to your WordPress site > Dashboard > Appearance > Editor > header.php (CTR+A copy what’s there and Paste it in Microsoft Notepad to keep a backup (this is important first step)

Sample Code to add and tweak IF you are using Contact Form 7 and want to track form submission

<!-- Facebook Pixel Code -->
<script>
!function(f,b,e,v,n,t,s){if(f.fbq)return;n=f.fbq=function(){n.callMethod?
n.callMethod.apply(n,arguments):n.queue.push(arguments)};if(!f._fbq)f._fbq=n;
n.push=n;n.loaded=!0;n.version='2.0';n.queue=[];t=b.createElement(e);t.async=!0;
t.src=v;s=b.getElementsByTagName(e)[0];s.parentNode.insertBefore(t,s)}(window,
document,'script','https://connect.facebook.net/en_US/fbevents.js');
fbq('init', '0000000000YourPixelCodeGoesHere0000');
fbq('track', "PageView");
<?php 
/*
PageView fires everywhere
Note: Pixel Helper may call out that you have multiple pixel events firing from the same page.
The Pixel Helper expects a page to fire a pixel event on load, but by tying events to elements, such as a button, you are using an alternative solution that overturns the expected behavior.
value: 5.99 can be changed according to your setup
 */
//this is the main domain front page
if (is_front_page()) : ?>
 fbq('track', 'ViewContent', {content_name: 'ChangeTheNameToAnything', content_category: 'Viewed', value: 5.99, currency: 'USD' });
<?php endif; ?>
<?php 
//all posts not paginated
if (is_single() && !is_paged()) : ?>
 fbq('track', 'ViewContent', { content_name: 'ChangeTheNameToAnythingblogPosting', content_category: 'Viewed', value: 5.99, currency: 'USD'});
<?php endif; ?>
<?php
//a particular blogPost
if (is_single('GivePermalinkOrIDofBlogPost')) : ?>
 fbq('track', 'Lead', { content_name: 'Courses', content_category: 'Viewed', value: 5.99, currency: 'USD'});
<?php endif; ?>
<?php 
//particular Page
if (is_page('GivePermalinkOrIDofPAGE')) : ?>
 fbq('track', 'Lead', {content_name: 'Plugins', content_category: 'Viewed', value: 5.99, currency: 'USD' });
<?php endif; ?>
<?php 
//track search
if ( is_search() ) : ?>
fbq('track', 'Search', {
 search_string: '<?php echo esc_html( get_search_query( false ) ); ?>'
 });
<?php endif; ?>
<?php
//contact form 7 (change the page name if its different like is_page('contact-us')
if( is_page('contact') ) : ?>
document.addEventListener( 'wpcf7submit', function( event ) {
 fbq('track', 'Lead', {content_name: 'ContactForm', content_category: 'Viewed', value: 5.99, currency: 'USD' });
}, false );
<?php endif; ?>
</script>
<!--leave this end script here as it ends all WordPress conditionals-->
<noscript>
<img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=0000000000YourPixelCodeGoesHere0000&ev=PageView&noscript=1" />
</noscript>
<!-- DO NOT MODIFY -->
<!-- End Facebook Pixel Code -->

Sample code to add WITHOUT Contact Form 7

<!-- Facebook Pixel Code -->
<script>
!function(f,b,e,v,n,t,s){if(f.fbq)return;n=f.fbq=function(){n.callMethod?
n.callMethod.apply(n,arguments):n.queue.push(arguments)};if(!f._fbq)f._fbq=n;
n.push=n;n.loaded=!0;n.version='2.0';n.queue=[];t=b.createElement(e);t.async=!0;
t.src=v;s=b.getElementsByTagName(e)[0];s.parentNode.insertBefore(t,s)}(window,
document,'script','https://connect.facebook.net/en_US/fbevents.js');
fbq('init', '0000000000YourPixelCodeGoesHere0000');
fbq('track', "PageView");
<?php 
/*
PageView fires everywhere
Note: Pixel Helper may call out that you have multiple pixel events firing from the same page.
The Pixel Helper expects a page to fire a pixel event on load, but by tying events to elements, such as a button, you are using an alternative solution that overturns the expected behavior.
value: 5.99 can be changed according to your setup
 */
//this is the main domain front page
if (is_front_page()) : ?>
 fbq('track', 'ViewContent', {content_name: 'ChangeTheNameToAnything', content_category: 'Viewed', value: 5.99, currency: 'USD' });
<?php endif; ?>
<?php 
//all posts not paginated
if (is_single() && !is_paged()) : ?>
 fbq('track', 'ViewContent', { content_name: 'ChangeTheNameToAnythingblogPosting', content_category: 'Viewed', value: 5.99, currency: 'USD'});
<?php endif; ?>
<?php
//a particular blogPost
if (is_single('GivePermalinkOrIDofBlogPost')) : ?>
 fbq('track', 'Lead', { content_name: 'Courses', content_category: 'Viewed', value: 5.99, currency: 'USD'});
<?php endif; ?>
<?php 
//particular Page
if (is_page('GivePermalinkOrIDofPAGE')) : ?>
 fbq('track', 'Lead', {content_name: 'Plugins', content_category: 'Viewed', value: 5.99, currency: 'USD' });
<?php endif; ?>
<?php 
//track search
if ( is_search() ) : ?>
fbq('track', 'Search', {
 search_string: '<?php echo esc_html( get_search_query( false ) ); ?>'
 });
<?php endif; ?>
</script>
<!--leave this end script here as it ends all WordPress conditionals-->
<noscript>
<img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=0000000000YourPixelCodeGoesHere0000&ev=PageView&noscript=1" />
</noscript>
<!-- DO NOT MODIFY -->
<!-- End Facebook Pixel Code -->

You can further customize these settings according to your own setup and business requirements. For example, if you wanted to track more pages, then it is just a matter of adding is_page(‘pageNameOrId’) just like the samples above.

What is Facebook Event Tracking & How to Install it to WordPress Site Video Lesson by RankYa

Create Custom Audiences & Create Conversion Campaigns

Head out to learn more about Custom Audiences and also Facebook Retargeting Campaigns using your facebook pixel because similar idea to Google Ads retargeting, you can Re-Show-Ads = Re-Target for more Facebook related easy to understand videos tutorials, check out Facebook Marketing Video Course

By RankYa

RankYa is a digital services provider dedicated to growing your sales and business website's results. Highly experienced technical problem solver, Google products expert with proven 'Social Media Marketing' skills, RankYa (100% Australian Owned and Operated) is dedicated to helping small businesses to grow.

We're looking forward to contributing towards your online success. Contact Us.

Questions? Leave a Comment!

Your email address will not be published. Required fields are marked *