Tracking Facebook ‘Likes’ and ‘Unlikes’ in Google Analytics

This article is in conjunction with the article ‘Guide to Social Interactions Tracking in Google Analytics‘ where I introduced the concept of social interactions tracking in Google Analytics and explained in great detail, what social interactions are and what are the requirements, for such type of tracking set up.

In this article, I will explain tracking Facebook Social Interactions (only Facebook ‘like’ and ‘unlike’ events) in order to keep the article short.

Once you understand how to track ‘like’ and ‘unlike’, you can then easily track other Facebook events like: ‘send’, ‘comment added’ and ‘comment removed’.

Following is the process for tracking Facebook ‘likes’ and ‘unlikes’ in Google Analytics, in a nutshell:

code sequence

Step-1: Install Facebook JavaScript SDK on your website.

Step-2: Add Facebook Social Interactions tracking code on your website. Place this code right below the Facebook Social Interactions tracking code.

Once you have implemented Facebook SDK and social interacting codes and tracked few Facebook social interactions (like Facebook ‘like’), you can then see the data in your GA view by navigating to: Acquisition > Social > Plugins report and then clicking on ‘Social Network and Action’ tab:

facebook like unlike

Get the App ID before Installing Facebook JavaScript SDK

You can also track Facebook social interactions in Google Analytics without using Facebook App ID.

However, I would strongly suggest getting Facebook App ID first because it has many advantages beyond social interaction tracking.

Through Facebook App ID, Facebook can identify your website, which can later be used for adding open graph protocol data, Facebook authentication, Facebook app analytics, etc.

In order to get the Facebook App ID, you first need to create a new Facebook App for your website.

Attribution Modelling in Google Analytics and Beyond
Attribution Modelling in Google Ads and Facebook

Get my best selling books on Attribution Modelling

  • Learn to implement attribution modelling in your organisation
  • Understand the customer purchase journey across devices
  • Determine the most effective marketing channels for investment

 Click book covers to find out more

Create a new Facebook APP for your website

We are going to create a new Facebook app just to get the app ID, which will later be used in installing Facebook JavaScript SDK on your website and to install various Facebook plugins (like the Facebook ‘like’ button).

Follow the steps below to create a new Facebook APP:

Step-1: Login to your Facebook account.

Step-2: Go to https://developers.facebook.com/apps  and then click on ‘+ Add a New App‘ button:

add a new app button

You will now see a dialog box like the one below:

add a new app for website

Since we are creating Facebook app for website, click on the ‘WWW‘ website icon.

Step-3: Type the name of your new APP and then click on ‘create new Facebook App ID’.

create new facebook app id


Step-4
: Enter your contact email, select ‘Business‘ from the ‘category‘ drop-down menu and then click on ‘Create App Id’ button.

create new app id

Note: You need to enter your contact email so that later you can make your Facebook APP available to the General Public.

You will now see the code for setting up ‘Facebook SDK for JavaScript’:

facebook APP ID

You don’t need to add this code to your website at this time, in order to install Facebook JavaScript SDK. Just pay close attention to the code and look for the unique Facebook APP ID and the special method called window.fbAsyncInit. Note down your Facebook App ID.

The window.fbAsyncInit method is very important because it calls the FB.init() method.

The method FB.init() is used to initialize and setup the Facebook JavaScript SDK. This is the method which enables Facebook API for you.

Step-5: Scroll down the page, enter the URL of your website (in the ‘site URL’ text box) and then click on the ‘next’ button:

about your website

Step-6: Click on the ‘Skip to Developer Dashboard’ link as shown below:

skip to developer dashboard

Step-7: Click on the ‘App Review’ link, found in the left hand navigation menu and then switch the toggle button to ‘Yes’:

make app public

Now your Facebook APP is available to the general public.

Do you want expert help in setting up/fixing GA4 and GTM?

If you are not sure whether your GA4 property is setup correctly or you want expert help migrating to GA4 then contact us. We can fix your website tracking issues.

