Skip to content

Instantly share code, notes, and snippets.

@Owanesh
Forked from rafaelstz/after-body-start.phtml
Last active December 17, 2020 14:30
Show Gist options
  • Save Owanesh/42685a0c4371ad43bef4e59f38a52b30 to your computer and use it in GitHub Desktop.
Save Owanesh/42685a0c4371ad43bef4e59f38a52b30 to your computer and use it in GitHub Desktop.
Facebook Pixel Code for Magento Stores
<?php
/* **** Pixel Facebook ****
app/design/frontend/<yourtheme>/default/template/page/html/head.phtml
Magento ver 1.9.1.0
content_ids can contains ID or SKU, what's on your facebook catalog?
*/
?>
<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', '<your_facebook_pixel_code>');
fbq('track', "PageView");
<?php
$request = $this->getRequest();
$module = $request->getModuleName();
$controller = $request->getControllerName();
$action = $request->getActionName();
$pageIdentifier = Mage::app()->getFrontController()->getAction()->getFullActionName();
/* VIEW CONTENT ACTION */
if(Mage::registry('current_product')) {
$product = Mage::registry('current_product');
$product_price_with_taxes = Mage::helper('tax')->getPrice($product, $product->getPrice());
$product_id = $product->getId();
$product_sku = $product->getSku();
$product_price = $product->getPrice();
$product_name = $product->getName();
echo "fbq('track', 'ViewContent', {
content_ids: ['".$product_sku."'],
content_type: 'product',
value: ".$product_price_with_taxes.",
content_name: '".$product_name."',
currency: 'EUR'
});";
}
/* SEARCH ACTION */
if($controller == 'result' || $controller =='advanced') {
echo "fbq('track', 'Search', {
search_string: '".$request->getQuery('q')."',
});";
}
// Customer registration
/*
if($module == 'customer' && $controller == 'account' && $action == 'index'){
echo "fbq('track', 'CompleteRegistration')";
}
*/
/* ADD TO CART ACTION */
if($module == 'checkout' && $controller == 'cart' && $action == 'index') {
$productID=Mage::getSingleton('checkout/session')->getLastAddedProductId(true);
$session= Mage::getSingleton('checkout/session');
foreach($session->getQuote()->getAllItems() as $item){
$productID = $item->getProductId();
$productSku = $item->getSku();
$productName = $item->getName();
$productQty = $item->getQty();
$price_with_taxes = Mage::helper('tax')->getPrice($item, $item->getPrice());
}
echo "fbq('track', 'AddToCart',{
content_ids: ['".$productSku."'],
content_type: 'product',
value: ".$price_with_taxes.",
content_name: '".$productName."',
currency: 'EUR'
});";
}
/* WISHLIST ACTION */
/*
if($module == 'wishlist') {
echo "fbq('track', 'AddToWishlist');";
}
*/
/* CHECKOUT START ACTION */
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');";
}
/* PURCHASE ACTION */
if (Mage::getURL('checkout/onepage/success/') == Mage::helper('core/url')->getCurrentUrl()){
$order = Mage::getModel('sales/order')->load(Mage::getSingleton('checkout/session')->getLastOrderId());
$grandTotal = $order->getGrandTotal();
$totalFormatted = number_format($grandTotal, 2, '.', ',');
$orderItems = $order->getItemsCollection();
$array_of_ids ="";
$array_of_names="";
$array_of_sku="";
foreach ($orderItems as $item){
$product_id = $item->product_id;
$product_sku = $item->getSku();
$product_name = $item->getName();
$array_of_ids = $array_of_ids. "'".$product_id."',"; //append id at id_list
$array_of_sku = $array_of_sku. "'".$product_sku."',";//append sku at sku_list
$array_of_names = $array_of_names. "'".$product_name."',";//append name at name_list
}
$array_of_ids=substr($array_of_ids, 0, -1);
$array_of_sku=substr($array_of_sku, 0, -1);
$array_of_names=substr($array_of_names, 0, -1);
echo " fbq('track', 'Purchase', {
value:".$totalFormatted.",
currency:'EUR',
content_type:'product',
num_items:".count($orderItems).",
content_name:[".$array_of_names."],
content_ids:[".$array_of_sku."]
})";
}
?>
</script>
<noscript><img height="1" width="1" style="display:none"
src=" https://www.facebook.com/tr?id=<your_facebook_pixel_code>&ev=PageView&noscript=1"
/></noscript>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment