public function gapi::requestReportData in Google Analytics Statistics 7
Same name and namespace in other branches
- 7.2 includes/gapi.class.php \gapi::requestReportData()
- 7.x inc/gapi.class.php \gapi::requestReportData()
Request report data from Google Analytics
$report_id is the Google report ID for the selected account
$parameters should be in key => value format
Parameters
String $report_id:
Array $dimensions Google Analytics dimensions e.g. array('browser'):
Array $metrics Google Analytics metrics e.g. array('pageviews'):
Array $sort_metric OPTIONAL: Dimension or dimensions to sort by e.g.('-visits'):
String $filter OPTIONAL: Filter logic for filtering results:
String $start_date OPTIONAL: Start of reporting period:
String $end_date OPTIONAL: End of reporting period:
Int $start_index OPTIONAL: Start index of results:
Int $max_results OPTIONAL: Max results returned:
File
- includes/
gapi.class.php, line 115
Class
- gapi
- GAPI - Google Analytics PHP Interface
Code
public function requestReportData($report_id, $dimensions, $metrics, $sort_metric = null, $filter = null, $start_date = null, $end_date = null, $start_index = 1, $max_results = 30) {
$parameters = array(
'ids' => 'ga:' . $report_id,
);
if (is_array($dimensions)) {
$dimensions_string = '';
foreach ($dimensions as $dimesion) {
$dimensions_string .= ',ga:' . $dimesion;
}
$parameters['dimensions'] = substr($dimensions_string, 1);
}
else {
$parameters['dimensions'] = 'ga:' . $dimensions;
}
if (is_array($metrics)) {
$metrics_string = '';
foreach ($metrics as $metric) {
$metrics_string .= ',ga:' . $metric;
}
$parameters['metrics'] = substr($metrics_string, 1);
}
else {
$parameters['metrics'] = 'ga:' . $metrics;
}
if ($sort_metric == null && isset($parameters['metrics'])) {
$parameters['sort'] = $parameters['metrics'];
}
elseif (is_array($sort_metric)) {
$sort_metric_string = '';
foreach ($sort_metric as $sort_metric_value) {
//Reverse sort - Thanks Nick Sullivan
if (substr($sort_metric_value, 0, 1) == "-") {
$sort_metric_string .= ',-ga:' . substr($sort_metric_value, 1);
// Descending
}
else {
$sort_metric_string .= ',ga:' . $sort_metric_value;
// Ascending
}
}
$parameters['sort'] = substr($sort_metric_string, 1);
}
else {
if (substr($sort_metric, 0, 1) == "-") {
$parameters['sort'] = '-ga:' . substr($sort_metric, 1);
}
else {
$parameters['sort'] = 'ga:' . $sort_metric;
}
}
if ($filter != null) {
$filter = $this
->processFilter($filter);
if ($filter !== false) {
$parameters['filters'] = $filter;
}
}
if ($start_date == null) {
$start_date = date('Y-m-d', strtotime('1 month ago'));
}
$parameters['start-date'] = $start_date;
if ($end_date == null) {
$end_date = date('Y-m-d');
}
$parameters['end-date'] = $end_date;
$parameters['start-index'] = $start_index;
$parameters['max-results'] = $max_results;
$parameters['prettyprint'] = gapi::dev_mode ? 'true' : 'false';
$response = $this
->httpRequest(gapi::report_data_url, $parameters, null, $this
->generateAuthHeader());
//HTTP 2xx
if (substr($response['code'], 0, 1) == '2') {
return $this
->reportObjectMapper($response['body']);
}
else {
throw new Exception('GAPI: Failed to request report data. Error: "' . strip_tags($response['body']) . '"');
}
}