The Blog

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:

Tracking Facebook Like Buttons with Google Analytics

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 <iframe>, or for a <fb:> 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 <html> 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):

  • <div id="fb-root"></div>
    <script>
    window.fbAsyncInit = function() {
    FB.init({appId: 'INSERT YOUR APP ID HERE', status: true, cookie: true,
    xfbml: true});
    };

    (function() {
    var e = document.createElement('script'); e.async = true;
    e.src = document.location.protocol +
    '//connect.facebook.net/en_US/all.js';
    document.getElementById('fb-root').appendChild(e);
    }());
    </script>

  • 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:

  • <fb:like href="http://www.somesite.com/somepage"></fb:like>

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:


<script>
FB.Event.subscribe('edge.create', function(href, widget) {
pageTracker._trackEvent('facebook','like', href);
});
</script>

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):


<script>
FB.Event.subscribe('edge.create', function(href, widget) {

pageTracker._setCustomVar(1,'facebookLiker','true',1);
pageTracker._trackEvent('facebook','like', href);
});
</script>

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:

Event Tracking for Facebook Like Buttons in Google Analytics

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 live Google Analytics or Website Optimizer Seminars for Success 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:

Google Analytics Advanced Segment for Facebook Like Button users

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

  • http://tylerfraser.com Tyler Fraser

    Terrific post. Thank you for laying it out in a clear way. I’ll be seeing this post again soon.

  • http://www.strategic-online-marketing.co.za Glynn

    Awesome post and a great help for tracking. Thanks – Have to bookmark this

  • rob

    I think I just identified a pretty major bug in Facebook’s ‘Like’ button, which allows webmasters to write data on FB’s social graph, which cannot be ‘deleted’ *if* they should they make a mistake when they initially implement the code into their website.

    And due to the way FB presents the tool and the code generator to the public, many webmasters do not read the docs (and just add the cut and paste code) and fail to add the meta tags required to admin the tool… which causes permanent problems later, if they ever want to admin the tool. In fact, most webmasters with this issue dont even know there an admin panel should have been dynamically generated for them when they first added the code, which they forever locked themselves out of.

    They post will probably be deleted soon… because ‘stickied’ threads in the developers’ forums tend to do that… but here is the most current status of the issue:

    http://forum.developers.facebook.net/viewtopic.php?pid=263145

    …this is a pretty major bug, imo. They need to stop the bleeding by getting a note on that ‘Like’ Button Code Generator asap, then trying to figure out a way they can remap the admins to the domains.

    But now they have an issue with authenticating the admin really has the website’s permission to admin their tool… and that’s a little sticky. I suggested they email the ‘tech contact’ listed in the whois of the domain in question. Otherwise you have a hijacking risk.

    Another issue is simply that the easiest solution may involve deleting the data associated with the fan’s actions, which means people waiting for the solution will eventually have their count go back to zero. And webmasters don’t get enough loves as is so I doubt they will take losing all their hard won contacts without a bit of a backlash. Especially when many are bloggers. I guess we’ll see what they say Monday.

    Anyway, thought you might be interested in following this.

    Take care,
    Rob

  • http://www.spydertrap.com Jason Douglas

    Hi! Thanks for posting this. One question: are you talking about the like button on facebook, or a like button placed on a blog post?

    I’ve already passed this article on to my dev team. Hope they understand it more than me :)

    • http://www.websharedesign.com/images/author-david-booth.jpg David

      Hi Jason – this is for a Like button placed on a blog post itself…you can see the like button near the top (next to Twitter and Google Buzz) and then again at the bottom of the blog post. Good luck!

  • http://www.optimise-firstfound.co.uk/ Andy @ FirstFound

    Thanks for this – it’s something I’d been wondering how to do, especially givent that the Like button is becoming fairly ubiquitous.

  • http://www.jonbparker.com Jon

    Wow, great post. I just implemented and works as you said!

  • http://amnavigator.com/blog/ Geno Prussakov

    Good one, Jason. Thank you for putting this together.

    I am using the “Like” button on every post of mine. This tracking capability through Google Analytics is another good reason to add it to my blog overall. Thanks again.

  • Reid

    Thanks for this. I’ve put it on my site and still waiting to see if it works. One question, though: if I’m using the new asynchronous code, would I then change the code you’ve provided to this:

    FB.Event.subscribe(‘edge.create’, function(href, widget) {
    _trackEvent(category, action, opt_label, opt_value)
    });

    I’m changing the pagetracker._ to the newer track event code, but maybe that’s not necessary?

    • http://www.websharedesign.com/images/author-david-booth.jpg David

      Hi Reid – the asynch version of an event call would look like this:
      _gaq.push(['_trackEvent', 'category', 'action', 'opt_label', opt_value]);

      Hope this helps!
      -Dave

  • Reid

    Thanks, David. Sorry for the boneheaded earlier comment. I figured out after I posted that I’d left out the _gaq.push part. But thanks for your comment.

    I was using the Google Analytics plugin on WordPress, and I had put that FB.Event.subscribe code in the header, and even though it did appear below the primary Analytics code, it still wasn’t firing. I was getting a “FB is undefined” in Firebug. So I put it in the footer, and that worked.

    Thanks a ton for this. I’d been looking for this for a long time.

  • Bobby

    Brilliant, thanks for the tip..

  • http://www.itsagreengreenworld.com EcoWarrior

    This is a great post . . . many thanks!

    One point . . . why not use the page title instead of href?

  • http://www.webhelder.com Jan de Vries

    Thanks fot the great post and tip we are fixen it right now!

  • jessica

    Thanks for the awesome post. I have one question, however — is it necessary to have a FB app id? After testing this out, I realized that I had left the “INSERT YOUR APP ID HERE” in the code.. and the callback on the edge.create event was still firing.. is there a problem with this? Do I need to enter a FB app ID there for some reason that is not obvious?

  • http://www.ronnau.com Anders Rønnau

    Excellent post. I was looking for the way to check what page the pagetracker was fired, and the “href” trick is brillant. Thanks!
    @EcoWarrior. You can do that – and if the button is in a widget on all pages, you want the href to tell you which page the clicker was on. ;)

Copyright © 2013, All Rights Reserved. Privacy and Copyright Policies.