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 to get Facebook App ID first, because it has many advantages beyond social interactions 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.

Get the E-Book (37 Pages)

Learn to read e-commerce reports book banner

Get the E-Book (104 Pages)

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.

Link your Facebook App with your existing Facebook Fan Page (Optional)

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 user’s friends. In 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.

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

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

One 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 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 a SEO plugin which let you add Facebook open graph protocol meta data to your website. As soon as you enable the open graph meta data 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 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 which 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
Related Articles

Learn about the Google Analytics Usage Trends Tool

The Google Analytics usage trend is a new tool which is used to visualise trends in your Google Analytics data and to perform trend analysis.


Take your knowledge of Web Analytics to the next level. Checkout my web analytics training course.


Become a Web Analytics & Conversion Optimization PRO with my FIVE most popular FREE e-Books

Select the ebook you want:

E-Book #1: Best Excel Charts For Data Analysis And Reporting (40 Pages)

E-Book #2: Beginners Guide To Google Analytics Attribution Modelling (52 Pages)

E-Book #3: Beginners Guide To Google Tag Manager (72 Pages)

E-Book #4: Google Tag Manager Data Layers (62 Pages)

E-Book #5: Key Performance Indicator - KPI (32 Pages)

Take your Analytics knowledge to the next level. Checkout my Best Selling Books on Amazon

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 Beyond
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.

Himanshu Sharma

Certified web analyst and founder of OptimizeSmart.com

My name is Himanshu Sharma and I help businesses find and fix their Google Analytics and conversion issues. If you have any questions or comments please contact me.

  • Over eleven years' experience in SEO, PPC and web analytics
  • Google Analytics certified
  • Google AdWords certified
  • Nominated for Digital Analytics Association Award for Excellence
  • Bachelors degree in Internet Science
  • Founder of OptimizeSmart.com and EventEducation.com

I am also the author of three books:

error: Alert: Content is protected !!