Knowledge Base

How to Implement Facebook Tracking Pixel in Magento – Part 2: AddToCart

In Part 1 of this series, we covered the basics of implementing tracking pixel for a Magento store. In this section of our multi part guide to implementing the Facebook tracking pixel, we will cover how to track the add to cart action. This will require a basic understanding of javascript, and potentially, observers and events in Magento. Your Magento theme may handle the add to cart action in one of two common ways: either via an AJAX request or requiring the user load the view cart page. If your Magento site does require the user to load the view cart page, I would strongly suggest creating your own Magento module so that you can separate your code from your theme’s code. However, creating your own custom Magento module will not be discussed in this guide.

Facebook Tracking Pixel

In Part 1 we discussed customizing the javascript snippets depending on how your catalog information is being submitted to Facebook, as well as how your products are configured within Magento (simple vs configurable products). The code snippets below will cover implementation for a catalog and store that is composed of simple products only.

Facebook Tracking Pixel: AJAX Add to Cart

If your Magento theme handles the add to cart action with AJAX requests, where the user remains on the product page, then a javascript snippet will need to be added to the product page. This will be easiest to implement by altering the product view template. The snippet would need to look similar to the code below.

<script>
 if (typeof jQuery !== 'undefined') {
   jQuery('addToCartSelector').click(function () {
     fbq('track', 'AddToCart', {
       content_ids: '<?php echo $_product->getId(); ?>',
       content_type: 'product',
       value: <?php echo $_product->getPrice(); ?>,
       currency: '<?php echo Mage::app()->getStore()->getCurrentCurrencyCode(); ?>'
     });
   });
 }
</script>

The snippet above assumes that the Facebook tracking pixel library has been initialized (“fbq”) and that there is a HTML element on the page that will be clicked to add a product to cart. In order for the click event to be associated with the HTML element the jQuery select “addToCartSelector” will need to be customized to your site or theme. A common select for many themes is “div.add-to-cart > .btn-cart”. When the add to cart button is clicked and this javascript is loaded, the Facebook tracking pixel library will push the product’s ID and price.

View Cart Redirect

Instead, if your Magento theme requires the user to load the view cart page after adding a product to cart, then you will have to implement an observer to temporarily store the product in a session variable as well as add a javascript snippet to the view cart page. After a product has successfully been added to a user’s cart Magento will dispatch the “checkout_cart_add_product_complete” event. This is the event that the observer will have to be associated with in order to capture which product was added, which will require you to add to or add an events section your theme’s appropriate “config.xml” file as well as add an observer class or a method to an existing observer.

Adding the event to the “config.xml” file will look similar to the XML below.

 <frontend>
     <events>
         <checkout_cart_add_product_complete>
             <observers>
                 <yourPackage_yourTheme_yourObserverMethod>
                     <class>yourPackage_yourTheme/observer</class>
                     <method>yourObserverMethod</method>
                 </yourPackage_yourTheme_yourObserverMethod>
             </observers>
         </checkout_cart_add_product_complete>
     </events>
 </frontend>

Within an observer or new observer class, which would be typically be found or created at a path like “app/code/local/<your package>/<your theme>/Model/Observer.php”, you would then add a method that temporarily stores the product that was added to cart in a Magento session variable. Below is an example of a such a method.

public function yourObserverMethod()
{
 $product = Mage::getModel('catalog/product')->load(Mage::app()->getRequest()->getParam('product', 0));

 $varienObject = new Varien_Object(array(
   'id' => $product->getId(),
   'sku' => $product->getSku(),
   'price' => $product->getPrice(),
 ));

 Mage::getModel('core/session')->setProductToShoppingCart($varienObject);
 
return $this;
}

Finally, on the view cart page you will need to add a section of code that will access the session variable and complete the javascript tracking snippet wit the appropriate information. Below is an example of this code using the session variable “ProductToShoppingCart” which was set in the snippet above.

<?php $product = Mage::getModel('core/session')->getProductToShoppingCart();
?>
<script>
 fbq('track', 'AddToCart', {
   content_ids: '<?php echo $product->getId(); ?>',
   content_type: 'product',
   value: <?php echo $product->getPrice(); ?>,
   currency: '<?php echo Mage::app()->getStore()->getCurrentCurrencyCode(); ?>'
 });
</script>

In the next section of this guide, we will discuss Facebook tracking pixel and the ‘Purchase’ event.

Reader Interactions

Leave a Reply

Your email address will not be published. Required fields are marked *