For cart page you can use the following piece of code:
<?php $quote = Mage::getSingleton('checkout/cart')->getQuote();
$productIds = "";
foreach($quote->getAllItems() as $item):
if($item->getParentItemId()) continue;
if (strlen($productIds)==0){
$productIds = "'".$item->getSku()."'";
}
else{
$productIds = $productIds.",'".$item->getSku()."'";
}
endforeach;?>
<script>
fbq('track', 'AddToCart', {
content_name: 'Shopping Cart',
content_ids: [<?php echo $productIds?>],
content_type: 'product',
value: <?php echo number_format($quote->getGrandTotal(),2,'.','');?>,
currency: '<?php echo Mage::app()->getStore()->getCurrentCurrencyCode();?>'
});
</script>
For purchase or order confirmation page you can use the following
piece of code -:
<?php $orderId = Mage::getSingleton('checkout/session')->getLastOrderId();
$order = Mage::getModel('sales/order')->load($orderId);
$productIds = "";
foreach($order->getAllItems() as $item):
if($item->getParentItemId()) continue;
if (strlen($productIds)==0){
$productIds = "'".$item->getSku()."'";
}
else{
$productIds = $productIds.",'".$item->getSku()."'";
}
endforeach;?>
<script>
fbq('track', 'Purchase', {
content_name: 'Order Confirmation',
content_ids: [<?php echo $productIds?>],
content_type: 'product',
value: <?php echo number_format($order->getGrandTotal(),2,'.','');?>,
currency: '<?php echo Mage::app()->getStore()->getCurrentCurrencyCode();?>'
});
</script>
Source:
Untested, but should work. If you are wanting all categories associated you'll need to loop over another collection of the products associated categories to build a list.
<?php $_category = Mage::registry('current_category'); ?>
<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', '<FB_PIXEL_ID>');
fbq('track', 'PageView');
fbq('track', 'ViewContent', {
content_type: 'product',
content_ids: ['<?php echo $this->getProduct()->getEntityId() ?>'],
content_name: '<?php echo $this->getProduct()->getName() ?>',
content_category: '<?php echo $_category->getName(); ?>',
value: <?php echo $this->getProduct()->getFinalPrice() ?>,
currency: '<?php echo Mage::app()->getStore()->getCurrentCurrencyCode(); ?>'
});
</script>