Is it possible to push tracking events to two sepa

2020-07-24 05:07发布

问题:

see Facebook Pixel docs (new as of 2016): https://developers.facebook.com/docs/facebook-pixel/using-the-pixel

Is it possible to push the same events to two different Facebook pixels at the same time? As far as I can tell, there is one instance of fbq created by the pixel code, it accepts one init call (specifying a single FB Pixel init) and all subsequent events are tied to the correct initialization of the single object.

回答1:

If you use the "init" event multiple times with different pixel IDs, all of the initialized pixels will receive all future events.



回答2:

Multiple Facebook Pixels Code

In order to be able to trigger distinct events to different Facebook pixels, without having it called to all previously initialized Facebook pixels, you can overwrite the fbq function (by copying and pasting the snippet below to your code base) and then call the tracking events as explained on the Usage section below.

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

Usage

Now the fbq function takes 3 parameters. The first param will be just "track". The second one is new, that's where you can specify the Facebook pixel ID you want to use to track this specific event. Third, as usual, say the event name. And, lastly, you can pass other options such as purchase value, currency, content_ids, etc.

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

Note that you can also use different purchase values for different pixels, and they will only be fired to that very specific pixel.

Demo

You can find the results in this product page, though I didn't use different data on the product page. But I use it to trigger Purchase events with different purchase values on the checkout. Also, if you don't have it yet, please download the Facebook Pixel Helper extension for Google Chrome. It'll help you debugging the Facebook Pixel.



标签: facebook