Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Pass post id to Facebook's pixel code WordPress

  • SOLVED

Hi! I'm implementing Facebook dynamic product ads. When you add the ViewContent pixel to a product page, you need to insert that product’s specific ID manually or via a tag from the ecommerce platform. I'm having trouble getting that tag to work in my code. Specifically, I need to insert woocommerce (or wordpress) post id in the content_ids part. Any ideas?
<!-- 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', 'xxx'); fbq('track', 'PageView');
fbq('track', 'ViewContent',{ content_ids: 'post id goes here', <====== content_type: 'product'
});
</script> <noscript><img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=xxx&ev=PageView&noscript=1" /></noscript> <!-- End Facebook Pixel Code -->

Answers (2)

2015-07-22

Reigel Gallarde answers:

try
replace this

'post id goes here'

with

'<?php echo $post->ID; ?>'


bubiduk comments:

Thanks. Tried that already. The result is the pixel returning the php code as it is:

content_ids: <?php echo $post->ID; ?>


Reigel Gallarde comments:

where are you putting this script? php file?


bubiduk comments:

In the header.
Just wanted to add this. In the past I had succesfully implemented something somewhat similar.
Look at this https://www.social-response.co.uk/facebook-conversion-pixel-woocommerce-values/. See how they grab the order price and insert it in the script.
Now I need something like this but for the specific case stated in the original question.

2015-07-22

Andrea P answers:

if you are placing that code into a .js file or anywhere where you can't actually use php to retrieve the post id, you could try with this (dirty) hack.

put this code in your header. be sure it's below the wp_head();


<script>
function AP_get_id(){
var postID = <?php echo $post->ID; ?>;
return postID;
}
</script>



then in the other file, instead of
'post id goes here'

put:

AP_get_id()

without quotes.

not sure if it would work on your scenario, but I think it worth it to try. ;)


bubiduk comments:

Did not work. Trying this stopped the pixel from loading correctly.


Andrea P comments:

ah!
if you have to put this code in the header, then the Reigel code should've worked. if it didn't might be because you've placed it before the function
wp_head();

that code must be placed after that function, otherwise the $post global won't be ready.


bubiduk comments:

you are right, it works now. I was using the "custom code" thingy from the theme, now I tried directly in the header and made sure of the location.
Now, how do we do this? Do I award the money to Reigel as he provided the first solution or to Andra who specified where to put it?


Andrea P comments:

Riegel would have said the same if he was online when I posted my last reply.
give the prize to him ;)


bubiduk comments:

cool! Just voted, Thanks!!