WordPress & Google Analytics: Tracking Your Tag Cloud

Hey there! I'm currently working on a CLI tool to deploy WordPress apps to DigitalOcean. Check it out! It's free and open source.

I was doing some minor changes to my WordPress theme this week (have you noticed any?) — some SEO improvements and an easter egg ;) Anyhow, I came across my super duper tag cloud, which is one of the things I like most about my website, seriously. I heard enough people, mostly designers and UI experts say that the tag cloud is of no use! But I really really like it, which is why I decided to actually see how useful my tag cloud is.

Yes, this post is about tracking your WordPress tag cloud using Google Analytics, and I’ll share with you a short code snippet that you can dump into your functions.php file, then start tracking yours!

Some of you might say “hey, just filter your Google Analytics reports using a /tag/ filter and you’ll pretty much find out the usage”. Right, but wrong. You see, the actual tag cloud is not the only place from which people might land on the tag archive pages. There may be posts linking to certain tag archives, there may be lists of tags underneath each post (like on my blog), or even Google can land somebody on your tag archives.

This means that in order to view the accurate results of your tag cloud usage, you’ll have to do something better than that. And here’s my simple solution for your functions.php file:

function my_wp_tag_cloud($return) {
	$tracking_code = "?utm_source=internal&utm_medium=tag_cloud&utm_campaign=tag_cloud";
	$return = preg_replace('/href='([^']+)'/','href='1' . $tracking_code . ''', $return);
	return $return;
add_filter('wp_tag_cloud', 'my_wp_tag_cloud', 10, 1);

This will append your tracking code to every tag in the cloud generated by the wp_tag_cloud built-in WordPress function. Now all you have to do is wait and look at your Google Analytics reports.

While writing this post, I also came up with an idea of using Google Analytics Events Tracking and I published a code snippet earlier in a post called How to Track Your Social Links with Google Analytics which uses jQuery to capture clicks on links to social media profiles. By looking at the links in the tag cloud we can come up with a simple jQuery selector to capture them all, then add an event upon click. Here’s the modified code:

jQuery("div.tag-cloud a").click(function() {
	_gaq.push(['_trackEvent', 'Internal', 'Tag Cloud', jQuery(this).attr('href')]);
	setTimeout('document.location = "' + jQuery(this).attr('href') + '"', 100);
	return false;

You can decide whichever method works best for you. Appending tracking code to links is more friendly since it works without javascript. Event tracking could be a little bit more neat and shiny, but the purpose of event tracking with Google Analytics is a little bit different than this. But anyways, one way or another, you’ll get your results in your analytics report.

Happy tracking, good luck and thanks for retweeting this post ;)

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.

1 comment