I'm using magento 2 and I'm trying to add the facebook pixel code into it , what's the right file to add it ?

4 Answers 4


It has not changed much from Magento 1, I reckon you should use the app/code/Magento/Checkout/view/frontend/templates/success.phtml

NB: don't change that file directly as your changes will be overriden by Magento upgrades, override the template in your theme or module.

  • there isn't any file so I can have it on all the pages ?
    – alexcr
    May 11, 2016 at 15:01
  • @alexcr well isn't the point of the Facebook pixel to track the conversion when a sale is made ? May 11, 2016 at 15:03
  • yes that is right , than you ! but what if I need a js on every page ? and what if I need a js called only on the home page ?
    – alexcr
    May 11, 2016 at 15:08
  • @alexcr well that's a different question, please create a new question to address this particular issue ;) May 11, 2016 at 15:09
  • I've posted another question , here is the link magento.stackexchange.com/questions/114944/… thank you for your time !
    – alexcr
    May 11, 2016 at 15:19

You need to create your own module to add facebook pixel tracking. Here are few links which could help you create your own module -:

http://www.magento.scommerce-mage.co.uk/create-module-in-magento-2.html http://devdocs.magento.com/guides/v2.1/extension-dev-guide/build/create_component.html

Once you have created your module then you can add checkout_onepage_success.xml file under \view\frontend\layout with the following content -:

<?xml version="1.0"?>
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
        <referenceBlock name="checkout.success">
            <block class="CompanyName\ModuleName\Block\Checkout\Success" name="modulename.checkout.success" template="checkout/success.phtml"/>

Then add success.phtml file under \view\frontend\templates\checkout with the following content -:

$order = $this->getOrder();
$pixelValue     = number_format($order->getBaseGrandTotal(),2);
$pixelCurrency  = $order->getOrderCurrencyCode();

fbq('track', 'Purchase', {
  value: <?php echo $pixelValue;?>,
  currency: '<?php echo $pixelCurrency;?>'

Then add success.php file under **\CompanyName\ModuleName\Block\Checkout** with the following content -:

 * Copyright © 2015 Scommerce Mage. All rights reserved.
 * See COPYING.txt for license details.

// @codingStandardsIgnoreFile

namespace CompanyName\ModuleName\Block\Checkout;

 * FB Page Block
class Success extends \Magento\Framework\View\Element\Template
     * @var \Magento\Sales\Model\OrderFactory
    protected $_salesFactory;

     * Checkout session
     * @var \Magento\Checkout\Model\Session
    protected $_checkoutSession;

     * @param \Magento\Framework\View\Element\Template\Context $context
     * @param \Magento\Sales\Model\OrderFactory $salesOrderFactory
     * @param \Magento\Checkout\Model\Session $checkoutSession
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Sales\Model\Order $salesOrderFactory,
        \Magento\Checkout\Model\Session $checkoutSession,
        array $data = []
    ) {
        $this->_salesFactory = $salesOrderFactory;
        $this->_checkoutSession = $checkoutSession;
        parent::__construct($context, $data);

     * Retrieve current order
     * @return \Magento\Sales\Model\Order\OrderFactory
    public function getOrder()
        $orderId = $this->_checkoutSession->getLastOrderId();
        return $this->_salesFactory->load($orderId);

Hope the above information helps you set up facebook conversation tracking but if you are not a developer then you can buy the following extension.



It is the same as in version 1.0.


Use templates - Be ware any change can be overridden by an upgrade, hence don't change the file directly.

Look here as well: Install Facebook Pixel Tracking


There are 3 methods to add pixel code to your Magento 2 website:

Full reference for all methods can be found in this tutorial: https://magentip.com/add-facebook-pixel-magento-2/

Hope this helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.