【问题标题】:Will it cause any issue if I use multiple Facebook Pixel in one page?如果我在一页中使用多个 Facebook Pixel 会导致任何问题吗?
【发布时间】:2019-04-01 14:09:18
【问题描述】:

我想在一个页面中同时包含我的 Facebook Pixel id 和客户的 Facebook Pixel id,这样我们双方都可以对用户有一些了解,而且客户也可以根据需要为该页面创建广告。

我已经测试了跟踪,它似乎工作正常。但是,我确实收到了来自 Pixel SDK 的关于“在此页面上检测到多个不同像素”的警告。

由于我找不到有关此场景的任何信息,我想知道这样做是否可以?

谢谢

【问题讨论】:

    标签: facebook


    【解决方案1】:

    我有同样的要求,我发现这是可能的,你不需要破解 Facebook 像素代码。

    即使没有记录,fbq 对象也支持多个像素 ID。

    只需使用不同的像素 ID 多次调用 init 函数。然后,当您执行fbq('track', "PageView"); 时,它会为每个像素发出一个事件。您可以根据需要多次调用fbq('track', '{{your_event_name}}'),它会跟踪所有先前初始化的像素的事件。

    所以你的最终代码应该是这样的:

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

    2019 年 4 月编辑 @chuck-le-butt 发现 facebook 的一篇博文记录了如何使用像素:https://developers.facebook.com/ads/blog/post/2017/11/28/event-tracking-with-multiple-pixels-tracksingle/

    【讨论】:

    • 刚刚使用 GTM 进行了测试,它确实有效,谢谢
    • 它有效。问题是您不能为每个像素调用不同的事件。就我而言,我需要为每个像素分配不同的购买价值,这就是为什么我不得不“破解”他们的代码。
    • @ChuckLeButt 记录在 7 个月 这个答案之后。不知道你为什么要把态度摆在那里。
    【解决方案2】:

    FWIW,Facebook 添加了 trackSingletrackSingleCustom 事件(截至 2017 年 11 月),以便在页面上初始化多个像素时选择性地触发特定像素上的事件。

    文档:https://developers.facebook.com/docs/facebook-pixel/events-advanced-use-cases/v2.12#multipixels

    博文:https://developers.facebook.com/ads/blog/post/2017/11/28/event-tracking-with-multiple-pixels-tracksingle/

    【讨论】:

      【解决方案3】:

      https://www.facebook.com/business/help/community/question/?id=10100525729830570

      根据 Facebook 帮助团队:

      虽然这应该没问题,但我们无法保证 如果在同一个网页上安装了多个像素并且 已收到来自尝试过此方法的人的差异报告 过去。

      【讨论】:

      • 链接现在已损坏:(
      【解决方案4】:

      在您的网站上拥有多个像素不应在您的网站本身上造成任何问题(即不会出现 JavaScript 错误,并且所有事件都会针对所有已初始化的像素 ID 发送)。我就这个问题写了blog post,所以请随时阅读那里进行更全面的讨论。

      在网站上包含完整的 Facebook 像素代码 sn-p once 后,您可以在调用任何跟踪事件之前简单地添加额外的像素 ID 初始化。因此,您在网站上的完整像素代码 sn-p 将如下所示:

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

      使用fbq('init') 初始化网站上的所有像素 ID 后,您可以通过在 JavaScript 控制台中运行以下代码来确认它们已被加载:

      fbq.getState().pixels
      

      您还应该安装Facebook Pixel Helper。它提供了有关发送到 Facebook 的每个像素和每个跟踪事件的一些详细信息。

      下一个自然问题是如何仅跟踪其中一个像素 ID 的事件。如果您正在处理 Facebook 动态广告的产品目录,您可能需要跟踪事件并将您在网站上的产品的唯一产品 ID 传递给 Facebook。

      截至 2017 年底,Facebook finally announced 功能可将像素事件跟踪到单个像素 ID。您只需使用操作trackSingletrackSingleCustom 而不是tracktrackCustom,如下所示:

      fbq("trackSingle, "[your-pixel-id]", "ViewContent", { content_type: "product", content_ids: ['892185001003'] });
      

      但是,这种范例仍然存在一个问题:即使在具有多个像素的网站上的一位开发人员不使用新操作,他的事件也会跟踪到网站上每个人的像素 ID。从我在过去几个月看到的情况来看,几乎每个开发人员仍然使用track 操作。我应该在这里说,如果其他开发人员将他们的事件跟踪到您的像素,这并不是世界末日,但我喜欢不要丢弃我的数据并可能抛弃分析和/或转化测量的想法。

      所以,问题仍然存在:您如何跟踪像素事件,而不让其他人也跟踪他们的事件到您的像素?

      &lt;noscript&gt; 向 Facebook 跟踪网页浏览事件的方法给了我一个提示,您可以做到这一点,只是不能通过 fbq 对象。

      在 Flavio Wuensche 的 an answer 中,他实际上实现了这个想法。我拿了它,清理它,修改它。

      代码

      (function (window, document) {
          if (window.myfbq) return;
          window.myfbq = (function () {
              if (arguments.length > 0) {
                  var pixelId, trackType, contentObj;
      
                  if (typeof arguments[0] == 'string') pixelId = arguments[0];
                  if (typeof arguments[1] == 'string') trackType = arguments[1];
                  if (typeof arguments[2] == 'object') contentObj = arguments[2];
      
                  var params = [];
                  if (typeof pixelId === 'string' && pixelId.replace(/\s+/gi, '') != '' &&
                  typeof trackType === 'string' && trackType.replace(/\s+/gi, '')) {
                      params.push('id=' + encodeURIComponent(pixelId));
                      switch (trackType) {
                          case 'PageView':
                          case 'ViewContent':
                          case 'Search':
                          case 'AddToCart':
                          case 'InitiateCheckout':
                          case 'AddPaymentInfo':
                          case 'Lead':
                          case 'CompleteRegistration':
                          case 'Purchase':
                          case 'AddToWishlist':
                              params.push('ev=' + encodeURIComponent(trackType));
                              break;
                          default:
                              return;
                      }
      
                      params.push('dl=' + encodeURIComponent(document.location.href));
                      if (document.referrer) params.push('rl=' + encodeURIComponent(document.referrer));
                      params.push('if=false');
                      params.push('ts=' + new Date().getTime());
      
                      if (typeof contentObj == 'object') {
                          for (var u in contentObj) {
                              if (typeof contentObj[u] == 'object' && contentObj[u] instanceof Array) {
                                  if (contentObj[u].length > 0) {
                                      for (var y = 0; y < contentObj[u].length; y++) { contentObj[u][y] = (contentObj[u][y] + '').replace(/^\s+|\s+$/gi, '').replace(/\s+/gi, ' ').replace(/,/gi, '§'); }
                                      params.push('cd[' + u + ']=' + encodeURIComponent(contentObj[u].join(',').replace(/^/gi, '[\'').replace(/$/gi, '\']').replace(/,/gi, '\',\'').replace(/§/gi, '\,')));
                                  }
                              }
                              else if (typeof contentObj[u] == 'string')
                                  params.push('cd[' + u + ']=' + encodeURIComponent(contentObj[u]));
                          }
                      }
      
                      params.push('v=' + encodeURIComponent('2.7.19'));
      
                      var imgId = new Date().getTime();
                      var img = document.createElement('img');
                      img.id = 'fb_' + imgId, img.src = 'https://www.facebook.com/tr/?' + params.join('&'), img.width = 1, img.height = 1, img.style = 'display:none;';
                      document.body.appendChild(img);
                      window.setTimeout(function () { var t = document.getElementById('fb_' + imgId); t.parentElement.removeChild(t); }, 1000);
                  }
              }
          });
      })(window, document);
      

      用法

      myfbq("[your-pixel-id]", "PageView");
      myfbq("[your-pixel-id]", "ViewContent");
      myfbq("[your-pixel-id]", "ViewContent", { content_type: "product", content_ids: ['892185001003'] });
      

      进一步讨论

      请注意,此myfbq 函数与fbq 对象没有关联或关联。它只是在网站上动态插入&lt;img&gt; 标签,调用 Facebook 以跟踪单个像素的事件。因此,您可以通过普通 Facebook 像素代码 sn-p 跟踪网站上初始化的所有像素(或使用新功能的单个像素)的事件,或者使用 myfbq 函数仅跟踪您自己的像素 ID,或者您可以两者都做!

      如果你想两者都做,请在网站上包含myfbq 函数,那么你的 Facebook 像素代码 sn-p 可能如下所示:

      <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', '12345678');
          fbq('init', '87654321');
          fbq('track', 'PageView'); //tracks a PageView event to all initialized pixels
      
          myfbq("87654321", "ViewContent", { content_type: "product", content_ids: ['892185001003'] }); //tracks a ViewContent event to only one pixel, with your own unique ProductId
      </script>
      <noscript>
          <img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=12345678&ev=PageView&noscript=1" />
          <img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=87654321&ev=PageView&noscript=1" />
      </noscript>
      

      【讨论】:

      • 您从哪里获得有关fbq.getState() 方法的信息?我找不到任何关于它的官方信息。是否有其他相关方法可用于了解 Facebook 跟踪行为?
      • 我使用开发者工具控制台“点”到 fbq 对象中,看看它上面有什么。要自己执行此操作,请打开控制台并输入 fbq.。键入句点后,它将显示对象的属性列表。您还可以转到 Sources 选项卡并将 fbq 添加到 Watch 部分。这使您可以扩展对象并查看它具有哪些属性。
      【解决方案5】:

      多个 Facebook 像素代码

      为了能够对不同的 Facebook 像素触发不同的事件,而无需调用所有先前初始化的 Facebook 像素,您可以覆盖 fbq 函数(通过复制和粘贴 sn-p下面到您的代码库),然后按照下面使用部分的说明调用跟踪事件。

      <script type="text/javascript">
      (function() {
        var fbq = (function() {
          function fbq()
          {
            if(arguments.length > 0) {
              var action, pixel_id, type_track, content_obj;
      
              if( typeof arguments[0] == "string") action = arguments[0];
              if( typeof arguments[1] == "string") pixel_id = arguments[1];
              if( typeof arguments[2] == "string") type_track = arguments[2];
              if( typeof arguments[3] == "object") content_obj = arguments[3];
      
              var params = [];
              if(typeof action == "string" && action.replace(/\s+/gi, "") != "" &&
              typeof pixel_id == "string" && pixel_id.replace(/\s+/gi, "") != "" &&
              typeof type_track == "string" && type_track.replace(/\s+/gi, "")) {
      
                params.push("id=" + encodeURIComponent(pixel_id));
                switch(type_track) {
                  case "PageView":
                  case "ViewContent":
                  case "Search":
                  case "AddToCart":
                  case "InitiateCheckout":
                  case "AddPaymentInfo":
                  case "Lead":
                  case "CompleteRegistration":
                  case "Purchase":
                  case "AddToWishlist":
                  params.push("ev=" + encodeURIComponent(type_track));
                  break;
                  default:
                  return;
                }
      
                params.push("dl=" + encodeURIComponent(document.location.href));
                params.push("rl=" + encodeURIComponent(document.referrer));
                params.push("if=false");
                params.push("ts=" + new Date().getTime());
      
                if(typeof content_obj == "object") {
                  for(var u in content_obj) {
                    if(typeof content_obj[u] == "object" && content_obj[u] instanceof Array) {
                      if(content_obj[u].length > 0) {
                        for(var y=0; y<content_obj[u].length; y++) { content_obj[u][y] = (content_obj[u][y]+"").replace(/^\s+|\s+$/gi, "").replace(/\s+/gi," ").replace(/,/gi, "§"); }
                        params.push("cd[" + u + "]=" + encodeURIComponent(content_obj[u].join(",").replace(/^/gi, "[\"").replace(/$/gi, "\"]").replace(/,/gi, "\",\"").replace(/§/gi, "\,")));
                      }
                    }
                    else if(typeof content_obj[u] == "string")
                    params.push("cd[" + u + "]=" + encodeURIComponent(content_obj[u]));
                  }
                }
      
                params.push("v=" + encodeURIComponent("2.5.0"));
      
                if(typeof window.jQuery == "function") {
                  var iframe_id = new Date().getTime();
                  jQuery("body").append("<img height='1' width='1' style='display:none;width:1px;height:1px;' id='fb_" + iframe_id + "' src='https://www.facebook.com/tr/?" + params.join("&") + "' />");
                  setTimeout(function() { jQuery("#fb_" + iframe_id).remove(); }, 1000);
                }
              }
            }
          }
      
          return fbq;
        });
      
        window.fbq = new fbq();
      })();
      </script>
      

      用法

      现在 fbq 函数需要 3 个参数。第一个参数将只是“跟踪”。第二个是新的,您可以在其中指定要用于跟踪此特定事件的 Facebook 像素 ID。第三,像往常一样,说出事件名称。最后,您可以传递其他选项,例如购买价值、货币、content_ids 等。

      <script>
        fbq('track', '<FIRST_PIXEL_ID>', 'PageView');
        fbq('track', '<FIRST_PIXEL_ID>', 'Purchase', {value: 79.9, currency: 'BRL'});
        fbq('track', '<SECOND_PIXEL_ID>', 'Purchase', {value: 89.9, currency: 'BRL'});
      </script>
      

      请注意,您也可以对不同的购买值使用不同的购买值 像素,它们只会被触发到那个非常特定的像素。

      演示

      你可以找到结果in this product page,虽然我没有在产品页面上使用不同的数据。但我用它在结账时触发具有不同购买值的购买事件。另外,如果您还没有,请下载 Google Chrome 的 Facebook Pixel Helper 扩展程序。它将帮助您调试 Facebook Pixel。

      【讨论】:

      • Facebook 是否以任何方式正式支持此功能?
      • Facebook 不以任何方式支持它。它只是工作。
      • 直到它没有抬头 ;-) 很好,以防万一没有其他选择。谢谢。
      • 当然。这绝对不是你想长期保留的东西;)
      • 我可以建议您处理代码中的数字类型吗?因此,当我们传递购买值(来自数字类型)时,它将被处理 *** else if (typeof content_obj[u] == "number") params.push("cd[" + u + "]= " + encodeURIComponent(content_obj[u])); ***
      【解决方案6】:

      Facebook 转换像素代码已更新,使广告客户能够跟踪同一网页中的多个像素。以前,如果您在同一页面上有多个版本的转化跟踪像素,则会导致冲突并导致数据丢失。新代码还可以更轻松地跟踪页面内操作,例如单击按钮。

      this

      【讨论】:

      • 感谢您的回复,但我使用的是新像素,而不是已弃用的转换像素。
      猜你喜欢
      • 2016-09-14
      • 1970-01-01
      • 2019-05-21
      • 2018-12-21
      • 2011-05-24
      • 2013-04-04
      • 2014-04-05
      • 2017-08-16
      • 2014-08-09
      相关资源
      最近更新 更多