Tracking Events Using Google Analytics

Google Analytics is a very popular tool for tracking visits, pageviews, and lots of other metrics for your website. Here’s a handy little trick which allows you to track any event you want on your site, a click on a specific link for example, and have it integrated into your analytics data.

1) First, make sure you are already using Google Analytics on your site (or the page that you want to track events).

2) Second, add this snippet just below the Google Analytics code:

<script type=”text/javascript”>
function trackEvent(category,url,description) {
try {
pageTracker._trackEvent(category, description, document.location.pathname);
setTimeout(‘document.location = “‘ + url + ‘”‘, 100);
} catch(err){}
}
</script>

4) Whenever you want to track something, you need to add a call to trackEvent, for example:

<a onclick=”trackEvent(‘Outbound Links’,'http://google.com’,'Google’);return false;” href=”http://google.com”> Google </a>

5) Now log into Google Analytics, click on ‘Content’ -> ‘Event Tracking’ you will be able to see clicks on the above link to Google (you might not see it right away!)

Happy tracking!

Tags: , , ,

Leave a Reply

Spam protection by WP Captcha-Free