How can I place multiple Facebook tracking pixels on a single web page?

We are trying to use Facebook ad tracking pixels to track ads. we looked at the documentation on Facebook and did not bring me there.

I need to know how to run multiple Facebook pixels on the same page, because we have multiple ads.

I took the code that he gave me per pixel and placed it on the page, and then we have the Cart page in which I fire or add a Buy event. But since I have the script 3 times, it fires 3 times. I want it to fire once per pixel.

This is what I have so far:

<!-- Facebook Pixel Code - Ad 1--> <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', 'xxxxxxxxxxxxx12'); fbq('track', "PageView"); // Purchase // Track purchases or checkout flow completions (ex. landing on "Thank You" or confirmation page) fbq('track', 'Purchase', {value: '1.00', currency: 'USD'}); </script> <noscript><img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=xxxxxxxxxxxxx12&ev=PageView&noscript=1" /></noscript> <!-- End Facebook Pixel Code --> <!-- Facebook Pixel Code - Ad 2--> <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', 'xxxxxxxxxxxxx34'); fbq('track', "PageView"); // Purchase // Track purchases or checkout flow completions (ex. landing on "Thank You" or confirmation page) fbq('track', 'Purchase', {value: '1.00', currency: 'USD'}); </script> <noscript><img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=xxxxxxxxxxxxx34&ev=PageView&noscript=1" /></noscript> <!-- End Facebook Pixel Code --> <!-- Facebook Pixel Code - Ad 2--> <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', 'xxxxxxxxxxxxx56'); fbq('track', "PageView"); // Purchase // Track purchases or checkout flow completions (ex. landing on "Thank You" or confirmation page) fbq('track', 'Purchase', {value: '1.00', currency: 'USD'}); </script> <noscript><img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=xxxxxxxxxxxxx56&ev=PageView&noscript=1" /></noscript> <!-- End Facebook Pixel Code --> 

Is this a way to do this? Or I need to do something else so that the Buy event does not start 4 times. I use the FB Pixel Helper plugin for Chrome to make sure all pixels shoot, but I get an error message that they shoot several times.

Would I do something like this?

  <!-- Facebook Pixel Code - ALL ADS--> <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', 'xxxxxxxxxxxxx12'); fbq('init', 'xxxxxxxxxxxxx34'); fbq('init', 'xxxxxxxxxxxxx56'); fbq('track', "PageView"); // Purchase // Track purchases or checkout flow completions (ex. landing on "Thank You" or confirmation page) fbq('track', 'Purchase', {value: '1.00', currency: 'USD'}); </script> <noscript><img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=xxxxxxxxxxxxx12&ev=PageView&noscript=1" /></noscript> <!-- End Facebook Pixel Code --> 
+5
source share
7 answers

I ran into the same problem and I could not find a solution anywhere. I delved into the code and found a method that you can use called "addPixelId". In my testing, you can use this right after "init", which will add a second identifier. Any events that you are tracking will now be sent to both identifiers. For instance:

 fbq('init', 'xxxxxxxxxxxxx12'); fbq('addPixelId', 'xxxxxxxxxxxxx34'); fbq('addPixelId', 'xxxxxxxxxxxxx56'); fbq('track', 'PageView'); 

The only thing you need to know is to track any events that you are tracking for all the identifiers you have identified. It worked fantastic for me, no problem.

+12
source

Just add this code to one JS file: // Facebook Multi-Pixel

  (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(); })(); 

