dev-master
9999999-devIntegration of Google Analytics API v4 to Sonata Admin Bundle
MIT
The Requires
- php >=5.5.9
- google/apiclient ^2.0
by Ersah ihlamur
googleanalytics
Integration of Google Analytics API v4 to Sonata Admin Bundle
It is a simple package that uses Google Analytics Api v4 to give you all the information available by Dimensions & Metrics. It has default Sonata Admin Bundle 'list' functionality. Meaning, you can sort, search, date filter your Google Analytics report., (*1)
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., (*2)
# Requirements, (*3)
SonataAdminBundle:, (*4)
https://sonata-project.org/bundles/admin/3-x/doc/getting_started/installation.html, (*5)
Add this lines to app/config/routing.yml, (*6)
_sonata_admin: resource: . type: sonata_admin prefix: /admin
# installation, (*7)
composer require ersah/ga-bundle=dev-master
add to /app/AppKernel.php :, (*8)
$bundles = [ ... new Ersah\GABundle\ErsahGABundle(), ];
# configuration, (*9)
ersah_ga: view_id: '' google_analytics_json_key: "%kernel.root_dir%/../" limit: 15 # results per page from: "30daysAgo" # also YYYY-MM-DD to: "today" # also YYYY-MM-DD sortBy: "ga:totalEvents" sorting: "DESCENDING" list: dimensions: - { value: 'ga:dimension1', label: 'Asset ID' } ... metrics: - { value: 'ga:totalEvents', label: 'Total Events' } ... #ga metrics and dimensions :
You can find available dimensions and metrics from here: https://developers.google.com/analytics/devguides/reporting/core/dimsmets, (*10)
Click check boxes items you want to use to see the possible combinations of dimensions and metrics., (*11)
# Google Api Key, (*12)
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)
Set relative path for your json key (set it on your server, better not into your repository) from execution path, ex:, (*14)
/app/config/config.yml, (*15)
ersah_ga: google_analytics_json_key: "%kernel.root_dir%/../" #root of the project
You can see repost here: /admin/ga/list, (*16)
# Google Analytics API v4, (*17)
Samples : https://developers.google.com/analytics/devguides/reporting/core/v4/samples, (*18)
# errors, (*19)
In that 403 error case, follow the link and authorize the API v4., (*20)
# Creating API for same configuration, (*21)
Install FosRestBundle:, (*22)
http://symfony.com/doc/master/bundles/FOSRestBundle/1-setting_up_the_bundle.html, (*23)
Enable Serializer in /app/config/config.yml:, (*24)
framework: ... serializer: { enable_annotations: true }
and:, (*25)
fos_rest: param_fetcher_listener: true body_listener: true format_listener: true view: view_response_listener: force body_converter: enabled: false validate: true
Add to app/config/routing.yml:, (*26)
ersah_api_ga: type: rest prefix: /api/ga resource: "@ErsahGABundle/Controller/ApiController.php"
Install JMSSerializerBundle:, (*27)
http://jmsyst.com/bundles/JMSSerializerBundle#installation, (*28)
Install NelmioApiDocBundle:, (*29)
http://symfony.com/doc/current/bundles/NelmioApiDocBundle/index.html, (*30)
You can see API endpoints and documentations in /api/doc, (*31)
# Bonus: Use Sonata Block Bundle for Main report, (*32)
In app/config/config.yml, (*33)
ersah_ga: ... main: metrics: - { value: 'ga:sessions', label: 'Sessions' } - { value: 'ga:bounceRate', label: 'Bounce Rate' } - { value: 'ga:avgTimeOnPage', label: 'Average Time On Page' } - { value: 'ga:pageviewsPerSession', label: 'Page Views Per Session' } - { value: 'ga:percentNewVisits', label: 'Percent New Visits' } - { value: 'ga:pageviews', label: 'Page Views' } - { value: 'ga:avgPageLoadTime', label: 'Average Page Load Time' }
In app/config/config.yml, (*34)
sonata_admin: dashboard: blocks: - position: left type: sonata.admin.block.admin_list - position: right type: ersah.block.service.ga
You can so your GA block on your /admin/dashboard, (*35)
Integration of Google Analytics API v4 to Sonata Admin Bundle
MIT
googleanalytics