Google Analytics API v4 Symfony bundle
, (*1)
, (*2)
use
At MEDIA.figaro http://media.figaro.fr, the advertising department of the french newspaper Le Figaro and part of the Figaro Group (CCM Benchmark), we use this bundle to monitor our digital platforms with Google Analytics., (*3)
It's a simple package that wraps the Google Analytics API version 4, and that gives you all the information to go straight to the point of getting some main metrics from GA., (*4)
To be able to use it, you have to setup a project on Google Console for Google Analytics, get the json key, then configure this package by setting the path for it. You'll have to add the developer email defined into the Google Console to the GA views to authorize it, otherwise the view won't be accessible through the API., (*5)
You can use the debug routes to go live and test a profile (ex id : 111111111, here with Docker) :, (*6)
http://symfony.dev/app_dev.php/analytics-api/111111111, (*7)
, (*8)
installation
composer require mediafigaro/google-analytics-api-symfony
add to /app/AppKernel.php :, (*9)
$bundles = [
...
new MediaFigaro\GoogleAnalyticsApi\GoogleAnalyticsApi(),
];
configuration
google_analytics_api.google_analytics_json_key
Set the relative path for your json key (set it on your server, better not into your repository) from execution path, ex: /data/analytics/analytics-27cef1a4c0fd.json., (*10)
/app/config/parameters.yml, (*11)
google_analytics_json_key: "../data/analytics/analytics-27cef1a4c0fd.json"
/app/config/config.yml, (*12)
google_analytics_api:
google_analytics_json_key: "%google_analytics_json_key%"
Google API key
Generate the json file from https://console.developers.google.com/start/api?id=analyticsreporting.googleapis.com&credential=client_key by creating a project, check the documentation : https://developers.google.com/analytics/devguides/reporting/core/v4/quickstart/service-php., (*13)
Google Analytics API v4
List of metrics for report building with search engine : https://developers.google.com/analytics/devguides/reporting/core/dimsmets eg. ga:sessions, ga:visits, ga:bounceRate ..., (*14)
Objects : https://github.com/google/google-api-php-client-services/tree/master/AnalyticsReporting, (*15)
(example : ReportData object : https://github.com/google/google-api-php-client-services/blob/master/AnalyticsReporting/ReportData.php), (*16)
Samples : https://developers.google.com/analytics/devguides/reporting/core/v4/samples, (*17)
debug
Add the debug routes for development purposes :, (*18)
/app/config/routing_dev.yml, (*19)
_google_analytics_api:
resource: "@GoogleAnalyticsApi/Resources/config/routing_dev.yml"
http://symfony.dev/app_dev.php/analytics-api/000000000, (*20)
000000000 = profile id that you can find in the analytics URL, p000000000 :, (*21)
https://analytics.google.com/analytics/web/?hl=en&pli=1#management/Settings/a222222222w1111111111p000000000/%3Fm.page%3DPropertySettings/, (*22)
Result of this debug page :, (*23)
, (*24)
errors
In that 403 error case, follow the link and authorize the API v4., (*25)
...
"message": "Google Analytics Reporting API has not been used in project xxxxxx-xxxxxx-000000
before or it is disabled. Enable it by visiting
https://console.developers.google.com/apis/api/analyticsreporting.googleapis.com/overview?project=xxxxxx-xxxxxx-000000
then retry. If you enabled this API recently, wait a few minutes for the action to propagate
to our systems and retry.",
"domain": "global",
"reason": "forbidden"
}
],
"status": "PERMISSION_DENIED"
example
Call the service :, (*26)
$analyticsService = $this->get('google_analytics_api.api');
$analytics = $analyticsService->getAnalytics();
Use the method helpers to get the main metrics within a date range :, (*27)
$viewId = '000000000'; // set your view id
// get some metrics (last 30 days, date format is yyyy-mm-dd)
$sessions = $analyticsService->getSessionsDateRange($viewId,'30daysAgo','today');
$bounceRate = $analyticsService->getBounceRateDateRange($viewId,'30daysAgo','today');
$avgTimeOnPage = $analyticsService->getAvgTimeOnPageDateRange($viewId,'30daysAgo','today');
$pageViewsPerSession = $analyticsService->getPageviewsPerSessionDateRange($viewId,'30daysAgo','today');
$percentNewVisits = $analyticsService->getPercentNewVisitsDateRange($viewId,'30daysAgo','today');
$pageViews = $analyticsService->getPageViewsDateRange($viewId,'30daysAgo','today');
$avgPageLoadTime = $analyticsService->getAvgPageLoadTimeDateRange($viewId,'30daysAgo','today');
contribution
You are welcome to contribute to this small Google Analytics v4 wrapper, to create more helpers or more., (*28)
Try the Symfony Debug Toolbar Git : https://github.com/kendrick-k/symfony-debug-toolbar-git and the docker Service Oriented Architecture for Symfony : https://github.com/mediafigaro/docker-symfony., (*29)
tutorial
French tutorial by Jérémy PERCHE, SUPINFO student., (*30)