How to Track Your Social Links with Google Analytics

Quick and easy one. See the links to my social profiles on your right? I’ve been displaying those for years, but I never knew if they were ever clicked on, and if they were, which ones are the most popular ones? Perhaps I have to get rid of some of them, and make other brighter to get more attention? Or maybe simply switch places and keep the most relevant ones on the top left?

After thinking for a while, I figured out that Google Analytics can help me solve this in no time. I heard about Event Tracking with Google Analytics quite some time ago, and I was actually using it to find out how many people were clicking the “more widgets” link in my sidebar (not much), but this time it’s about tracking outgoing links to your social profiles and not only — you can use this technique to track any outgoing links, just make sure you keep it simple.

First things first: go through the Event Tracking guide on Google and make sure you understand what’s going on in theory. Then find out a good javascript code or jQuery selector to bring your social links together, mine is:

jQuery(".social a[rel=me]")

Then use that code to assign an onclick event to each of the social links:

jQuery(".social a[rel=me]").click(function() {
    /* some code goes here */
});

And finally add the magic Google code which does two things: A, tells the Google Analytics servers that your links was pressed and B, slows down the browser relocation for a few milliseconds so that A has a little bit more time. I know slowing down your website doesn’t sound right, but I guess that this is the only way to track visitors with low internet speed (do you have any of those?) Below is the full code that I use on my blog, you can use that as is, but keep in mind that you’ll have to modify your jQuery selector in order to match your website structure:

jQuery("div.social a[rel=me]").click(function() {
	_gaq.push(['_trackEvent', 'Social', 'Profile', jQuery(this).attr('href')]);
	setTimeout('document.location = "' + jQuery(this).attr('href') + '"', 100);
	return false;
});

As you can see, I push the event into the Social category with a Profile action, and then also pass the href attribute of the object that was clicked (a social profile in our case.) All this is then visible in your Google Analytics reports under the Content – Event Tracking section. It’s up to you how you name these, but I’m thinking globally, thus having a Social category which can house Profile clicks, and then extend it to track Share events like Facebook Likes, Retweets, etc.

Hope this helps you sort some things out and gives you a better feel of what Google Analytics is capable of. There’s much more to learn, and Google’s Manuals are there to help you. Don’t be afraid to experiment, and keep your eyes on the javascript debugger console (Chrome or Firefox) as that’s usually the first place to look at when things go wrong.

Thanks for reading and sharing. Hope you have a great New Year party, or hope you did if you’re reading this in 2011 already ;) Cheers!

About the author

Konstantin Kovshenin

WordPress Core Contributor, ex-Automattician, public speaker and consultant, enjoying life in Moscow. I blog about tech, WordPress and DevOps.

2 comments