Reputation: 97
This is my first attempt to work with the Core Reporting API. I have successfully made it through the Hello Analytics tutorial and making API requests with no issue. My problem lies with querying the API for using Dimensions, Metrics, and Filters. Below is the code I am working with.. I am able to display how many visitors I have between the first of the month and the current day. Then it displays how many of these came from organic search. I am hoping someone can give me an example on querying the API with a more complex request.. perhaps including Dimensions, Metrics, Filters.. and then displaying then in rows. Any help is much appreciated. Below is my code so far...
//QUERY THE CORE REPORTING API
function getResults($analytics, $profileId, $first_day, $today) {
return $analytics->data_ga->get(
'ga:' . $profileId,
$first_day,
$today,
'ga:visits, ga:organicSearches');
}
//OUTPUT THE RESULTS
function printResults(&$results) {
if (count($results->getRows()) > 0) {
$profileName = $results->getProfileInfo()->getProfileName();
$rows = $results->getRows();
$visits = $rows[0][0];
$organic = $rows[0][1];
print "<h1>$profileName</h1>";
echo '<table border="1" cellpadding="5">';
echo '<tr>';
echo '<td>Visits</td>';
echo '<td>Organic</td>';
echo '</tr>';
echo '<tr>';
echo '<td>'. $visits . '</td>';
echo '<td>'. $organic . '</td>';
echo '</td>';
echo '</table>';
} else {
print '<p>No results found.</p>';
}
}
Upvotes: 3
Views: 4959
Reputation: 3690
This is the definition of data_ga->get
function in the api source.
public function get($ids, $startDate, $endDate, $metrics, $optParams = array())
{
$params = array('ids' => $ids, 'start-date' => $startDate, 'end-date' => $endDate, 'metrics' => $metrics);
$params = array_merge($params, $optParams);
return $this->call('get', array($params), "Google_Service_Analytics_GaData");
}
complete parameter list is here
all parameters except ids,startdate, enddate and metrics are optional and needs to be sent as associative array as 5th argumant of get function.
Upvotes: 2
Reputation: 31
Here is the code:
$optParams = array(
'dimensions' => 'ga:date,ga:customVarValue1,ga:visitorType,ga:pagePath',
'sort' => '-ga:visits,ga:date',
'filters' => 'ga:visitorType==New',
'max-results' => '100');
$metrics = "ga:visits";
$results = $analytics->data_ga->get(
'ga:' . $profileId,
'2013-03-01',
'2013-03-10',
$metrics,
$optParams);
For displaying the result:
function getRows($results) {
$table = '<h3>Rows Of Data</h3>';
if (count($results->getRows()) > 0) {
$table .= '<table>';
// Print headers.
$table .= '<tr>';
foreach ($results->getColumnHeaders() as $header) {
$table .= '<th>' . $header->name . '</th>';
}
$table .= '</tr>';
// Print table rows.
foreach ($results->getRows() as $row) {
$table .= '<tr>';
foreach ($row as $cell) {
$table .= '<td>'
. htmlspecialchars($cell, ENT_NOQUOTES)
. '</td>';
}
$table .= '</tr>';
}
$table .= '</table>';
} else {
$table .= '<p>No results found.</p>';
}
return $table;
}
You can have better understanding if you try to get this demo working.
Also refer to this code
Upvotes: 3