Skip to content

Instantly share code, notes, and snippets.

@rafaelstz
Created March 10, 2016 16:07
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save rafaelstz/4dcaecae4318eb552b48 to your computer and use it in GitHub Desktop.
Save rafaelstz/4dcaecae4318eb552b48 to your computer and use it in GitHub Desktop.
Facebook Pixel Code for Magento Stores
<!-- Facebook Pixel Code -->
<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', 'Put your pixel ID here');
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=282024181986351&ev=PageView&noscript=1"
/></noscript>
<!-- End Facebook Pixel Code -->
<!-- If you dont have an after body start yet, its time to create one -->
<reference name="after_body_start">
<block type="core/template" name="after_body_start_script" template="page/html/after-body-start.phtml" />
</reference>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment