How to track Facebook events via Google Tag Manager

Last Updated: February 2, 2022

Prerequisites for adding a Facebook Events via Google Tag Manager:

  1. Basic Understanding of Facebook pixels and Facebook events.
  2. You have a Google Tag Manager account.
  3. Google Tag Manager is installed on your website.
  4. You have the publish permission at the GTM container level.
  5. You have an active Facebook Business Manager account.
  6. You have at least one Facebook ad account which is linked to your Facebook business manager account.

Introduction to Facebook Pixel

The tracking code (or tag) used by Facebook is called the ‘Facebook Pixel’. It is basically a bunch of JavaScript code which you should add to your website so that Facebook can:

  1. Track and report on your website users activities like pageviews, view content (viewing product detail pages), website searches, signups, leads, ‘add to cart’, ‘purchases’ etc.
  2. Retarget your website users on Facebook.
  3. Find a new target audience/customers for your business.
  4. Track and report on the conversions generated by your Facebook ads.
  5. Automatically optimize your ads for conversions.

Following is an example of Facebook Pixel Code:

<!-- 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', '87945356632151151');
  fbq('track', 'PageView');
</script>
<noscript><img height="1" width="1" style="display:none"
  src="https://www.facebook.com/tr?id=87945356632151151&ev=PageView&noscript=1"
/></noscript>
<!-- End Facebook Pixel Code -->

The Facebook pixel id

The Facebook pixel code contains a unique ID called the ‘Pixel id‘. It is in a format that resembles a mobile phone number. For example in the pixel code below:

