如果我在一个页面中使用多个Facebook Pixel会导致任何问题吗?

问题描述 投票:28回答:6

我想在一个页面中包含我的Facebook Pixel id和客户的Facebook Pixel id,因此我们都可以对用户有一些了解,如果他或她想要,客户也可以为该页面创建AD。

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

由于我找不到关于这种情况的任何信息,我想知道这样做是否可以?

谢谢

facebook
6个回答
24
投票

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

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

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

所以你的最终代码看起来像这样:

<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/


8
投票

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/


6
投票

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

根据Facebook帮助团队:

虽然这应该没问题,但如果在同一网页上安装了多个像素,并且收到了过去尝试过这种情况的人的差异报告,我们无法保证结果的准确性。


6
投票

在您的网站上拥有多个像素不应该在您的网站上产生任何问题(即不存在JavaScript错误,并且将针对所有初始化的像素ID发送所有事件)。我写了一篇关于这个问题的blog post,所以请随时阅读以便进行更全面的讨论。

在网站上包含完整的Facebook像素代码段之后,您可以在调用任何跟踪事件之前简单地添加其他像素ID。因此,网站上的完整像素代码段如下所示:

<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动态广告的产品目录,则可能需要跟踪事件并将Facebook的唯一产品ID传递给网站上的产品。

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

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

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

因此,问题仍然存在:如何仅将像素事件跟踪到像素,而不是让其他人跟踪他们的事件到你的像素?

跟踪网页浏览事件到Facebook的<noscript>方法给了我一个提示,你可以做到这一点,而不是通过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对象没有关联或关系。它只是在网站上动态插入<img>标签,调用Facebook来跟踪单个像素的事件。因此,您可以通过常规的Facebook像素代码片段跟踪事件在网站上初始化的所有像素(或使用新功能的单个像素),或者使用myfbq函数跟踪您自己的像素ID,或者您可以同时执行这两项操作!

如果您想同时执行这两项操作,请在网站上包含myfbq函数,然后您的Facebook像素代码段可能如下所示:

<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>

4
投票

多个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个参数。第一个参数将只是“跟踪”。第二个是新的,您可以在其中指定要用于跟踪此特定事件的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。


2
投票

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

this

© www.soinside.com 2019 - 2024. All rights reserved.