Knowledge Base

How to Implement Facebook Tracking Pixel in Magento – Part 3: Purchase

We’ve covered implementing Facebook Tracking Pixel in Magento – ViewContent, as well as implementing Facebook Tracking Pixel – AddToCart. In this section of our multi part guide to implementing the Facebook tracking pixel, we will cover how to track the ‘Purchase’ or checkout success event. After the user has successfully completed an order Magento will redirect the user to a “thank you” page. This is where we will focus on implementing the tracking pixel.

Facebook Tracking Pixel

The theme file responsible for this page is often found in a path like “app/design/frontend/<your package>/<your theme>/template/checkout/success.phtml”. This file will have an order variable already instantiated, but for the sake of completeness the code blocks below will assume that it does not.

 $orderId = $this->getOrderId();
 $order = Mage::getModel('sales/order')->loadByIncrementId($orderId);

 $visibleItems = $this->getOrder()->getAllVisibleItems();
 $contentIds = array();
 foreach ($visibleItems as $visibleItem)
     $contentIds[] = $visibleItem->getId();
 fbq('track', 'Purchase', {
   content_ids: [<?php echo "'" . implode("','", $contentIds) . "'" ?>],
   content_type: 'product',
   value: <?php echo $order()->getGrandTotal(); ?>,
   currency: '<?php echo Mage::app()->getStore()->getCurrentCurrencyCode(); ?>'

The code snippet above collects the product IDs of each visible item in the order, then submits the tracking pixel with the grand total of the order. If your store or catalog is built with configurable or grouped products, you will have to customize the snippet to submit what is most appropriate and potentially change ‘content_type’ to ‘product_group’.

Reader Interactions

Leave a Reply

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