s.parentNode.insertBefore(t,s)}(window, document,’script’,
‘https://connect.facebook.net/en_US/fbevents.js’);
fbq(‘init’, ‘87945356632151151‘);
fbq(‘track’, ‘PageView’);
</script>

The Facebook pixel id is: 87945356632151151

Through pixel id, Facebook is able to differentiate between various Facebook advertisers.

The Facebook machine-learning algorithm uses this pixel code to learn about your website users on Facebook. For example what Fan pages they like, click, comment, people they engage with, etc on day to day basis.

The more the pixel code fires on your website, the more Facebook learns about your website users and the better it becomes in targeting them and other similar audiences to complete your website conversions.

What that means, if you have got brand-new Pixel code, Facebook will know little to nothing about your website users’ activities on Facebook. Consequently, your Facebook ads may initially struggle to generate sales/leads for your website and you are likely to see negative ROI.

Businesses who are making a killing on Facebook, generating millions of dollars in sales via Facebook ads, all have got a very matured Facebook pixel.

So their pixels know a lot more about their target audience/customers than your brand new pixel.

Introduction to Facebook Events

The Facebook pixel code track your website users’ activities on Facebook.

Facebook calls these users activities ‘events‘. Each user activity is defined via a unique Facebook event.

For example, users’ purchase activity on your website is defined via the ‘Purchase‘ event:

fbq('track', 'Purchase', {
value: 247.35,
currency: 'USD'
});

A user’s ‘add to cart’ activity on your website is defined via the ‘AddToCart‘ event:

fbq('track', 'AddToCart', {
value: 3.50,
currency: 'USD'
});

Each Facebook event has got one or more attributes. For example, the Facebook ‘purchase’ event has got two attributes: ‘value’ (which is the value of the purchase) and ‘currency’ (the currency in which the purchase was made). Similarly, Facebook’s ‘AddToCart’ event has got two attributes: ‘value’ and ‘currency’.

Following are the examples of other events recognized by Facebook:

  1. Search
  2. ViewContent
  3. AddToWishlist
  4. InitiateCheckout
  5. AddPaymentInfo
  6. Lead
  7. CompleteRegistration

#1 Search – this event should be fired when a user performs a search on your website. Following is the example of the code used to track Search Event:

fbq('track', 'Search', {
search_string: 'leather sandals'
});

#2 ViewContent – this event should be fired when a user visits an important content page or product detail page on your website. Following is the example of the code used to track ‘ViewContent’ Event:

fbq('track', 'ViewContent', {
value: 3.50,
currency: 'USD'
});

#3 AddToWishlist – this event should be fired when a user adds an item to a wishlist on your website. Following is the example of the code used to track ‘AddToWishlist’ Event:

fbq('track', 'AddToWishlist', {
value: 247.35,
currency: 'USD'
});

#4 InitiateCheckout – this event should be fired when a user enters the checkout on your website. Following is the example of the code used to track ‘InitiateCheckout’ Event:

fbq('track', 'InitiateCheckout');

#5 AddPaymentInfo – this event should be fired when a user adds his payment information during the checkout on your website. Following is the example of the code used to track ‘AddPaymentInfo’ Event:

fbq('track', 'AddPaymentInfo');

#6 Lead – this event should be fired when a user submits a lead on your website. Following is the example of the code used to track ‘Lead’ Event:

fbq('track', 'Lead', {
value: 10.00,
currency: 'USD'
});

#7 CompleteRegistration – this event should be fired when a user completes registration on your website. I used this event to track newsletter subscribers. Following is the example of the code used to track the ‘CompleteRegistration’ Event:

fbq('track', 'CompleteRegistration', {
value: 25.00,
currency: 'USD'
});

Standard Vs Custom Facebook Events

Standard Facebook events are the ones that are defined by Facebook.

Following are examples of standard Facebook events:

  1. Search
  2. ViewContent
  3. AddToWishlist
  4. AddToCart
  5. InitiateCheckout
  6. AddPaymentInfo
  7. Purchase
  8. Lead
  9. CompleteRegistration

Custom Facebook Events are the ones that are defined by people like me and you.

So if there is any users’ activity that you can not track via standard Facebook events then use the custom Facebook events for that.

Through custom events, you can track any users’ activity on your website on Facebook.

Following is the syntax for tracking custom events on Facebook:

fbq('track', '<EVENT_NAME>', {
<parameter_key>: <parameter_value>,
<parameter_key>: '<parameter_value>'
});

Facebook Default Pixel Code (also called ‘pixel base code’)

The Facebook default pixel code refers to the pixel code supplied by Facebook.

This code does not contain any customization from the end-user (like you or your developer). This code also does not contain any standard or custom Facebook event code.

You can add the Facebook default pixel code on your website in the following ways:

  1. Hard code the pixel code on all the pages of your website.
  2. Use Google Tag Manager to deploy the default pixel code on your website.
  3. Enter the Facebook Pixel id. Many Shopping carts like ‘Shopify’ can automatically add Facebook default pixel code on your website. All you need to do is supply your Facebook pixel id to your shopping cart software.

Tracking Facebook Events

In order to track a Facebook event, you would need to add the event code to your website.

You can add the event code in the following ways:

  1. Hard code the event code on your website.
  2. Use Google Tag Manager to deploy the event code on your website.
  3. Enter the Facebook Pixel id. Many Shopping carts like ‘Shopify’ can track all standard Facebook events automatically. All you need to do is supply your Facebook pixel id to your shopping cart software.

For example, if you want to track ‘purchases’ made by users on your website, within Facebook then you need to copy-paste the following similar code on your ‘order confirmation’ page:

fbq('track', 'Purchase', {
value: 247.35,
currency: 'USD'
});

Note the word ‘similar’. This is not going to be your actual code.

The actual code is likely to contain server-side variables which pull data from your shopping cart. In the case of Google Tag Manager, the actual code is likely to contain data layer variables that pull data from the data layer embedded on your ‘order confirmation’ page.

You can, however, send this example code to your developer and tell him to collect the data in this format. He should understand, what to do next.

Follow the steps below to track Facebook events via Google Tag Manager:

Let us suppose you want to track ‘add to carts‘ on your website within Facebook so that you can see which Facebook ads are resulting in ‘add to cart’ activity on your website.

Step-1: Navigate to your Facebook Events Manager: https://www.facebook.com/events_manager2/list/

Step-2: Click on your Facebook pixel:

Click on your Facebook pixel 1

Step-3: Click on the ‘Manage Integrations’ button:

Manage Integrations 1

You should now see a dialogue box like the one below:

integrations 1

Step-4: Click on the ‘Manage’ drop-down menu next to ‘Browser pixel’ and then click on ‘Add to another website’:

Add to another website 1

You should now see a window like the one below:

use a partner integration

Step-5: Click on the ‘Install Code Manually‘ button:

install manually

Step-6: Click on the ‘Copy Code‘ button to copy the Facebook default pixel code:

copy code

Step-7: Log in to your Google Tag Manager account and create a new custom HTML tag.

Step-8: Name your new tag ‘Facebook default pixel code’ and then paste the Facebook pixel code in the text area:

facebook default pixel code

Step-9: Set the tag to fire on all pages of your website:

all pages

Step-10: Save your new tag and then preview it to see whether it fires on all of your web pages.

Step-11: Publish your container.

Step-12: Navigate to the Facebook Pixel Event Generator tool and select ‘Add to Cart‘ from the first drop-down menu:

facebook event generator tool

Step-13 (Optional): Enter one or more parameters:

enter parameters

Step-14: Scroll down the page and then click on the ‘Copy Code‘ button:

copy code button

Step-15: Navigate back to your GTM account.

Step-16: Create a new Custom HTML tag. Name this tag ‘Send Add to Cart Event to Facebook’ and then paste the ‘add to cart’ code you copied earlier in the text area:

send add to cart event

Step-17: Click on ‘Advanced Settings’ and then click on ‘Tag Sequencing’:

advanced settings gtm

Step-18: Select the checkbox named Fire a tag before Send Add to Cart Event to Facebook fires’ and then select ‘Facebook default pixel code’ from the ‘Setup Tag

tag sequencing

Note: Also click on the checkbox named “Don’t fire Send Add to Cart Event to Facebook if Facebook default Pixel Code fails or is paused“.

Step-19: Fire this tag only on those web pages which contain the ‘Add to Cart’ button and when the button is clicked.

Step-20: Test whether this tag fire correctly. If it does then publish the container.

Step-21: Repeat Steps 12 to 20 for tracking other Facebook events.

  1. How to add Facebook Pixel to Google Tag Manager
  2. facebook.com Referral Traffic in Google Analytics Explained
  3. Facebook Attribution and Conversion Windows Tutorial
  4. Facebook ROI Calculation, Analysis and Examples
  5. How to set up the Facebook Attribution Tool
  6. How to Setup Funnels in Facebook Ads Manager
  7. How to use funnels for your Facebook Ad Campaigns
  8. How to advertise on Facebook for FREE with unlimited budget
  9. Facebook Pixel vs Google Analytics Data Discrepancies
  10. facebook.com Referral Traffic in Google Analytics Explained
  11. Send Facebook Pixel Purchase Event via Google Tag Manager
  12. Open Graph Protocol for Facebook Explained with Examples
  13. Tracking Facebook ‘Likes’ and ‘Unlikes’ in Google Analytics
  14. Google Analytics for Facebook Tutorial
  15. Facebook Attribution Models Tutorial
  16. GTM Server Side Tagging for Facebook Tutorial
  17. The impact of Apple IOS 14.5 update on Facebook Ads
  18. How to add Facebook Pixel to WordPress Website
  19. How to add Facebook Pixel to WordPress without a Plugin
  20. How to use the Facebook Event Setup Tool
  21. How to use the Facebook Pixel Helper to Test Facebook Events
  22. How to automatically turn off Facebook ads on weekends
  23. How to name Facebook Ad Campaigns like a Pro

Register for the FREE TRAINING...

"How to use Digital Analytics to generate floods of new Sales and Customers without spending years figuring everything out on your own."



Here’s what we’re going to cover in this training…

#1 Why digital analytics is the key to online business success.

​#2 The number 1 reason why most marketers are not able to scale their advertising and maximize sales.

#3 Why Google and Facebook ads don’t work for most businesses & how to make them work.

#4 ​Why you won’t get any competitive advantage in the marketplace just by knowing Google Analytics.

#5 The number 1 reason why conversion optimization is not working for your business.

#6 How to advertise on any marketing platform for FREE with an unlimited budget.

​#7 How to learn and master digital analytics and conversion optimization in record time.



   

My best selling books on Digital Analytics and Conversion Optimization

Maths and Stats for Web Analytics and Conversion Optimization
This expert guide will teach you how to leverage the knowledge of maths and statistics in order to accurately interpret data and take actions, which can quickly improve the bottom-line of your online business.

Master the Essentials of Email Marketing Analytics
This book focuses solely on the ‘analytics’ that power your email marketing optimization program and will help you dramatically reduce your cost per acquisition and increase marketing ROI by tracking the performance of the various KPIs and metrics used for email marketing.

Attribution Modelling in Google Analytics and BeyondSECOND EDITION OUT NOW!
Attribution modelling is the process of determining the most effective marketing channels for investment. This book has been written to help you implement attribution modelling. It will teach you how to leverage the knowledge of attribution modelling in order to allocate marketing budget and understand buying behaviour.

Attribution Modelling in Google Ads and Facebook
This book has been written to help you implement attribution modelling in Google Ads (Google AdWords) and Facebook. It will teach you, how to leverage the knowledge of attribution modelling in order to understand the customer purchasing journey and determine the most effective marketing channels for investment.

About the Author

Himanshu Sharma

  • Founder, OptimizeSmart.com
  • Over 15 years of experience in digital analytics and marketing
  • Author of four best-selling books on digital analytics and conversion optimization
  • Nominated for Digital Analytics Association Awards for Excellence
  • Runs one of the most popular blogs in the world on digital analytics
  • Consultant to countless small and big businesses over the decade
error: Alert: Content is protected !!