Install Meta Pixel with Google Tag Manager
Updated: Wednesday, May 1, 2024
Get the Meta Pixel ID
To set up the Meta Pixel on Google Tag Manager, we need to retrieve its ID from Meta’s Business Manager.
To get started, go to https://business.facebook.com.
Go to Business settings then Data sources > Datasets.
![Business settings in Meta Business Manager](https://data-marketing-school.com/images/blog/meta-pixel/meta_business_settings.en_hu0533df8c600b6ebf5e775c57536fef00_60763_838x0_resize_q75_h2_box.webp)
If you don’t already have a pixel, you’ll need to create one.
Once the pixel has been created, click on its ID to copy it.
![Meta Pixel ID](https://data-marketing-school.com/images/blog/meta-pixel/new_pixel_id.en_hu1c16001ded50b9a6a830a8ad4aa3dccb_43489_952x0_resize_q75_h2_box.webp)
Install the Meta Pixel base code with Google Tag Manager (PageView event)
In Google Tag Manager, create a new tag and select this template from the community gallery:
![facebookarchive tag template](https://data-marketing-school.com/images/blog/meta-pixel/modele_balise_facebook_archive.en_hu0533df8c600b6ebf5e775c57536fef00_86557_1282x0_resize_q75_h2_box.webp)
You can then configure the tag by entering the following information:
- your pixel identifier
- the event you wish to send (here, select the standard PageView event)
![Meta Pixel tag configuration on PageView event](https://data-marketing-school.com/images/blog/meta-pixel/configuration_balise_facebookarchive.en_hu0533df8c600b6ebf5e775c57536fef00_164243_1832x0_resize_q75_h2_box.webp)
As a reminder, this configuration corresponds to the following code:
<!-- Meta 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', '946789763753044');
fbq('track', 'PageView');
</script>
<!-- End Meta Pixel Code -->
Click on Save and publish your changes in your container.
Setting up standard Meta Pixel events with Google Tag Manager
Now that the basic code is set up (with the standard PageView
event), you can track other standard Meta events that may correspond to user behavior on your site.
You’ll find all standard Meta events on their official documentation.
Here we’ll install the Purchase
event for the example.
Unlike the PageView
event, the Purchase
event has two mandatory parameters: currency
and value
.
![Purchase standard event configuration](https://data-marketing-school.com/images/blog/meta-pixel/configuration_evenement_standard_purchase.en_hu1c16001ded50b9a6a830a8ad4aa3dccb_187605_1466x0_resize_q75_h2_box.webp)
Once you’ve configured the tag using the steps above, you can save it and publish your changes.
To test that the tag is working properly, you can use the Google Tag Manager debugger and the Meta Pixel Helper extension.
![Checking Purchase event parameters with Meta Pixel Helper extension](https://data-marketing-school.com/images/blog/meta-pixel/meta_pixel_helper_purchase_hu5c716a5035c022c3aaf8e5f62bb8211f_6254_210x0_resize_q75_h2_box.webp)
We can see above that the parameters are indeed sent with the Purchase
event and in the screenshot just below that these same parameters are indeed received by Meta.
To carry out this check, I went to the event manager and then to the Test events tab.
![Test events in the events manager](https://data-marketing-school.com/images/blog/meta-pixel/evenement_de_test_business_manager_purchase.en_hu1c16001ded50b9a6a830a8ad4aa3dccb_277453_2084x0_resize_q75_h2_box.webp)
Add additional data to your events (contents, content_ids) to benefit from the Meta Advantage+ catalog
To find out more about the Meta Advantage+ catalog: https://www.facebook.com/business/help/397103717129942
Adding content_ids from GA4’s e-commerce Data Layer
Useful resources :
GA4’s e-commerce data layer provides a product table called items
.
This items
array contains the item_id
key that we’ll need to send to Meta for each product.
The format required for product identifiers to be properly understood by Meta is a Javascript array of strings
.
Here’s an example: [“id_765”, “id_543”, “id_123”]
.
For this tutorial, we’ll start with this Data Layer:
dataLayer.push({ ecommerce: null }); // I delete the old ecommerce object
dataLayer.push({
event: "purchase", // event name
ecommerce: {
// event parameters
currency: "EUR",
value: 29.99,
coupon: "FLASH",
transaction_id: "154561",
shipping: 4.48,
tax: 5.99,
items: [ // products in the order when this event is fired
{
// products parameters
item_id: "4561897",
item_name: "T-shirt Iron Man",
price: 29.99,
quantity: 19
},
{
item_id: "4561898",
item_name: "T-shirt Batman",
price: 29.99,
quantity: 19
},
{
item_id: "4561899",
item_name: "T-shirt Thor",
price: 29.99,
quantity: 19
},
{
item_id: "4561900",
item_name: "T-shirt Captain America",
price: 29.99,
quantity: 19
}
]
}
});
To access the items
array, we’ll need to create a data layer variable in Google Tag Manager.
![Creating data layer variable ecommerce.items](https://data-marketing-school.com/images/blog/meta-pixel/variable_dlv_items_gtm.en_hu0533df8c600b6ebf5e775c57536fef00_94695_1262x0_resize_q75_h2_box.webp)
Here, DLV stands for Data Layer Vvariable.
Next, we’ll create a custom Javascript variable into which we’ll insert the following code.
function () {
var content_ids = [];
{{DLV - ecommerce.items}}.forEach(function (el) {
content_ids.push(el.item_id);
});
return content_ids;
}
We name this variable OP - content_ids. OP stands for Object Properties as I’m using the same naming as the Meta reference documentation.
![Custom Javascript variable setup for content_ids](https://data-marketing-school.com/images/blog/meta-pixel/configuration_variable_cjs_content_ids.en_hu0533df8c600b6ebf5e775c57536fef00_118134_1344x0_resize_q75_h2_box.webp)
We add the content_ids
parameter to the Object Properties of our Meta tag.
![Add content_ids to Object Properties in the tag configuration](https://data-marketing-school.com/images/blog/meta-pixel/content_ids_object_properties_hu2ec579060b5994fc171ea69b73624d96_39269_998x0_resize_q75_h2_box.webp)
We then check in Google Tag Manager’s debug mode that all the information has been correctly sent.
![Debugging Object Properties in Google Tag Manager](https://data-marketing-school.com/images/blog/meta-pixel/debug_object_properties_gtm_hu70622912f42953e756c62c0843192444_21730_633x0_resize_q75_h2_box.webp)
Finally, we check in the event handler of Meta’s Business Manager whether the event has been reported with the correct parameters.
![content_ids check in events manager](https://data-marketing-school.com/images/blog/meta-pixel/verification_content_ids_gestionnaire_evenement.en_hu0533df8c600b6ebf5e775c57536fef00_110105_1412x0_resize_q75_h2_box.webp)
Adding contents from GA4’s e-commerce data layer
The process is the same as explained in the previous section for content_ids
. However, the code for the custom JavaScript variable will change.
function() {
var contents = [];
{{DLV - ecommerce.items}}.forEach(function(el) {
contents.push({ id: el.item_id, quantity: el.quantity });
});
return contents;
}
What to do with the data sent to Meta?
Create personalized audiences (or retargeting audiences)
The data collected by the Meta Pixel can be used to identify the behavior of your visitors, enabling you to create audiences based on this behavior.
For example, you can create an audience for visitors who have added a product to the shopping cart but haven’t purchased (the so-called abandoned carts).
To do this, go to Audiences > Create an audience > Custom audience > Source: Website in your Business Manager.
![Create a personalized audience in Meta Audiences](https://data-marketing-school.com/images/blog/meta-pixel/creation_audience_personnalisee_meta.en_hu0533df8c600b6ebf5e775c57536fef00_163804_1198x0_resize_q75_h2_box.webp)
Feeding machine learning algorithms
Meta algorithms learn what’s good for your business from the events reported by the Meta Pixel. The more and the better the data from the pixel, the more you can “train” the algorithm to bring you what you want (i.e. sales, leads, awareness, depending on your objectives).
Let’s say your goal is to generate purchases on your site, you can install the Purchase
event and set it as a conversion event in your ad sets.
![Adset optimization according to the Purchase event](https://data-marketing-school.com/images/blog/meta-pixel/adset_evenement_de_conversion_achat.en_hu0533df8c600b6ebf5e775c57536fef00_342825_2234x0_resize_q75_h2_box.webp)
Didn't find what you were looking for?
Get help from the Data Marketing Club