2

Can somebody help me implemented this script? Any advice is appreciated. Thank You.

For product page:

     <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: ['1234'],    
        content_name: 'ABC Leather Sandal',
        content_category: 'Shoes',
        value: 0.50,
        currency: 'USD'
    });
    </script>
1

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>
  • I already did the cart page and success page, my issue is with product page ( viewcontent event). – Andrei Andronache Nov 16 '15 at 23:06
  • Sorry, Added an answer for the product detail page. – B00MER Nov 17 '15 at 0:45
  • It's not working. When i do this, my product page it's not loading. I get a blank page. – Andrei Andronache Nov 17 '15 at 15:32
  • 1
    Was my mistake, it's working everything but category name. – Andrei Andronache Nov 17 '15 at 19:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.