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:
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:
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.
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:
You will now see a dialog box like the one below:
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’.
Step-4: Enter your contact email, select ‘Business‘ from the ‘category‘ drop-down menu and then click on ‘Create App Id’ button.
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’:
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:
Step-6: Click on the ‘Skip to Developer Dashboard’ link as shown below:
Step-7: Click on the ‘App Review’ link, found in the left hand navigation menu and then switch the toggle button to ‘Yes’:
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:
#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:
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:
Step-5: Scroll down to ‘Contact Info’ section, click on the ‘Select an existing page‘ drop-down menu:
Step-6: Select your Facebook fan page and then click on the ‘save changes’ button:
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:
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:
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:
- If you are already using a Facebook Plugin (like Facebook Like button) on your website.
- 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:
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:
Following is a visual summary of tracking Facebook social interactions tracking in Google Analytics:
Other articles on Facebook Pixel Tracking
- facebook.com Referral Traffic in Google Analytics Explained
- Facebook Attribution and Conversion Windows Tutorial
- Facebook ROI Calculation, Analysis and Examples
- How to set up the Facebook Attribution Tool
- How to Setup Funnels in Facebook Ads Manager
- How to use funnels for your Facebook Ad Campaigns
- How to advertise on Facebook for FREE with unlimited budget
- Secret to Setup Facebook Pixel Tracking Correctly in Google Tag Manager
- Facebook Pixel vs Google Analytics Data Discrepancies
- facebook.com Referral Traffic in Google Analytics Explained
- Learn to set up Facebook Pixel via Google Tag Manager
- Send Facebook Pixel Purchase Event via Google Tag Manager
- Open Graph Protocol for Facebook Explained with Examples
- Google Analytics for Facebook Tutorial
- Facebook Attribution Models Tutorial
- GTM Server Side Tagging for Facebook Tutorial
- The impact of Apple IOS 14.5 update on Facebook Ads
- How to add Facebook Pixel to WordPress Website
- How to add Facebook Pixel to WordPress without a Plugin
- How to track Facebook events via Google Tag Manager
- How to use the Facebook Event Setup Tool
- How to use the Facebook Pixel Helper to Test Facebook Events
- How to automatically turn off Facebook ads on weekends
- How to name Facebook Ad Campaigns like a Pro
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:
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:
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.
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:
You will now see a dialog box like the one below:
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’.
Step-4: Enter your contact email, select ‘Business‘ from the ‘category‘ drop-down menu and then click on ‘Create App Id’ button.
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’:
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:
Step-6: Click on the ‘Skip to Developer Dashboard’ link as shown below:
Step-7: Click on the ‘App Review’ link, found in the left hand navigation menu and then switch the toggle button to ‘Yes’:
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:
#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:
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:
Step-5: Scroll down to ‘Contact Info’ section, click on the ‘Select an existing page‘ drop-down menu:
Step-6: Select your Facebook fan page and then click on the ‘save changes’ button:
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:
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:
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:
- If you are already using a Facebook Plugin (like Facebook Like button) on your website.
- 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:
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:
Following is a visual summary of tracking Facebook social interactions tracking in Google Analytics:
Other articles on Facebook Pixel Tracking
- facebook.com Referral Traffic in Google Analytics Explained
- Facebook Attribution and Conversion Windows Tutorial
- Facebook ROI Calculation, Analysis and Examples
- How to set up the Facebook Attribution Tool
- How to Setup Funnels in Facebook Ads Manager
- How to use funnels for your Facebook Ad Campaigns
- How to advertise on Facebook for FREE with unlimited budget
- Secret to Setup Facebook Pixel Tracking Correctly in Google Tag Manager
- Facebook Pixel vs Google Analytics Data Discrepancies
- facebook.com Referral Traffic in Google Analytics Explained
- Learn to set up Facebook Pixel via Google Tag Manager
- Send Facebook Pixel Purchase Event via Google Tag Manager
- Open Graph Protocol for Facebook Explained with Examples
- Google Analytics for Facebook Tutorial
- Facebook Attribution Models Tutorial
- GTM Server Side Tagging for Facebook Tutorial
- The impact of Apple IOS 14.5 update on Facebook Ads
- How to add Facebook Pixel to WordPress Website
- How to add Facebook Pixel to WordPress without a Plugin
- How to track Facebook events via Google Tag Manager
- How to use the Facebook Event Setup Tool
- How to use the Facebook Pixel Helper to Test Facebook Events
- How to automatically turn off Facebook ads on weekends
- 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.