Magento 1.9 – Adding New Facebook Pixel to

headjavascriptmagento-1.9

Trying to add the new Facebook pixel to Magento 1.9.1.1. So it can appear on all pages.

I have added the following to app/design/frontend/rwd/CustomTheme/layout/local.xml between

<?xml version="1.0"?>
  <layout version="0.1.0">
    <default>
      <reference name="head">
        <action method="addItem"><type>skin_js</type><name>js/facebook.js</name></action>
      </reference>
    </default>
  </layout>

also tried

<reference name="head">
  <action method="addJs"><script>js/facebook.js</script></action>
</reference>

and have added the following NEW Facebook pixel to js/facebook.js

<!-- 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', '1111111');
fbq('track', "PageView");</script>
<noscript><img height="1" width="1" style="display:none"
src="https://www.facebook.com/tr?id=1111111&ev=PageView&noscript=1"
/></noscript>
<!-- End Facebook Pixel Code -->

It fails to load the Facebook pixel in my source code? What have done wrong?

How do you add any script to the head area otherwise?

Best Answer

I would look at using google tag manager to handle this so when the script changes or you want to add more scripts to the site its all done in the one place.

If you still want to add the script yourself you should not use a js file and create a block instead:

<?xml version="1.0"?>
  <layout version="0.1.0">
    <default>
      <reference name="content">
        <block type="core/template" name="facebook.pixel" as="facebook.pixel" template="page/facebook-pixel.phtml" />
      </reference>
    </default>
  </layout>

and then in skin/frontend/{package}/{theme}/js/facebook.js add your js code.

Related Topic