How would I put multiple Facebook tracking pixels on one web page?

后端 未结 7 1149
情书的邮戳
情书的邮戳 2020-12-30 11:36

We are trying to use Facebook\'s Ad tracking pixels to track ads. we looked at Facebook\'s documentation and that led me no where.

I need to know how to trigger mult

7条回答
  •  猫巷女王i
    2020-12-30 11:59

    The issue is that the Facebook Pixel code/initialization can take a little bit of time to load not to mention a user's connection and device will also affect the init time.

    You DO NOT want to paste the code three times.

    First call the core FB JS Pixel code that references the fbevents.js library

    !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');
    

    Then create your array of pixel ids

    // Array of FB PixelIDs
    var pixel_ids = ['xxx111','xxx222'];
    

    Iterate over the array and call each pixel initialization with a small delay. I'd tweak it to meet your needs. You'll need to use a closure function to ensure the proper value of your array is assigned.

    // Iterate over the ids and ensure each one is initialized
    for (var i = 1; i <= pixel_ids.length; i++){
      // Add delay here to allow each pixel to instantiate
      setTimeout(function(x) { return function() {fbq('init',x.toString());};
        }(pixel_ids[i-1]), 33*i);
    }
    

    Now it's time to call your actual tracking events also with a delay to ensure they are fired after pixel initialization.

    setTimeout(function() { fbq('track', "PageView");}, (33*pixel_ids.length)+33);
    setTimeout(function() { fbq('track', 'Purchase', {value: '1.00', currency: 'USD'});},(33*pixel_ids.length)+66);
    

    For good measure put a HTML no script call to PageView for each pixel

    
    
    

    The number of events fired should equal the number of pixels you have times the number of events (i.e. each event registered for each pixel) ... there should not be errors if you implement right.

提交回复
热议问题