Warning: Your browser is very out-of-date. You should upgrade to a better browser.

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

Magento July 2021

Magento 2 Blog Extensions

While Magento isn't the go-to platform for blogs it can be useful for store owners to have a quick and easy way to post updated...
Magento April 2021

Notes: Magento 2 Directory Structure

## Magento 1 A brief reminder of the Magento 1 folder structure: * **app/code** followed by either * **/core** for core files * **/community** for...
Magento March 2021

Magento 2.4.2

The latest version of [Magento](/magento-ecommerce) has been released - Magento version 2.4.2 is the usual mix of security updates, performance and some improvements to the...

More MagentoSEO Posts...

Related SEO Posts

Notes SEO June 2019

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...
Magento SEO November 2016

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....
Magento SEO November 2016

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...

More MagentoSEO Posts...