Note: This is a bonus section. It has nothing to do with tracking Facebook events in Google Analytics. I have added this section to this article because I find it very useful and worth sharing.

Following are the advantages of linking your Facebook App with your existing Facebook Fan Page:

#1 Your Facebook Fan Page will turn into a powerful Facebook App Page, which can not only enhance your users’ experience on Facebook but also give you the ability to use Facebook Analytics for apps in addition to Facebook Insight:

facebook analytics app

#2 When a user likes an article on your website, it will show up on the user’s timeline activity, which can then be seen by the user’s friends. This way your article can get additional exposure on Facebook.

To link your Facebook App with your existing Facebook Fan Page follow the steps below:

Step-1: Go to your Facebook Fan page, click on the ‘About‘ link.

Step-2: Under the ‘Page Info‘ section, change the category to ‘Brands and Products‘ and sub-category to ‘App Page‘ as shown below:

fan page category

Step-3: Go to https://developers.facebook.com/apps and then click on your app.

app details

Step-4: Click on ‘App Details‘ link as shown below:

Step-5: Scroll down to ‘Contact Info’ section, click on the ‘Select an existing page‘ drop-down menu:

select an existing page

Step-6: Select your Facebook fan page and then click on the ‘save changes’ button:

select facebook app page

Install Facebook JavaScript SDK on your website

Once you have got your Facebook App ID, you now need to install Facebook JavaScript SDK on your website, in order to track Facebook events in Google Analytics. Follow the steps below:

Step-1:  Go to https://developers.facebook.com/docs/plugins/like-button, enter your Facebook Fan Page URL in the ‘URL to Like’ text box and then click on the ‘Get Code’ button:

facebook like button code

Once you have clicked on the ‘Get Code’ button, you will then see the Facebook Javascript SDK code (which contains your Facebook APP ID) along with Facebook Like and Share button code, as shown below:

select facebook app id


Step-2
: Copy-paste the Facebook JavaScript SDK code, right after the opening <body> tag on each page of your website.

Step-3: Copy-paste the plugin code (Facebook ‘like’ button code), wherever you want the plugin to appear on your website.

Note: You can track only those Facebook actions for which you have installed the right Facebook plugin on your website. For e.g. if you want to track Facebook ‘send’ event then you first need to install Facebook ‘send’ button on your website.

If you want to track Facebook ‘comment’ then you first need to install Facebook ‘comment’ plugin on your website. In order to learn more about social interactions tracking in general, read this article: Guide to Social Interactions Tracking in Google Analytics

Add Facebook App ID to your Facebook SDK code

If the Facebook SDK code is already installed on your website, then you need to find it and add your APP ID to it.

In the following cases, the Facebook JavaScript SDK is already installed on your website:

  1. If you are already using a Facebook Plugin (like Facebook Like button) on your website.
  2. If you are using an SEO plugin which lets you add Facebook open graph protocol metadata to your website. As soon as you enable the open graph metadata setting, via your plugin, Facebook SDK is automatically installed on your website.

But even if the Facebook SDK is already installed on your website, you should still add your App ID to it.

Since I use the ‘WordPress SEO by Yoast‘ plugin on my website, all I have to do to add the App ID to my Facebook JavaScript SDK code,  navigate to WordPress Admin panel > SEO > Social > Facebook and enter my Facebook APP ID: 

facebook app id wordpress

Note (1): Even if you use Google Tag Manager, you still need to copy-paste the Facebook JavaScript SDK code right after the opening <body> tag on each page of your website. This is because the Facebook JavaScript SDK code can’t be deployed via GTM.

Note (2): You need to add Facebook JavaScript SDK code only once on your website.

Note (3): You can also track Facebook social interactions in Google Analytics without creating a new Facebook App and without using Facebook App ID. I have used Facebook App ID because it has many advantages, as explained before.

Note (4): You don’t need to add the Facebook JavaScript SDK code every time you install a new Facebook plugin on your website. Though Facebook will ask you to do so.

Introduction to Facebook Social Interactions Tracking

