Implement Facebook Pixel in Default Carts through Google Tag Manager
A. Create a tag for Facebook Pixel Page View Initialization
Create this tag if you don’t already have a Facebook Pixel tag on your website that fires on all pages where you have the Google Tag Manager code.
Create Page View tag
Click to create a new tag.
As Tag Type select Custom HTML. In the script below, under '{your-pixel-id-goes-here}' type your Facebook pixel ID. The script below is the Pixel Base Code and can be found in the Facebook/Meta Pixel documentation.
<!-- 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', '{your-pixel-id-goes-here}');
fbq('track', 'PageView');
</script>As Triggering select All Pages.

B. Configure dataLayer variables in Google Tag Manager
Configure dataLayer variables in Google Tag Manager to further use them in the Facebook implementation.
2checkout shopping carts contain a dataLayer with eCommerce information on the Google Analytics 4 structure. You can view this by typing dataLayer into the browser console and under gtag4 you can see the eCommerce information on Google Analytics 4 structure both at checkout and at purchase on the order Finish page.

Configure dataLayer variables in Google Tag Manager
To capture the eCommerce information from the dataLayer, create the following User-Defined Variables in Google Tag Manager. For each variable:
Click to create a new User-Defined Variable.
As Variable Type select Data Layer Variable.
Under Data Layer Variable Name use the name below.
Under Data Layer Version select Version 2.
Create the following variables:
Name:
gtag4.event— Data Layer Variable Name:gtag4.event.

Name:
gtag4.currency— Data Layer Variable Name:gtag4.currency.
Name:
gtag4.items— Data Layer Variable Name:gtag4.items.
Name:
gtag4.tax— Data Layer Variable Name:gtag4.tax.
Name:
gtag4.transaction_id— Data Layer Variable Name:gtag4.transaction_id.
Name:
gtag4.value— Data Layer Variable Name:gtag4.value(captures total order value, including tax).
C. Create a User-defined variable in Google Tag Manager for Facebook called FacebookEventsScript
Create the FacebookEventsScript variable
Click on New to create a new user-defined-variable.

As Variable Type select Custom JavaScript.
In the Custom JavaScript section paste the code below. This code will capture product, transaction id and transaction value data, using the dataLayer variables previously created.
D. Create a trigger specifically for the purchase event
Create a new trigger which is restricted only to the purchase event and name it Purchase Event Only.
E. Create a tag for the Facebook Pixel for the purchase event
Create Facebook Purchase tag
Create a new tag (for example: Facebook Purchase).
As Tag Type select Custom HTML.
In the HTML section, paste the following code (this code will send product, transaction id and transaction value data to Meta when a purchase takes place).
For Triggering, select the trigger restricted to the purchase event (Purchase Event Only).

Optional — send information with the Facebook Pixel at checkout if needed
Create a trigger specifically for the checkout event
Privacy/consent note
The tags for Facebook Pixel need to be based on consent. Adjust the tags described above to condition them based on your method of collecting consent.
Last updated
Was this helpful?


