PHP code example of mrugeshtatvasoft / laravel-analytics
1. Go to this page and download the library: Download mrugeshtatvasoft/laravel-analytics library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
mrugeshtatvasoft / laravel-analytics example snippets
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_'),
]
]
];
use mrugeshtatvasoft\LaravelAnalytics\Period;
use mrugeshtatvasoft\LaravelAnalytics\PrebuiltRunConfigurations;
$client = App::make('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('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);