Copyrights @ Journal 2014 - Designed By Templateism - SEO Plugin by MyBloggerLab

Friday, 29 January 2016

How to Add the Facebook Pixel Code to WordPress

Share
For this step, we will add the Facebook code to a variable, and then output it between the “head” HTML tags using a custom function and a WordPress hook (as opposed to editing any specific template files).
To do this, we will modify the code slightly. The code contains both double quotes and single quotes, so it’s cleaner if we choose one over the other. There are less double quotes, so we should change all double quotes in the code to single quotes. You should have pasted the code into a text file, so go ahead and modify those quotes now.
After changing double quotes to single, you should be left with something similar to below


<!-- 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','//connect.facebook.net/en_US/fbevents.js');

fbq('init', 'xxxxxxxxxxxxxxxxxxxxxxxxx');
fbq('track', 'PageView');</script>
<noscript><img height='1' width='1' style='display:none'
src='https://www.facebook.com/tr?id=xxxxxxxxxxxxxxxxxxxxxxxxx&ev=PageView&noscript=1'
/></noscript>
<!-- End Facebook Pixel Code -->

Now, below is the function that can be added to the functions.php template file of the child theme that you created in the above steps


add_action('wp_head','tthq_hook_fb_pixel');

function tthq_hook_fb_pixel() {
$output = "<!-- 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','//connect.facebook.net/en_US/fbevents.js');

fbq('init', 'xxxxxxxxxxxxxxxxxxxxxxxxx');
fbq('track', 'PageView');</script>
<noscript><img height='1' width='1' style='display:none'
src='https://www.facebook.com/tr?id=xxxxxxxxxxxxxxxxxxxxxxxxx&ev=PageView&noscript=1'
/></noscript>
<!-- End Facebook Pixel Code -->";
echo $output;
}


Of course, “xxxxxxxxxxxxxxxxxxxxxxxxx” in the above should be the number that is specific to your Facebook account. Plus, the functions may not be exact, because Facebook may have made some changes. So be sure to copy your code exactly from Facebook and modify just the quotes as explained above.
Now, once you re-save the functions.php file (of your child theme) with the above function/hook in place, the Facebook Pixel should be placed.
You can test this by visiting your web site, and clicking “View Source” from the context menu, and looking for the placement of the code within the “head” HTML tags. Beyond that you can go back into your Facebook account and click on the audience name to see if it says “Active” next to “Status” at the far right. You can also view the audience size, and any other relevant data, at any time within there as well. Once your audience reaches 30 people it will be eligible for advertising. Another way to test placement of the Facebook pixel code, if you’re a Chrome user, is to use this extension by Facebook.
Remember that people will eventually “fall off” the list, and since the list can have people on it from Day one (because Facebook could potentially add past visitors to it), it might at some point fall below the eligibility point for low-traffic web sites.
At any rate, it’s best to try and reach these visitors within a short while after they visited your web sites (and didn’t perform the desired action the first time around). So, get to advertising! If this is your first time advertising within Facebook, you might notice too that you can create custom audiences in many different ways, including bulk uploading a list of email addresses. If any of those email addresses match those of any logged in Facebook user (or Instagram), they are eligible to be advertised to. Exciting stuff!

















0 comments:

Post a Comment