mrugeshtatvasoft / laravel-analytics
GA4 integration for laravel
v2.0
2024-06-27 05:41 UTC
Requires
- php: ^8.1
- google/analytics-data: ^0.8.6
- illuminate/contracts: >=9.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
README
This package offers integration to GA4 properties with some out of the box methods. Inspired by Spatie integration for GA3. Requires Laravel 9+.
Installation
You can install the package via composer:
composer require mrugeshtatvasoft/laravel-analytics
You can publish the config file with:
php artisan vendor:publish --tag="analytics-config"
This is the contents of the published config file:
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_'), ] ] ];
Usage
Inside Laravel:
use mrugeshtatvasoft\LaravelAnalytics\Period; use mrugeshtatvasoft\LaravelAnalytics\PrebuiltRunConfigurations; $client = App::make('analytics-v4'); $lastMonth = Period::months(1); $results = $client->runReport(PrebuiltRunConfigurations::getMostVisitedPages($lastMonth));
You may configure your own report configuration, or use a pre-built report:
// Use this on the laravel side to get it from the container $analytics = App::make('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);
Yay, results:
[
"dimensions" => [
"country" => "United States",
"landingPage" => "/",
"date" => "20220903",
],
"metrics" => [
"sessions" => "113",
],
],
[
"dimensions" => [
"country" => "United States",
"landingPage" => "/services/",
"date" => "20220902",
],
"metrics" => [
"sessions" => "110",
],
],
Or Using Prebuilt Report Configurations:
$lastMonth = Period::months(1); $analytics->runReport(PrebuiltRunConfigurations::getMostVisitedPages($lastMonth));
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
New Feature Includes
Set Not expression
// Prepare a filter $filter = new StringFilter(); $filter->setDimension('eventName')->exactlyMatches('Brand Viewer')->setNotExpression();
Run batch reports at a time
$array = [] // add all run report request $this->batchReport($array);
Please note: You can run Max 5 request at a time
License
The MIT License (MIT). Please see License File for more information.