我已经使用Spatie软件包从我的Google Analytics(分析)帐户中提取了Google Analytics(分析)数据。我需要使用chart.js或任何其他方式来可视化这些数据,并在表格内显示这些数据。我不确定如何做,因为我是laravel和Google Analytics(分析)的新手。
public function fetchUserTypes(Period $period): Collection
{
$response = $this->performQuery(
$period,
'ga:sessions',
[
'dimensions' => 'ga:userType',
]
);
return collect($response->rows ?? [])->map(function (array $userRow) {
return [
'type' => $userRow[0],
'sessions' => (int) $userRow[1],
];
});
}
public function fetchTopBrowsers(Period $period, int $maxResults = 7): Collection
{
$response = $this->performQuery(
$period,
'ga:sessions',
[
'dimensions' => 'ga:browser',
'sort' => '-ga:sessions',
]
);
$topBrowsers = collect($response['rows'] ?? [])->map(function (array $browserRow) {
return [
'browser' => $browserRow[0],
'sessions' => (int) $browserRow[1],
];
});
if ($topBrowsers->count() <= $maxResults) {
return $topBrowsers;
}
return $this->summarizeTopBrowsers($topBrowsers, $maxResults);
}
protected function summarizeTopBrowsers(Collection $topBrowsers, int $maxResults): Collection
{
return $topBrowsers
->take($maxResults - 1)
->push([
'browser' => 'Others',
'sessions' => $topBrowsers->splice($maxResults - 1)->sum('sessions'),
]);
}
这是输出的样子
用户类型 [{“ type”:“新访客”,“会话”:54},{“ type”:“回访者”,“会话”:11}]
顶级浏览器 [{{“浏览器”:“ Chrome”,“会话”:31},{“浏览器”:“ Safari”,“会话”:19},{“浏览器”:“ Safari(应用内)”,“会话” :6},{“浏览器”:“ Samsung Internet”,“会话”:5},{“浏览器”:“ Android Webview”,“会话”:1},{“浏览器”:“ Edge”,“会话” :1},{“浏览器”:“其他”,“会话”:2}]