Package Data | |
---|---|
Maintainer Username: | panakour |
Maintainer Contact: | panakourweb@gmail.com (Panagiots Koursaris) |
Package Create Date: | 2016-12-22 |
Package Last Update: | 2023-02-03 |
Home Page: | |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-22 03:09:55 |
Package Statistics | |
---|---|
Total Downloads: | 7,421 |
Monthly Downloads: | 7 |
Daily Downloads: | 3 |
Total Stars: | 26 |
Total Watchers: | 6 |
Total Forks: | 10 |
Total Open Issues: | 1 |
This package helps php developers to use Google Analytics API V4 with convenient way. The code is clean and was written with good OOP practices. Any help to improve this package would be appreciated.
The package is compatible with laravel framework.
Install the package via composer:
composer require panakour/analytics
To use it with laravel add the GoogleAnalyticsServiceProvider to the config/app.php
:
'providers' => [
...
Panakour\Analytics\GoogleAnalyticsServiceProvider::class
]
If you want to use the facade of the package add it to the config/app.php
:
'aliases' => [
...
'Analytics' => Panakour\Analytics\Facades\Analytics::class
]
Copy the analytics config file with the command:
php artisan vendor:publish --provider="Panakour\Analytics\GoogleAnalyticsServiceProvider"
For those who have a credential with google analytics api continue here otherwise look at how to create credential with google analytics api.
Be sure that you have service-account-credentials.json
file within storage\app\google-analytics\
.
Add the view id to .env
file:
GOOGLE_ANALYTICS_VIEW_ID=324235464
Panakour\Analytics\Facades\Analytics
Analytics::get();
Analytics::setDateRange('2016-10-01', '2016-11-25');
Analytics::get();
Analytics::setDateRange('2016-10-01', '2016-11-25');
Analytics::setMaxResults(20);
Analytics::setMetrics(['ga:entrances', 'ga:pageviews', 'ga:bounceRate']);
Analytics::setDimension(['ga:pagePath', 'ga:pageTitle']);
Analytics::setDimensionFilter('ga:pagePath', 'REGEXP', '/i-want-to-get-all-data-that-has-this-page-path');
Analytics::setOrder('ga:pageviews', 'VALUE', 'DESCENDING');
return Analytics::get();
setMetrics
and setDimension
methods accept an array containing the wanted metrics and dimension. Available metrics and dimensions
setDimensionFilter
accept 3 parameters. First parameter get the dimension name in which you want to filter analytics data. Second parameter get the operator (REGEXP, BEGINS_WITH, ENDS_WITH and more) you want. Third parameter get the expression. For example if you want to get all analytics data in which the page path include play
or simple
words you can use: Analytics::setDimensionFilter('ga:pagePath', 'REGEXP', '(\/play\/|\/simple\/)');
If you want to get analytics data for multiple pages in a single request by their exact paths, you can use the 'IN_LIST' operator and pass an array of paths as the third parameter. E.g.
Analytics::setDimensionFilter('ga:pagePath', 'IN_LIST', ['/i-want-data-for-this-path', '/and/this-path-too']);
setOrder
method accept 3 parameters. First parameter get the name in which you want to order the data. Second get OrderType usually VALUE
. Third get the SortOrder usually ASCENDING
or DESCENDING
.
Panakour\Analytics\Contracts\Analytics
Instead of facade you can get analytics data using the Analytics interface:
use Panakour\Analytics\Contracts\Analytics;
class GoogleAnalyticsController
{
//inject analytics interface
public function get(Analytics $analytics)
{
$analytics->setDateRange('2016-12-01', '2016-12-20');
$analytics->setMaxResults(11);
$analytics->setMetrics(['ga:pageviews', 'ga:uniquePageviews', 'ga:avgTimeOnPage', 'ga:entrances', 'ga:bounceRate']);
$analytics->setDimension(['ga:pagePath', 'ga:pageTitle']);
$analytics->setDimensionFilter('ga:pagePath', 'REGEXP', '(\/this-value-in-path\/|\/or-this-value-in-path\/)');
$analytics->setOrder('ga:pageviews', 'VALUE', 'DESCENDING');
return $analytics->get();
}
}
Guide from google analytics api v4: https://developers.google.com/analytics/devguides/reporting/core/v4/quickstart/service-php
Create service account
.User Management
and add the account you have copied with the permissions that you want.service-account-credentials.json
.view settings
from admin panel.