在本地主机和开发服务器上停止 Facebook Pixel 跟踪

Pha*_*Tan 2 tracking facebook-pixel

我正在使用 Facebook Pixel 在我的网站上进行跟踪。

<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>
Run Code Online (Sandbox Code Playgroud)

任何停止或过滤像 GA 过滤器的解决方案排除域或 IP?谢谢。

lim*_*fil 6

用于添加 FB 像素的代码段首先通过检查变量来检查 FB 像素是否已添加fbq

if(f.fbq)return;
Run Code Online (Sandbox Code Playgroud)

因此,从初始注入跳过 FB 像素脚本的最简单方法是fbq在 FB 片段之前定义变量。

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

将任何域添加到list阵列以阻止 FB 像素执行。