July 6, 2017

How to Integrate Facebook Pixel Events to your Magento Store

Today We will see how you can integrate your facebook pixel correctly to your Magento Ecommerce Store.

You can Paste Following code to your Mangeto Store’s theme File and you will be able to Integrage Following Facebook Events at right place.
1.PageView
2.ViewContent
3.AddToCart
4.InitiateCheckout
5.Purchase
6.Search
7.Complete Registration
8.AddToWishlist

First Please go to the header.phtml of your current theme..
Here is general path of header.phtml file

/public_html/app/design/frontend/ultimo/default/template/page/html

Now paste the following code.


  <?php $request = $this->getRequest();
$module = $request->getModuleName();
$controller = $request->getControllerName();
$action = $request->getActionName();
$pageIdentifier = Mage::app()->getFrontController()->getAction()->getFullActionName(); 
?>
 
<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');
 // Insert Your Facebook Pixel ID below. 
 fbq('init', 'YOUR-PIXEL-ID');
 fbq('track','PageView');
</script>
  
 <?php if($controller == 'result' || $controller =='advanced') :? >
    <script> fbq('track', 'Search')</script>  
 <?php endif; ?>
 
 <?php if($module == 'customer' && $controller == 'account' && $action == 'index'):>
    <script>fbq('track', 'CompleteRegistration')</script> 
 <?php endif; ?> 
 <?php if($module == 'wishlist') : ?>
     <script>fbq('track', 'AddToWishlist')</script>
<php endif; ?>
 <? $currentUrl = Mage::helper('core/url')->getCurrentUrl();
       $url = Mage::getSingleton('core/url')->parseUrl($currentUrl);
       $path = $url->getPath();
 ?>
  
 <?php if($path == '/default/securecheckout/') : ?>
     <script>fbq('track', 'InitiateCheckout')</script>
<?php endif; ?>
 
 
 <?php if($path == '/success.html') : ?>
   <script>fbq('track', 'Purchase', {value: '0', currency: 'USD'})</script>;
 <?php endif ?>
   

Now For facebook ViewContent event to your mangeto store with Specific product values
go to /public_html/app/design/frontend/ultimo/default/template/catalog/product

Now open file View.phtml

Now paste following code at bottom


<script>
fbq('track', 'ViewContent', { content_type: 'product',content_ids: ['<?php echo $_product->getId() ?>'],content_name: '<?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?>',value:0,
currency: '<?php echo Mage::app()->getStore()->getCurrentCurrencyCode(); ?>' });

</script>

Now For facebook AddToCart event to your mangeto store with Specific product values

Go to /public_html/app/design/frontend/ultimo/default/template/checkout

Now Open Cart.phtml
Now paste following code at bottom


<?php $quote = Mage::getSingleton('checkout/cart')->getQuote();<br ?> $productIds = "";
foreach($quote->getAllItems() as $item):
if($item->getParentItemId()) continue;
if (strlen($productIds)==0){
$productIds = "'".$item->getId()."'";
}
else{
$productIds = $productIds.",'".$item->getId()."'";
}
endforeach;?>
<script>
fbq('track', 'AddToCart', {
content_name: 'Cart',
content_ids: [],
content_type: 'product',
value: <?php echo number_format($quote->getGrandTotal(),2,'.','');?>,
currency: '<?php echo Mage::app()->getStore()->getCurrentCurrencyCode();?>'
});
</script>

Please paste the code as it is with comments only replace your pixel id.
If you are finding any issues and you want me to do on behalf of it..
Please contact me on

Leave a Reply

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