Tích hợp Google Analytics API vào ứng dụng Laravel 11 để thống kê truy cập website
1 : Cài đặt laravel với lệnh :
composer create-project laravel/laravel la11googleanalytic
Tiếp theo mình cài gói laravel analytic theo hướng dẫn sau https://github.com/akki-io/laravel-google-analytics/wiki/1.-Installing
composer require akki-io/laravel-google-analytics
Cài các bước tiếp theo như hướng dẫn ở gói đó
dưới đây là 1 số hàm hay dùng :
<?php
namespace App\Http\Controllers;
use AkkiIo\LaravelGoogleAnalytics\Facades\LaravelGoogleAnalytics;
use AkkiIo\LaravelGoogleAnalytics\LaravelGoogleAnalyticsResponse;
use AkkiIo\LaravelGoogleAnalytics\LaravelGoogleAnalyticsServiceProvider;
use AkkiIo\LaravelGoogleAnalytics\Period;
use Google\Analytics\Data\V1beta\RunRealtimeReportRequest;
use Google\Analytics\Data\V1beta\BetaAnalyticsDataClient;
use Google\Service\AnalyticsReporting;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Cache;
class GoogleController extends Controller
{
public function analytic(Request $request)
{
$data['total_user_1'] = LaravelGoogleAnalytics::getTotalUsers(Period::days(1));
$data['total_views_1'] = LaravelGoogleAnalytics::getTotalViews(Period::days(1));
$data['total_averagesession'] = LaravelGoogleAnalytics::getAverageSessionDuration(Period::days(1));
$data['total_views_2'] = LaravelGoogleAnalytics::getTotalUsersBySessionSource(Period::months(12));
$data['total_views_3'] = LaravelGoogleAnalytics::getMostUsersByCity(Period::days(100), 30);
$data['total_averagesession_date'] = LaravelGoogleAnalytics::getAverageSessionDurationByDate(Period::days(30));
$data['mostusersbyage'] = LaravelGoogleAnalytics::getMostUsersByAge(Period::days(1));
$data['mostviewsbypage'] = LaravelGoogleAnalytics::getMostViewsByPage(Period::days(10));
$data['mostuserbyscreen'] = LaravelGoogleAnalytics::getMostUsersByScreenResolution(Period::days(30));
$data['totalnew_return'] = LaravelGoogleAnalytics::getTotalNewAndReturningUsers(Period::days(30));
$data['totalviewbypage'] = LaravelGoogleAnalytics::getTotalViewsByPage(Period::days(1));
$data['getMostViewsByUser'] = LaravelGoogleAnalytics::getMostViewsByUser(Period::days(1));
$data = Cache::remember('data', Carbon::now()->addMinutes(3), function () {
$data['usecurrentonline'] = LaravelGoogleAnalytics::getCurrentOnlineUsers();
$data['total_user'] = LaravelGoogleAnalytics::getTotalUsers(Period::days(1));
$data['total_views'] = LaravelGoogleAnalytics::getTotalViews(Period::days(1));
// return $data;
// });
$data['usecurrentonline'] = Cache::remember('usecurrentonline', Carbon::now()->addMinutes(3), function () {
return LaravelGoogleAnalytics::getCurrentOnlineUsers();
});
$data['total_user'] = Cache::remember('total_user', Carbon::now()->addMinutes(720), function () {
return LaravelGoogleAnalytics::getTotalUsers(Period::days(1));
});
$data['total_views'] = Cache::remember('total_views', Carbon::now()->addMinutes(720), function () {
return LaravelGoogleAnalytics::getTotalViews(Period::days(1));
});
//dd($data);
return view('analytics.index', compact('data'));
}
public function customanalytics()
{
$analytics = new Analytics();
$realtimeReport = $analytics->realtime->getReport([
'dimensions' => [
'ga:pagePath',
],
'metrics' => [
'ga:pageviews',
'ga:uniquePageviews',
],
'filtersExpression' => 'ga:pagePath==/your/specific/page'
]);
dd($realtimeReport);
}
}