This post was published in 2016 and may contain information, techniques or code examples that are no longer current. Please double-check official documentation and modern best practices before using anything from this article.
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 -->More Magento posts
—
Magento database structure
The database structure of Magento is designed to store and manage various aspects of an e-commerce website, including products, orders, customers, and more. Understanding the Magento database structure is crucial…
—
Accessibility in ecommerce websites
In the digital age, where online shopping has become an integral part of our lives, ecommerce websites hold the key to business success. Amidst the rush to create visually appealing…
—
Using Magento to get small businesses online
For small businesses aiming to establish a formidable online presence and drive sales, Magento emerges as a powerful e-commerce platform offering a suite of features tailored to their needs. Scalability…
Continue reading "Using Magento to get small businesses online"
—
How can I speed-up my Magento website?
Speed optimisation is crucial for a Magento website as it directly impacts user experience, conversions, and search engine rankings. You should prepare your server so it is set-up to use…
More SEO posts
—
Building cookieless tracking without Google Analytics
The era of ubiquitous third-party cookies is drawing to a close, accelerated by stringent regulations like the GDPR in Europe and the CCPA in California, alongside browser initiatives such as…
Continue reading "Building cookieless tracking without Google Analytics "
—
Structured data for SEO
Adding structured data to a website is one of those steps that often gets overlooked, yet it can make a real difference to how a site performs in search engines.…
—
Simple Analytics: A privacy-focused alternative to Google Analytics
In an era where digital privacy concerns are at the forefront of online discourse, many organisations are reassessing their tools to ensure compliance with data protection laws and maintain user…
Continue reading "Simple Analytics: A privacy-focused alternative to Google Analytics"
—
Cookieless website tracking and analytics
Cookieless website tracking is a method of collecting analytics data and monitoring website behaviour without the need for traditional browser cookies. Traditionally, cookies have been a key component in tracking…
Continue reading "Cookieless website tracking and analytics"