Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop Facebook Pixel tracking on localhost and development server

I'm using Facebook Pixel for tracking on my website.

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

Any solution to stop or filter like GA filter excludes domain or IP? Thanks.

like image 839
Pham Minh Tan Avatar asked Aug 20 '26 20:08

Pham Minh Tan


2 Answers

Code snipet for adding FB pixel is first checking if FB pixel is already added, by checking for variable fbq:

if(f.fbq)return;

So the simplest way to skip FB pixel script from initial injection is to define fbq variable before FB snipet.

<script>
    const list = ['localhost'];
    if (list.includes(window.location.hostname)) {
        window.fbq = function() {};
    }
    // ...original Facebook Pixel code here
</script>

Add any domain to the list array in order to block FB pixel execution.

like image 193
lima_fil Avatar answered Aug 22 '26 11:08

lima_fil


In Traffic permissions, you can allow only the production domain, which will automatically block localhost. FB Traffic permissions setup

Details here: https://www.facebook.com/business/help/278125336598935?id=1205376682832142

like image 23
Harsha N Hegde Avatar answered Aug 22 '26 09:08

Harsha N Hegde



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!