Before you can track Facebook likes and Unlikes in Google Analytics, you first need to capture them. You can capture these events by using a method called FB.Event.subscribe(). This method is used to subscribe to a Facebook event (like liking a page) and to define callback function:

Syntax: FB.Event.subscribe(event, callback)

Here event is the name of event type. It can be:

#1 edge.create – this Facebook event is fired when someone likes the page using Facebook like button

#2 edge.remove – this Facebook event is fired when someone unlikes the page using Facebook like button

#3 message.send – this Facebook event is fired when someone use the Facebook ‘send’ button.

#4 comment.create – this Facebook event is fired when someone posts a comment using Facebook Comments Plugin

#5 comment.remove – this Facebook event is fired when someone deletes a comment in the Facebook Comments Plugin

callBack is the callback function that is triggered when a Facebook event is fired.

Through callback function, you can send Facebook events to Google Analytics via the ‘send’ command or push Facebook events into GTM data layer.

Following is an example of how you can use the callback function to send Facebook ‘like’ event to Google Analytics via the ‘send’ command:

FB.Event.subscribe(‘edge.create’, function(targetURL){

 ga(‘send’, ‘social’, ‘Facebook’, ‘like’, targetUrl);

});

Similarly, you can use the callback function to send Facebook ‘unlike’ event to Google Analytics via the ‘send’ command:

FB.Event.subscribe(‘edge.remove’, function(targetURL){

 ga(‘send’, ‘social’, ‘Facebook’, ‘unlike’, targetUrl);

});

Add Facebook Social Interactions tracking code on our website.

At this stage, we now have the new Facebook App ID and we have the Facebook JavaScript SDK and Facebook Social Plugins installed on our website. All we have to do now is to add the Facebook Social Interactions tracking code on our website. Copy-paste the following code right after your Facebook JavaScript SDK code on each page of your website:

<!-- Facebook Social Interaction Tracking -->
<script>
 window.fbAsyncInit = function(){
  FB.Event.subscribe('edge.create',function(targetURL) {
       ga('send', 'social', 'Facebook', 'like', targetURL);   

      }
  );

   FB.Event.subscribe('edge.remove',function(URL) {
       ga('send', 'social', 'Facebook', 'unlike', URL);   

      }
  ); 

  FB.Event.subscribe('message.send',function(location) {
       ga('send', 'social', 'Facebook', 'send', location);   

      }
  );  

   FB.Event.subscribe('comment.create',function(href) {
       ga('send', 'social', 'Facebook', 'comment added', href);   

      }
  ); 

   FB.Event.subscribe('comment.remove',function(loc) {
       ga('send', 'social', 'Facebook', 'comment deleted', loc);   

      }
  );  

};
</script>
<!-- Facebook Social Interaction Tracking -->

Remember the sequence in which, the various tracking codes will be added to your website:

code sequence

Following is a visual summary of tracking Facebook social interactions tracking in Google Analytics:

tracking facebook likes without gtm
  1. facebook.com Referral Traffic in Google Analytics Explained
  2. Facebook Attribution and Conversion Windows Tutorial
  3. Facebook ROI Calculation, Analysis and Examples
  4. How to set up the Facebook Attribution Tool
  5. How to Setup Funnels in Facebook Ads Manager
  6. How to use funnels for your Facebook Ad Campaigns
  7. How to advertise on Facebook for FREE with unlimited budget
  8. Secret to Setup Facebook Pixel Tracking Correctly in Google Tag Manager
  9. Facebook Pixel vs Google Analytics Data Discrepancies
  10. facebook.com Referral Traffic in Google Analytics Explained
  11. Learn to set up Facebook Pixel via Google Tag Manager
  12. Send Facebook Pixel Purchase Event via Google Tag Manager
  13. Open Graph Protocol for Facebook Explained with Examples
  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 track Facebook events via Google Tag Manager
  21. How to use the Facebook Event Setup Tool
  22. How to use the Facebook Pixel Helper to Test Facebook Events
  23. How to automatically turn off Facebook ads on weekends
  24. How to name Facebook Ad Campaigns like a Pro

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