It is common knowledge that Google Analytics (hereinafter GA) is a powerful tool for collecting data about user activity on your site - the relevant statistics is available at GA site.But sometimes web developer needs to manipulate (to collect, analyze, build graphs) the statistics on the side of the site. You can use Google Analytics PHP Interface (hereinafter GAPI). Its usage is the very question we are going to inquire into here, and also to construct a chart which will be based on the statistics, gathered with the help of Google Charts API.
As you know statistics gathered by GA is only available online http://www.google.com/analytics/. But very often GA standard functionality does not allow for collecting the statistics you want. To select statistics that best caters to your specific needs you can access «Event Tracking».
Let’s imagine that we want to collect statistics on specific authors blogging on //internetdevels.com the one you are reviewing now. You need to insert into standard GA code (see below):
var _gaq = _gaq || []; _gaq.push(['_setAccount', 'your_ga_key']); _gaq.push(['_trackPageview']); (function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })();
the following line:
_gaq.push(['_trackEvent', 'Blog author', '%username%', '%blog_title%']);
No need to explain that you should substitute the author’s name and the name of the blog on the PHP side on the page.tpl.php or hook_page_alter. Similarly, you can collect data download speeds, as it is described here.
So, let’s work more closely on the tracking events method.
_trackEvent(category, action, opt_label, opt_value, opt_noninteraction);
category (required) — the generalized name of the events you want to monitor. In our case it's «Blog author»;
action (required) — the option by which we group events. In our case it’s a nickname of the author;
label (optional) — an option that helps to create additional categorization of events. For this option we use the name of the blog;
value (optional) — an integer that allows for passing additional information about the event. It doesn’t need to be used hereby;
non-interaction (optional) — a boolean parameter, which having being installed in TRUE, indicates that this event is supposed to be ignored as long as the failure statistics being calculated. No need to use it, either.You can get a closer look at the details related to tracking events here.