Google Analytics Proxy with Nginx

Here’s a quick tip! If you need to serve a specific script, stylesheet or any other file from your own domain, you can easily proxy it with nginx. A good example is the ga.js¬†file for Google Analytics. Here’s how I proxy it with nginx, in the server context:

# Google Analytics Proxy
rewrite ^/ga.js$ /ga/ last;
location /ga/ {
        proxy_pass http://www.google-analytics.com/ga.js;
        break;
}

This rewrites the ga.js filename to the /ga/ pseudo-directory, in the context of which I can use the proxy_pass directive to fetch the file from Google. This way I have total control over the file that’s being served and especially the HTTP headers, which I was after in the first place.

You can repeat the trick with basically any file, but keep in mind that each one is a little extra load on your server, so add a caching layer where possible.

Using the Google Analytics API in WordPress

There’s a great article over at Theme.fm on Using the Google Analytics Data Feed API in WordPress which covers some pretty interesting techniques to grab and make use of data stored in your Google Analytics profile, like what are your top 10 posts this month, or what were the most searched ones, etc. I’ve authored that article so feel free to ask questions ;)

WordPress & Google Analytics: Tracking Your Tag Cloud

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

More Google SEO Tips

“What are some simple ways that I can improve my website’s performance in Google?”

Yeah, they are simple enough… I bet you’ve been following the Google webmaster central blog and have seen the post about their new SEO starter guide. Well, as they said, everybody knows this stuff already, but they just wanted to remind us all with one handy little guide. That’s okay, there are some pretty interesting facts in the guide, especially the “avoid” sections.

You see, when you’re creating a webpage from scratch, it’s pretty easy to line with the rules. I mean the title tags, the meta description tags, linking structure, site navigation. Well, the real pain in the ass is when you get to edit a complete website with around 300-400 pages, that all have same titles, same descriptions and keywords tags, all done in plain html and a tabled structure. Oh, and it has a stupid javascript menu aswell, like in the 90’s ;)

I’ve also noticed that stuffing keywords in the page title makes absolutely no sense. I mean Google’s first results page (on almost any keyword or keyphrase) contains high ranked pages that have short, descriptive titles, so there really is no need to repeat your keywords 5-6 times in the title, I’d say it’s rather harmful doing so.

Almost forgot. Have you seen the new Google Analytics overview pages? Their new design rocks and the advanced segmentation too – you can get up to 4 factors onto your dashboard line chart, so if you’re using Google Analytics as a stats program, you won’t have to click anywhere else besides your dashboard. Though I still do encourage you to stop wasting time (and money) and start using Google Analytics further more than just a stats program. There’s plenty of information on the web about how to take advantage of all Google Analytics’ features.