How do I track that little Facebook "Like" button in Google Analytics?

We get asked very often how to track Facebook “Like” buttons inside Google Analytics, so here’s a post that will show you how to do it.  There are basically three steps – first, you need to get the Like button on your pages using the XFBML / Javascript version of the Like Button.  Next, you’ll need to fire some Google Analytics code in a Facebook event that tells us when a successful “like” action has occurred.  Lastly, you need to find where it all ends up in Google Analytics and take action based on your new data!

1. Get a Facebook Like Button on your page(s)

The first step is to actually get a Facebook Like Button on your page. The quickest way to do this is to go here:

http://developers.facebook.com/docs/reference/plugins/like

Just fill out the form and click the “Get Code” button…you’ll see something like this pop up:

Now, you’ve got two options here. The iFrame version is admittedly the easiest to install, but unfortunately you don’t have any way to track when someone actually clicks and successfully “likes” your page as a result. There’s no onClick event you can use for an, or for a tag for that matter. And even if you could, the click itself doesn’t tell you if someone actually successfully liked your page or not – remember, they have to log into Facebook first, via a popup that might even be blocked in some browsers, so your click and Like counts may be very different.So instead, we’re going to use the XFBML version (highlighted).To implement this, you’re going to need to use the Facebook Javascript SDK – basically it works like this (and you can view the source of this page – you’re looking at a working example):

    • Update your tag to include

xmlns:fb='http://developers.facebook.com/schema/' xmlns:og='http://opengraphprotocol.org/schema/'

    • Add the proper Open Graph Protocol meta tags – this is not necessarily required but a good thing to do.
    • Reference the Facebook Javascript library – we recommend you use the following asynchronous version (make sure to put your own App ID in there):
    • Add the XFBML that you generated above (make sure to update it with your own page URL as the source and add any available attributes you might want) where you’d like your button to appear on your page:

2. Fire a Google Analytics Event when a “Like” action occurs

OK, at this point you’ve got your Like button up and running. Now in order to track these in Google Analytics, we’re going to use Event Tracking to fire an event whenever someone successfully Likes the page. You can choose to set this up in whatever hierarchy you like, but here’s one you might want to use:

Category: “facebook”
Action: “like”
Label: URL of the page that was “liked”

Using the _trackEvent() method of the pageTracker object, the Google Analytics javascript code would look like this:


pageTracker._trackEvent('facebook','like', href);

Note: This is the synchronous version of the Google Analytics code and assumes a javascript variable called href contains the URL of the page that’s being “liked”

So the last step in this process is to actually fire this code only when a successful Facebook like occurs. To do this, we’re going to subscribe to an event that Facebook exposes to us when the successful “like” action occurs. When we detect that event, we’ll fire our _trackEvent code. Here’s the code that will do it:

Note: The safest placement of this code is somewhere BELOW your standard Google Analytics tracking code – this will ensure that the pageTracker object has been defined before you try to use it

That’s it!  Feel free to get creative here…want to use a Custom Variable to create a segment of either visitors (scope = 1) or sessions (scope = 2) that “Liked” something?  Then all you would have to do is something like this (make sure your slot 1 is available and you want this at a visitor scope for the exact code below):

Want to stick this data into a database so you can use it for any number of customized reasons? Just fire some AJAX in there. Just about anything you can accomplish with Javascript can be done with each Like event.

3. Find that data in Google Analytics and USE it!

Now, you can log into your Google Analytics account and head over to the Content > Event Tracking reports and take a look at the Categories, Actions and Labels as you’ve defined them:

 

Note that here we’ve drilled down to the Category of “facebook” and the action of “like” – now we’re looking at a list of the URL’s we passed in – or in our case the actual blog posts that people “liked.” They’re a bit ugly since they’ve been formatted for proper URL encoding, but hovering over the label itself will let you see the URL that was “liked” pretty clearly.

So what do we do with it?

Well, this is one more set of data that can be used to identify what content resonates with your user base.  Which are the popular themes of blogs?  Which are the authors that our visitors like the best?  Which topics do people like to read about?  We can look at the Site Usage tab right here to understand if people who like certain blog posts are any more engaged (think bounce rate, time on site, average pageviews per visit) than others.  We can look at our Ecommerce tab and find out if people who like certain pages are more or less likely to, say, sign up for one of our Google Analytics trainings.  With this knowledge, we can tailor the content that we write to the things that people like the best and the things that get people to engage with our site and convert on our goals.

And don’t forget about your Advanced Segments! Below you can see a quick Advanced Segment configuration that will quickly let you compare those Facebook Likers to any other segment of traffic you care to define:

 

Hope this helps a few of you out, and we’d love to hear how you’re using this in the comments!

David Booth

David is a founding partner and principal consultant at Cardinal Path, as well as an author, instructor, adjunct professor, and regular speaker. As a consultant, David has advised and worked with companies and organizations across five continents in web analytics and business intelligence, statistical analysis and testing, technology selection and deployment, and online & search marketing. David is the author of Google AdWords Essential Training (Lynda.com, 2011) and Google Website Optimizer Essential Training (Lynda.com, 2010) and served as a technical editor of Performance Marketing with Google Analytics (Wiley, 2010) and Google Analytics Essential Training (Lynda.com, 2011). He is currently working on yet another project to be published in the Spring of 2012, and he teaches a masters level course on web analytics and online marketing at the Monterey Institute of International Studies. As an instructor and speaker, David has keynoted numerous events and has led seminars and sessions focused on digital analytics and search & conversion marketing all around the world for audiences ranging from C-level executives to technical implementation teams. He has been involved in web application development as an engineer and consultant since the late 1990's, beginning his career with Intel Corporation. He was a founder and partner at WebShare, LLC through its merger, and spent two years with the United States Peace Corps developing and deploying websites and web applications to attract grants and international aid for Guatemalan NGOs and local development organizations. David earned his Master of Business Administration in International Management from the Monterey Institute of International Studies and holds a B.S. in Electrical and Computer Engineering from the University of Illinois at Urbana-Champaign.

Share
Published by
David Booth

Recent Posts

Google Delays Third-Party Cookie Deprecation to 2025

Google announced on April 23 that it will again delay third-party cookie deprecation (3PCD) in…

3 days ago

Understanding Funnel Reports in GA4

Funnel reports have long been one of the most actionable reports in a marketing analyst’s…

5 days ago

GA4 Monetization Reports: An Overview

GA4’s Monetization reports provide organizations with simple but actionable views into the revenue-generating aspects of…

1 week ago

This website uses cookies.