I’m using GAPI to try and create simple reports from our Google Analytics data.
My internal customer would like to see an overview with 2 fields per page: pageviews and unique visitors.
I have the pageviews part working, but I cannot get the unique visitors part to work.
This is my current code. The filter would be a parameter they can change, but is hardcoded for now:
require 'gapi.class.php';
$ga = new gapi(ga_email,ga_password);
$filter = 'pagepath =~ ^/home$';
$ga->requestReportData(ga_profile_id,
array('pagepath'),
array('pageviews'),
'pageviews',
$filter,
'2010-10-01',
'2010-10-31');
and then some code to show the results which is not really relevant here.
As said, that works, but if I add ‘visitors’ to the metric like this:
$ga->requestReportData(ga_profile_id,
array('pagepath'),
array('pageviews','visitors'),
'-pageviews',
$filter,
'2010-10-01',
'2010-10-31');
Then I get this error:
PHP Fatal error: Uncaught exception 'Exception' with message 'GAPI: Failed to request report data. Error: "Illegal combination of dimensions and metrics"' in gapi.class.php:218
Stack trace:
#0example.filter.php(24): gapi->requestReportData('30296235', Array, Array, '-pageviews', 'pagepath =~ ^/h...', '2010-10-01', '2010-10-31')
#1 {main}
thrown in gapi.class.php on line 218
The Google Analytics API restricts what combinations it can show you. Specifically,
visitors(not to be confused withvisits) is heavily restricted as to what metrics it can be used with.visitorsandhourare the two most heavily restricted metrics and dimensions you can query in Google Analytics. So, the above combination you tried is invalid, and Google refuses to compute it. Just switch it tovisits, and it’ll work.Notice on this valid combinations page, almost all of the metrics restrict
visitors.The reason for this is likely that
visitorsis much more computationally expensive, occasionally impossible to compute, and, to be honest, not particularly useful.visitoris aggregate ofvisits, but constrained to a particular cookie set, whereas a visit is just one particular session for a visitor. It’s also less useful, sincevisitorsdoes not map 1:1 to people. For example, I access StackOverflow from Safari, Chrome, and Firefox on my home and work computers, as well as from my iPhone. SO sees me as 7 visitors! It’s not a useful way of tracking users. Visit, by being more limited in what it offers, provides a better view.Also, since being a visitor spans multiple sessions, you can’t quite have a
pagePathfor them.