是否有可能将跟踪事件推送到两个单独的Facebook pixel ?



查看Facebook Pixel文档(2016年新版本):https://developers.facebook.com/docs/facebook-pixel/using-the-pixel

是否有可能同时将相同的事件推送到两个不同的Facebook像素?据我所知,像素代码创建了一个fbq实例,它接受一个init调用(指定单个FB pixel init),所有后续事件都绑定到单个对象的正确初始化。

多个Facebook像素代码

为了能够触发不同的Facebook像素的不同事件,而不需要调用所有先前初始化的Facebook像素,您可以覆盖fbq函数(通过复制并粘贴下面的代码片段到您的代码库),然后调用跟踪事件,如下面的用法一节所述。

<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个参数。第一个参数就是"track"。第二个是新的,在这里你可以指定想要用来跟踪特定事件的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>

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

演示

你可以在这个产品页面找到结果,虽然我没有在产品页面上使用不同的数据。但是我用它来触发结账时具有不同购买值的Purchase事件。此外,如果你还没有它,请下载谷歌Chrome的Facebook像素助手扩展。它将帮助你调试Facebook Pixel。

如果使用不同像素id多次使用"init"事件,则所有初始化的像素将接收所有未来事件。

最新更新