tda / laravel-analytics-v4
laravel 8的GA4集成
v1.0.0
2023-03-07 09:44 UTC
Requires
- php: ^8.1
- google/analytics-data: ^0.8.6|^0.9
- illuminate/contracts: ^8.0
- spatie/laravel-package-tools: ^1.9.2
Requires (Dev)
- laravel/pint: ^1.0
- nunomaduro/collision: ^6.0
- nunomaduro/larastan: ^2.0.1
- orchestra/testbench: ^7.0|^8.0
- pestphp/pest: ^1.22.2|^1.22
- pestphp/pest-plugin-laravel: ^1.4
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: ^9.5
- spatie/laravel-ray: ^1.26
This package is not auto-updated.
Last update: 2024-09-17 23:17:28 UTC
README
此包提供了与GA4属性集成的功能,并附带一些开箱即用的方法。灵感来源于Spatie集成 GA3,并最初从MyOutDeskLLC分叉。需要Laravel 8及以上。
安装
您可以通过Composer安装此包
composer require tda/laravel-analytics-v4
您可以使用以下命令发布配置文件
php artisan vendor:publish --tag="analytics-v4-config"
这是已发布配置文件的内容
return [ 'property_id' => env('ANALYTICS_PROPERTY_ID', 'XXXXXXXXX'), 'service_account_credentials_json' => storage_path('app/analytics/service-account-credentials.json'), // This data is passed into the built-in cache mechanism for google's CredentialWrapper 'cache' => [ 'enableCaching' => env('ANALYTICS_CACHE',false), 'authCache' => null, 'authCacheOptions' => [ 'lifetime' => env('ANALYTICS_CACHE_LIFETIME', 60), // you may want to set this higher 'prefix' => env('ANALYTICS_CACHE_PREFIX', 'analytics_'), ] ] ];
使用方法
Laravel内部
use Tda\LaravelAnalyticsV4\Period; use Tda\LaravelAnalyticsV4\PrebuiltRunConfigurations; $client = App::make('laravel-analytics-v4'); $lastMonth = Period::months(1); $results = $client->runReport(PrebuiltRunConfigurations::getMostVisitedPages($lastMonth));
您可以选择配置自己的报告配置,或使用预构建的报告
// Use this on the laravel side to get it from the container $analytics = App::make('laravel-analytics-v4'); // Prepare a filter $filter = new StringFilter(); $filter->setDimension('country')->exactlyMatches('United States'); // Prepare a report $reportConfig = (new RunReportConfiguration()) ->setStartDate('2022-09-01') ->setEndDate('2022-09-30') ->addDimensions(['country', 'landingPage', 'date']) ->addMetric('sessions') ->addFilter($filter); $analytics->convertResponseToArray()->runReport($reportConfig);
是的,结果是
[
"dimensions" => [
"country" => "United States",
"landingPage" => "/",
"date" => "20220903",
],
"metrics" => [
"sessions" => "113",
],
],
[
"dimensions" => [
"country" => "United States",
"landingPage" => "/services/",
"date" => "20220902",
],
"metrics" => [
"sessions" => "110",
],
],
或者使用预构建的报告配置
$lastMonth = Period::months(1); $analytics->runReport(PrebuiltRunConfigurations::getMostVisitedPages($lastMonth));
测试
composer test
更新日志
请参阅更新日志了解最近更改的详细信息。
贡献
提交一个通过测试的PR。
鸣谢
许可
MIT许可(MIT)。请参阅许可文件以获取更多信息。