I have an interest in loading Facebook Pixel/GTM in an external JS file. Why? I’m dealing with a large HTML site with a lot of pages, and I’d rather create one simple file that has the code just in case the company decides to change the script. It’s better than going to every…single…page to change the code. However, when I attempt to do this, I’m unable to see any tracking on the web page. It was mentioned that it can be caused by the <script>
and <noscript>
tags within the external file by FB and GTM.
When I insert the path <script src="foo/foo.js" /></script>
, there’s no errors posting in the Chrome Console. The path is correct and yet with GTM and Facebook, there’s an error that it cannot detect the code. Am I better of off using an external jQuery function to post this data dynamically, or is there a simpler solution?
the foo.js file:
<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',
'https://connect.facebook.net/en_US/fbevents.js');
fbq('init', '000000000');
fbq('track', 'PageView');
</script>
<noscript><img height="1" width="1" style="display:none"
src="https://www.facebook.com/tr?id=000000000&ev=PageView&noscript=1"
/></noscript>
<!-- End Facebook Pixel Code -->
<!-- Google Tag Manager -->
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-WOOOOFY');</script>
Thank you.