Simple single script to add Facebook’s Pixel Code tracking to your Magento ecommerce website. The code should be added into the head.phtml file within your template. This includes tracking of search and checkout progress and new orders.

Simply replace all the $facebookPixelId with your actual Facebook Pixel Code ID.

<!-- All in one Facebook Pixel Code --> 
<?php $facebookPixelId = "000000000000000000000"; // Your Pixel Code goes here ?>
<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', '<?php echo $facebookPixelId; ?>'); 
 fbq('track', "PageView");
<?php 
 $request = $this->getRequest();
 $module = $request->getModuleName();
 $controller = $request->getControllerName();
 $action = $request->getActionName();
 $pageIdentifier = Mage::app()->getFrontController()->getAction()->getFullActionName(); 
 // Lead
 if(Mage::registry('current_product')) {
 echo "fbq('track', 'Lead');";
 }
 // Search
 if($controller == 'result' || $controller =='advanced') { 
 echo "fbq('track', 'Search');"; 
 }
 // Customer registration
 if($module == 'customer' && $controller == 'account' && $action == 'index'){
 echo "fbq('track', 'CompleteRegistration')";
 }
 // Add to cart
 if($module == 'checkout' && $controller == 'cart' && $action == 'index') {
 echo "fbq('track', 'AddToCart');";
 }
 // Add to wishlist
 if($module == 'wishlist') {
 echo "fbq('track', 'AddToWishlist');";
 }
 // Checkout start 
 if(Mage::getURL('checkout/onepage') == Mage::helper('core/url')->getCurrentUrl()) {
 echo "fbq('track', 'InitiateCheckout');";
 
 // As the onepagecheckout don't have a specific page for add billing info, i placed the tracker here
 echo "fbq('track', 'AddPaymentInfo');";
 }
 // Checkout success
 if ($pageIdentifier == 'transparente_standard_redirect') {
 $lastOrderId = Mage::getSingleton('checkout/session')->getLastOrderId();//Geeting last order id
 $order = Mage::getSingleton('sales/order'); //getting order model
 $order->load($lastOrderId); //loding current order
 $_totalData =$order->getData(); //loading order model data
 $grandTotal = number_format($_totalData['grand_total'],2,'.',''); //loading grand total
 echo "fbq('track', 'Purchase', {value: '$grandTotal', currency: 'BRL'});";
 }
 ?>
</script> 
<noscript><img height="1" width="1" style="display:none" 
src=" https://www.facebook.com/tr?id=<?php echo $facebookPixelId; ?>&ev=PageView&noscript=1" 
/></noscript> 
<!-- All in one End Facebook Pixel Code -->

Magento

Magento is a feature-rich ecommerce platform built on open-source technology that provides online merchants with unprecedented flexibility and control over the look, content and functionality of their ecommerce store.

About Magento Ecommerce · Magento Posts

Related Magento Posts

Aheadworks switch to subscription model

Aheadworks will be removing Composer keys and download access for existing customers and switching entirely to a subscription model. From February 2022 Aheadworks announced it... March 2022 · Magento

Magento Store Hierarchy

Before you start building-out your Magneto website you must first plan your store hierarchy. Magento's hierarchy is referred to throughout your store's configuration. Each store... February 2022 · Magento

Optimising Magento 2 in Easy Steps

Some (fairly) simple step-by-step instructions to ensure your Magento 2 store is running the best it can. While most of these options should only require... December 2021 · Magento

More MagentoSEO Posts...

Related SEO Posts

Remove outdated content from Google Search Results

From time to time unwanted pages can appear in Google Search Results. Could be a temporary page, old content or even a test page that's... June 2019 · Notes · SEO

All-in-one Google Analytics tracking in Magento

Simple single script to add Google’s conversion tracking to your Magento ecommerce website. The code should be added into the footer.phtml file within your current template.... November 2016 · Magento · SEO

All-in-one Facebook Pixel Code for Magento ecommerce

Simple single script to add Facebook’s Pixel Code tracking to your Magento ecommerce website. The code should be added into the head.phtml file within your... November 2016 · Magento · SEO

More MagentoSEO Posts...