我试图从分析API中获得一些数据,然后使用谷歌图表API显示它的面积图表。
以下数组包含来自Analytics API v4的两天数据。此屏幕截图显示了如何在Query Explorer中的表中返回/呈现数据。
<?php
$results = [
0 =>
[
'dimensions' =>
[
0 => 'Organic Search',
1 => '20160831',
],
'metrics' =>
[
0 =>
[
'values' =>
[
0 => '13',
1 => '0',
2 => '0.0',
],
],
],
],
1 =>
[
'dimensions' =>
[
0 => 'Social',
1 => '20160830',
],
'metrics' =>
[
0 =>
[
'values' =>
[
0 => '7',
1 => '0',
2 => '0.0',
],
],
],
],
2 =>
[
'dimensions' =>
[
0 => 'Organic Search',
1 => '20160830',
],
'metrics' =>
[
0 =>
[
'values' =>
[
0 => '6',
1 => '0',
2 => '0.0',
],
],
],
],
3 =>
[
'dimensions' =>
[
0 => 'Social',
1 => '20160831',
],
'metrics' =>
[
0 =>
[
'values' =>
[
0 => '6',
1 => '0',
2 => '0.0',
],
],
],
],
4 =>
[
'dimensions' =>
[
0 => 'Referral',
1 => '20160831',
],
'metrics' =>
[
0 =>
[
'values' =>
[
0 => '3',
1 => '0',
2 => '0.0',
],
],
],
],
5 =>
[
'dimensions' =>
[
0 => 'Referral',
1 => '20160830',
],
'metrics' =>
[
0 =>
[
'values' =>
[
0 => '2',
1 => '0',
2 => '0.0',
],
],
],
],
6 =>
[
'dimensions' =>
[
0 => 'Direct',
1 => '20160830',
],
'metrics' =>
[
0 =>
[
'values' =>
[
0 => '1',
1 => '0',
2 => '0.0',
],
],
],
],
];
?>
我正在尝试将30天的Default Channel Grouping and Sessions数据绘制到Area Chart中。图表接受的数据格式如下:
[
[
{
"label": "Day",
"id": "day"
},
{
"label": "Organic Search",
"id": "organic_search",
"type": "number"
},
{
"label": "Referral",
"id": "referral",
"type": "number"
},
{
"label": "Social",
"id": "social",
"type": "number"
},
{
"label": "Direct",
"id": "direct",
"type": "number"
}
],
[
"20160831",
"13",
"3",
"0",
"6"
],
[
"20160830",
"1",
"6",
"2",
"7"
]
]
下面是我的代码,尝试将API数据放入图表的Array中。
<?php
function getChartData($rows)
{
// Get the unique dates
$dates = [];
foreach ($rows as $row) {
$dates[] = $row['dimensions'][1];
}
$dates = array_unique($dates);
// get the uniqe dimensions
$dimensions = [];
foreach ($rows as $row) {
$dimensions[] = $row['dimensions'][0];
}
$dimensions = array_unique($dimensions);
// Put all the values and the dimension filtered by date
$values = [];
foreach ($dates as $date) {
foreach ($rows as $row) {
if ($date == $row['dimensions'][1]) {
$val = $row['metrics'][0]['values'];
array_push($val, $row['dimensions'][0]);
array_unshift($val, $date);
$values[] = $val;
}
}
}
// Group the sessions metric value by date
$chartValues = [];
foreach ($dates as $date) {
$val = [];
foreach ($values as $value) {
if ($date == $value[0]) {
$val[] = $value[1];
}
}
array_unshift($val, $date);
$chartValues[] = $val;
}
return $chartValues;
}
我遇到的问题是,API并不总是每天返回完全相同数量的频道分组。
例如,对于8月31日,没有直接的业务会话数据。当它按原样传递给图表时,它会失败,因为它总是希望数组中的值始终具有相同的项数。
当任何Channel Grouping Session数据丢失时,我如何添加空值,这样Area Charts阵列每天都会收到相同数量的值?
发布于 2016-09-12 04:32:23
如果有人遇到同样的问题,我做了以下工作来让它正常工作:
<?php
function getAreaChartValues($response)
{
// Get the rows of the response
$rows = $response->toSimpleObject()->reports[0]['data']['rows'];
$dimensions = [];
$results = [];
foreach ($rows as $row) {
// Add all the dimensions to the dimensions array
$dimensions[] = $row['dimensions'][0];
$date = isset($row['dimensions'][1]) ? $row['dimensions'][1] : '';
$ValueType = $row['dimensions'][0];
$value = $row['metrics'][0]['values'][0];
if (!isset($results[$date])) {
$results[$date] = [];
}
$results[$date][$ValueType] = $value;
}
ksort($results);
$dimensions = array_unique($dimensions);
// Check for any non existant value for a dimension
// And add empty values
foreach ($dimensions as $dimension) {
foreach ($results as &$result) {
if (!array_key_exists($dimension, $result)) {
$result[$dimension] = 0;
}
ksort($result);
}
}
return $results;
}
发布于 2016-09-02 05:35:12
尝试使用null
值
if ($date == $row['dimensions'][1]) {
$val = $row['metrics'][0]['values'];
array_push($val, $row['dimensions'][0]);
array_unshift($val, $date);
$values[] = $val;
} else {
$val = null;
array_push($val, $row['dimensions'][0]);
array_unshift($val, $date);
$values[] = $val;
}
https://stackoverflow.com/questions/39280213
复制相似问题