I am using Google Analytics Api V3 and trying to show query results on the chart. I have this code at the moment but it's not showing on the chart. I think I am not passing the rows to chart correctly because I messed up foreach loop
google.load('visualization', '1.0', {'packages':['corechart']});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Topping');
data.addColumn('number', 'Slices');
data.addRows([
<?php
$first_date = '30daysAgo';
$last_date = 'yesterday';
$metrics = 'ga:sessions';
$optParams = array(
'dimensions' => 'ga:browser',
'sort' => '-ga:sessions',
'max-results' => '10'
);
try {
$results = $analytics->data_ga->get($analytics_id, $first_date, $last_date, $metrics, $optParams);
function printDataTable($results) {
if (count($results->getRows()) > 0) {
$value .= '';
foreach ($results->getRows() as $row) {
$value .= '';
foreach ($row as $cell) {
$value .= ''
. htmlspecialchars($cell, ENT_NOQUOTES)
. ' ';
}
}
} else {
$value .= 'No results found';
}
print $value;
}
print_r(printDataTable($results));
}
catch(Exception $e) {
printf('There was an Error in your query: %s', $e->getMessage());
}
?>
]);
var options = {'title':'Title',
'width':400,
'height':300};
var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
chart.draw(data, options);
}