PHP code example of zfhassaan / genlytics
1. Go to this page and download the library: Download zfhassaan/genlytics 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/ */
zfhassaan / genlytics example snippets
/*
* Package Service Providers...
*/
...
\zfhassaan\genlytics\provider\AnalyticsServiceProvider::class,
...
'aliases' => Facade::defaultAliases()->merge([
...
'Genlytics' => \zfhassaan\genlytics\facades\AnalyticsFacade::class,
...
])->toArray(),
$analytics = new Genlytics();
$period = ['start_date' => $request->period['start_date'],'end_date'=> $request->period['end_date']];
$active_users = $analytics->runReports($period,['name' => $state],['name' => 'activeUsers'] );
// For Single Dimension and Metric
$dimensions = ['name' => 'browser']; //1
$metrics = ['name' => 'activeUsers']; //1
$period = [['start_date' => '30daysAgo', 'end_date' => 'today']];
$result = $analytics->runReports($period, $dimensions, $metrics);
// For Multiple Dimensions and Metrics
try {
$analytics = new Genlytics();
$dimensions = [
['name' => 'browser'], //1
['name' => 'country'], //2
['name' => 'date'], //3
['name' => 'city'], //4
['name' => 'dateHour'], //5
['name' => 'firstUserSourceMedium'], //6
['name' => 'mobileDeviceMarketingName'], //7
['name' => 'operatingSystemWithVersion'], //8
['name' => 'dayOfWeek'], //9
['name' => 'defaultChannelGroup'], //10
['name' => 'language'], //11
['name' => 'dayOfWeekName'], //12
['name' => 'deviceCategory'], //13
['name' => 'contentGroup'], //14
['name' => 'fullPageUrl'], //15
];
$metrics = [
['name' => 'activeUsers'], //1
['name' => 'engagedSessions'], //2
];
// Period can be from any range to any range which can be checked from the GA Query Builder
$period = [['start_date' => '30daysAgo', 'end_date' => 'today']];
$results = array_map(function ($d) use ($analytics, $metrics, $period) {
return array_map(function ($m) use ($analytics, $d, $period) {
$result = $analytics->runReports($period, $d, $m);
return $result->content();
}, $metrics);
}, $dimensions);
return response()->json(['analytics' => $results]);
} catch (\Exception $e) {
return response()->json(['status' => false, 'error' => $e->getMessage()], 400);
}
$analytics->RunDimensionReport(['start_date' => '2022-01-01','end_date' => '2022-01-31'],'browser');
bash
php artisan vendor:publish