Skip to content

Commit

Permalink
Preserve compatibility with Laravel 9
Browse files Browse the repository at this point in the history
Twill can't do anything about the fact that spatie/laravel-analytics doesn't support GA4 with PHP 8.0 and Laravel 9, but we still need to keep v4 of laravel-analytics around, to avoid composer incompatibility issues when installing Twill on Laravel 9. We will drop support for Laravel 9 in Twill 4.
  • Loading branch information
ifox committed Jan 26, 2024
1 parent 6309324 commit 6102a7a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 19 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"myclabs/php-enum": "^1.5",
"pragmarx/google2fa-qrcode": "^2.0",
"spatie/laravel-activitylog": "^4.0",
"spatie/laravel-analytics": "^5.0",
"spatie/laravel-analytics": "^4.0|^5.0",
"spatie/once": "^3.0"
},
"require-dev": {
Expand Down
2 changes: 1 addition & 1 deletion docs/content/1_docs/11_dashboard/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ return [

It is using Spatie's [Laravel Analytics](https://github.com/spatie/laravel-analytics) package.

Follow [Spatie's documentation](https://github.com/spatie/laravel-analytics#how-to-obtain-the-credentials-to-communicate-with-google-analytics) to set up a Google service account and download a json file containing your credentials, and provide your Analytics view ID using the `ANALYTICS_VIEW_ID` environment variable.
Follow [Spatie's documentation](https://github.com/spatie/laravel-analytics#how-to-obtain-the-credentials-to-communicate-with-google-analytics) to set up a Google service account and download a json file containing your credentials, and provide your Analytics view ID using the `ANALYTICS_PROPERTY_ID` environment variable.

## User activity

Expand Down
61 changes: 44 additions & 17 deletions src/Http/Controllers/Admin/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,30 +287,57 @@ private function formatAuthActivity(Activity $activity): array
*/
private function getFacts()
{
/** @var Analytics $analytics */
$analytics = app()->makeWith(Analytics::class, ['propertyId' => config('analytics.property_id')]);
// TODO: cleanup when dropping support for Laravel 9
$useV5API = true;
if (class_exists('Spatie\Analytics\Facades\Analytics')) {
/** @var Analytics $analytics */
$analytics = app()->makeWith(Analytics::class, ['propertyId' => config('analytics.property_id')]);
} else {
/** @var Analytics $analytics */
$analytics = app(Analytics::class);
$useV5API = false;
}

try {
$response = $analytics->get(
Period::days(60),
['totalUsers', 'screenPageViews', 'bounceRate', 'screenPageViewsPerSession'],
['date']
);
if ($useV5API) {
$response = $analytics->get(
Period::days(60),
['totalUsers', 'screenPageViews', 'bounceRate', 'screenPageViewsPerSession'],
['date']
);

$statsByDate = $response->map(function (array $item) {
return [
'date' => $item['date'],
'users' => (int) $item['totalUsers'],
'pageViews' => (int) $item['screenPageViews'],
'bounceRate' => $item['bounceRate'],
'pageviewsPerSession' => $item['screenPageViewsPerSession'],
];
})->reverse()->values();
} else {
$response = $analytics->performQuery(
Period::days(60),
'ga:users,ga:pageviews,ga:bouncerate,ga:pageviewsPerSession',
['dimensions' => 'ga:date']
);

$statsByDate = Collection::make($response['rows'] ?? [])->map(function (array $dateRow) {
return [
'date' => $dateRow[0],
'users' => (int)$dateRow[1],
'pageViews' => (int)$dateRow[2],
'bounceRate' => $dateRow[3],
'pageviewsPerSession' => $dateRow[4],
];
});
}
} catch (InvalidConfiguration $exception) {
$this->logger->error($exception);

return [];
}

$statsByDate = $response->map(function (array $item) {
return [
'date' => $item['date'],
'users' => (int) $item['totalUsers'],
'pageViews' => (int) $item['screenPageViews'],
'bounceRate' => $item['bounceRate'],
'pageviewsPerSession' => $item['screenPageViewsPerSession'],
];
})->reverse()->values();

$dummyData = null;
if ($statsByDate->isEmpty()) {
$dummyData = [
Expand Down

0 comments on commit 6102a7a

Please sign in to comment.