Add Facebook's Custom Audience Pixel Code into Magento

by user28587   Last Updated February 01, 2018 11:09 AM - source

I need to add a Custom Audience Pixel Code for Facebook into my Magento site. The instructions say to paste this inbetween the tags, which is simple with basic websites but a lot more complex with Magento.

Any idea where to add this?

Answers 3

I would make a module - let's say Yourcompany_FbCode

app/code/local/Yourcompany/FbCode/etc/config.xml :

<?xml version="1.0"?>

app/design/frontend/base/default/layout/yourcompany_fbcode.xml :

<layout version="1.0.0">
        <reference name="before_body_end">
            <block type="core/template" name="fbpixel.block" after="-" template="yourcompany/fbcode/fbpixel.phtml">
                <action method="setCacheLifetime"><s>0</s></action>

app/design/frontend/base/default/template/yourcompany/fbcode/fbpixel.phtml :

(Your fb pixel code goes in here - this is the example code from fb)

<!-- Custom Audience Pixel Code -->
    // Insert Your Custom Audience Pixel ID below.
    fbq('init', '<FB_PIXEL_ID>');
    fbq('track', 'PageView');
<!-- Insert Your Custom Audience Pixel ID below. -->
<noscript><img height="1" width="1" style="display:none"
<!-- End Custom Audience Pixel Code -->

app/etc/modules/Yourcompany_FbCode.xml :

<?xml version="1.0"?>

app/code/local/Yourcompany/FbCode/Helper/Data.php :


class Yourcompany_FbCode_Helper_Data extends Mage_Core_Helper_Abstract {


And that's it - your pixel will be inserted into every page on the site, at the bottom to help with loading speed.

July 28, 2015 22:56 PM

It depends upon what do you want to achieve, there are several ways you can do it in Magento. Either you can create a custom module or copy paste the given pixel tracking code in head.phtml or footer.phtml. Both these methods will install your custom audience pixel tracking on your Magento site.

If you chose latter, just make sure you add this in your custom theme instead of base theme because base theme will be overwritten with your next Magento upgrade.

But bear in mind that custom facebook audience pixel tracking does come with standard and custom events which is far more work than adding pixel tracking code in head or footer section of your Magento site. For more information regarding these events use the following link

And if you are interested in implementing all the events, the below module can help you achieve this on your Magento store -:

Hope it helps

Cheers S

September 01, 2015 21:57 PM

I added the same code code to "Miscellaneous Scripts" in the admin panel system->Configuration->Design->HTML Head. I add this as an answer if some one can see this and explain is it a good practice.

March 10, 2016 09:04 AM

Related Questions

importing and exporting tracking

Updated January 25, 2018 06:09 AM

M2 Tracking progress via API

Updated December 03, 2017 08:09 AM

How to implement Custom Order Option in magento

Updated August 06, 2015 14:04 PM