Sam Starling
9 November 2011

Google Analytics and "Like" Buttons

This is a quick post to show you how to track user interactions with “social” buttons inside Google Analytics.

Luckily, the three main social interaction buttons that you see around the web (Facebook’s ‘Like’, Twitter’s ‘Tweet’, and Google’s ‘+1’), all have ways of letting you perform an action when they’re clicked.

To do so, you just need to add some Javascript into your page along these lines:

var url = document.URL;
var category = 'Social Media';

/* Facebook */
FB.Event.subscribe('edge.create', function(href, widget) {
  _gaq.push(['_trackEvent', category, 'Facebook', url]);
});

/* Twitter */
twttr.events.bind('tweet', function(event) {
  _gaq.push(['_trackEvent', category, 'Twitter', url]);
});

/* Google */
function plusone_vote(obj) {
  _gaq.push(['_trackEvent', category, 'Google', url]);
}

That code assumes that you’ve got _gaq available as a variable – that is, this Javascript should come after your the Google Analytics tracking snippet.

I’ve assumed you’ve already got FB and twttr available as objects, which will be true if you’ve got Facebook and Twitter buttons loaded in the usual way.