And than, an example of use:

 fbq('track', "<PIXEL_ID>", "PageView"); fbq('track", "<PIXEL_ID>", "ViewContent", { content_name: "name test", content_category: "category test", content_ids: ["test"], content_type: "product", value: 7.99, currency: "GBP" }); 

etc.

+4
source

This is the current working approach that I recommend based on the Facebook source code (based on https://developers.facebook.com/ ). Use the init function several times, it's just ...

 <!-- Facebook Pixel Code --> <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','https://connect.facebook.net/en_US/fbevents.js'); fbq('init', '158xxxxxxxxxx911'); fbq('init', '180xxxxxxxxxx762'); fbq('init', '125xxxxxxxxxx694'); fbq('track', 'PageView'); </script> <noscript><img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=180xxxxxxxxxx762&ev=PageView&noscript=1" /></noscript> <!-- End Facebook Pixel Code --> 
+2
source

The problem is that the Facebook pixel code / initialization may take a little time to load, not to mention the user's connection, and the device will also affect the initialization time.

You do NOT want to embed code three times.

First call the main FB JS Pixel code, which 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 slight delay. I would tweak it to suit your needs. You will need to use the close function to ensure the correct value of your array.

 // 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 trigger the current tracking events with a delay too , to ensure that they will be fired after the pixels are initialized.

 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 a good measure, put an HTML no script call to PageView for each pixel

 <noscript><img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=xxx111&ev=PageView&noscript=1"/> </noscript> <noscript><img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=xxx2222&ev=PageView&noscript=1"/> </noscript> 

The number of triggered events must match the number of pixels that you have, the number of events (i.e. each event registered for each pixel) ... there should be no errors if you exercise the right.

+1
source

@ClosDesign, you're on the right track! As you suspected, you can call fbq('init', '[your-pixel-id]') several times, which adds each pixel identifier to the list of identifiers in which all events will be tracked. I wrote a blog post about this issue of using multiple Facebook pixels on a single site, so read there for more details.

Using the sample code, you first want to include the Facebook JavaScript pixel code. This will load the JavaScript from Facebook, which is necessary to send pixel events using the fbq object:

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

Then, the init time of all pixel IDs:

 fbq('init', 'xxxxxxxxxxxxx12'); fbq('init', 'xxxxxxxxxxxxx34'); fbq('init', 'xxxxxxxxxxxxx56'); 

After initializing all the pixel identifiers on your site with fbq('init') use the Facebook Pixel Helper to confirm that all the pixels have been loaded. You can also manually confirm that they were downloaded by running the following code in the JavaScript console:

 fbq.getState().pixels 

After that, any calls you make to the track event will be tracked by all the pixel identifiers that you initialized above. Therefore, if you call:

 fbq('track', "PageView"); 
Event

A PageView will be sent to Facebook for all three pixels. Your Purchase event works the same.

When it comes to Facebook <noscript> tags, you will need to add an <img> for each pixel identifier that you want to call on your site:

 <noscript> <img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=xxxxxxxxxxxxx12&ev=PageView&noscript=1" /> <img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=xxxxxxxxxxxxx34&ev=PageView&noscript=1" /> <img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=xxxxxxxxxxxxx56&ev=PageView&noscript=1" /> </noscript> 

Note that if the user has disabled JavaScript, those <img> tags send a request to Facebook to track the PageView event. The Purchase event will not be tracked unless you create another <img> for each pixel identifier and provide data to track the Purchase event.

Naturally, the following question: "Can I track an event for only one of the many pixels initialized on my site?" Do not use Facebook fbq object, no.

However, there is a workaround . Using the <img> above, you can post events to Facebook and track the event with only one pixel identifier. This method completely bypasses the fbq object and dynamically creates an <img> with the event information that you specify.

code

 (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); 

Using

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

Another solution is to simply use the no-javascript approach if you want to track only specific events for specific pixel IDs

You can see several examples in your docs: https://developers.facebook.com/docs/facebook-pixel/api-reference

 <img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=FB_PIXEL_ID&amp;ev=MyCustomEvent&amp;cd[custom_param]=custom_value" /> <img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=FB_PIXEL_ID&amp;ev=PageView&amp;noscript=1" /> 

You just need to get the parameters formatted correctly.

0
source

According to the FB recommendation, it’s very easy to track different events for different pixels:

 fbq('trackSingle', 'FB_PIXEL_ID', 'Purchase', customData); fbq('trackSingleCustom', 'FB_PIXEL_ID', 'CustomEvent', customData); 

https://developers.facebook.com/docs/facebook-pixel/api-reference#tracking-events-for-individual-pixels

0
source

Source: https://habr.com/ru/post/1244847/


All Articles