How to Add Facebook Pixel Code to Magento Success Page

Now a day’s, New Facebook pixel code is more intricate way to Web developers. From top of Google analytics  we get more javascript to drop into all pages where we can grow under like & share buttons, analytics tracking, advert tracking. Code helps to work website neatly.
For process of adding a Successful order tracking. Place .phtml for success

Now, open the file

app\design\frontend\PACKAGE\THEME\template\checkout\success.phtml

Facebook conversion tracking need the total order. Use the following php code to set last order to amount variable which was already in tracking code.

<?php
//Get Order Number & Order Total
$order = Mage::getModel('sales/order')->loadByIncrementId(Mage::getSingleton('checkout/session')->getLastRealOrderId());
$amount = number_format($order->getGrandTotal(),2);
?>

At last after adding Facebook conversion code. Replace 100000001 with value form of your facebook conversion code.  It appears twice in javascript and Image SRC. Make necessary changes but also it appears twice

<script type="text/javascript">
var fb_param = {};
fb_param.pixel_id = '100000001';
fb_param.value = '<?php echo $amount; ?>';
fb_param.currency = 'GBP';
(function(){
  var fpw = document.createElement('script');
  fpw.async = true;
  fpw.src = '//connect.facebook.net/en_US/fp.js';
  var ref = document.getElementsByTagName('script')[0];
  ref.parentNode.insertBefore(fpw, ref);
})();
</script>
<noscript><img height="1" width="1" alt="" style="display:none" src="https://www.facebook.com/offsite_event.php?id=100000001&amp;value=<?php echo $amount; ?>&amp;currency=GBP" /></noscript>


You can now go ahead and test to confirm. 

No comments:

Powered by Blogger.