Google Analytics provides some hooks for tracking Facebook likes on your web pages as well as other social interactions. This lets you see how many people have "liked" your pages, and which pages are most popular on Facebook!
This is a quick guide on how to set up Facebook like tracking in Google Analytics.
First, in your <head>
tag, put your Google Analytics tracking code as normal.
<head>
...
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXX-X']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</head>
Then, immediately after your <body>
tag, add the Facebook script according to their documentation.
<body>
<div id="fb-root"></div>
<script>(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/all.js#xfbml=1&appId=XXXXXXXXXXXXXXX";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
...
Now here comes the magic. Immediately after the Facebook script, add a new script tag with the following:
<script>
window.fbAsyncInit = function() {
FB.Event.subscribe('edge.create', function(targetUrl) {
_gaq.push(['_trackSocial', 'facebook', 'like', targetUrl]);
});
FB.Event.subscribe('edge.remove', function(targetUrl) {
_gaq.push(['_trackSocial', 'facebook', 'unlike', targetUrl]);
});
FB.Event.subscribe('message.send', function(targetUrl) {
_gaq.push(['_trackSocial', 'facebook', 'send', targetUrl]);
});
};
</script>
This will track likes, unlikes and shares of all your pages and send the data to Google Analytics.
Don't forget to actually add the "Like" button somewhere on your page!
<div class="fb-like" data-send="false" data-width="300"></div>
Now, in Google Analytics, you will be able to find the reports by clicking Traffic Sources -> Social -> Plugins.