ersah / ga-bundle
Google Analytics API v4与Sonata Admin Bundle的集成
Requires
- php: >=5.5.9
- google/apiclient: ^2.0
This package is not auto-updated.
Last update: 2024-09-23 16:20:33 UTC
README
这是一个简单的包,使用Google Analytics Api v4为您提供所有通过维度和度量提供的信息。它具有默认的Sonata Admin Bundle 'list'功能。这意味着您可以排序、搜索、日期过滤Google Analytics报告。
要使用它,您需要在Google Console上设置一个Google Analytics项目,获取json密钥,然后通过设置其路径来配置此包。您需要将Google Console中定义的开发者电子邮件添加到GA视图以授权它,否则视图将无法通过API访问。
需求
SonataAdminBundle
https://sonata-project.org/bundles/admin/3-x/doc/getting_started/installation.html
将以下行添加到app/config/routing.yml
_sonata_admin:
resource: .
type: sonata_admin
prefix: /admin
安装
composer require ersah/ga-bundle=dev-master
添加到/app/AppKernel.php
$bundles = [
...
new Ersah\GABundle\ErsahGABundle(),
];
配置
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 :
您可以从这里找到可用的维度和度量:https://developers.google.com/analytics/devguides/reporting/core/dimsmets
选择您要使用的复选框项目,以查看维度和度量的可能组合。
Google Api Key
从https://console.developers.google.com/start/api?id=analyticsreporting.googleapis.com&credential=client_key通过创建项目生成json文件,检查文档:https://developers.google.com/analytics/devguides/reporting/core/v4/quickstart/service-php。
设置json密钥的相对路径(在您的服务器上设置,最好不要在您的存储库中设置)从执行路径,例如
/app/config/config.yml
ersah_ga:
google_analytics_json_key: "%kernel.root_dir%/../" #root of the project
您可以在这里看到重新发布:/admin/ga/list
Google Analytics API v4
示例:https://developers.google.com/analytics/devguides/reporting/core/v4/samples
错误
在此403错误情况下,请点击链接并授权API v4。
为相同的配置创建API
安装FosRestBundle
https://symfony.com.cn/doc/master/bundles/FOSRestBundle/1-setting_up_the_bundle.html
在/app/config/config.yml中启用Serializer
framework:
...
serializer: { enable_annotations: true }
和
fos_rest:
param_fetcher_listener: true
body_listener: true
format_listener: true
view:
view_response_listener: force
body_converter:
enabled: false
validate: true
添加到app/config/routing.yml
ersah_api_ga:
type: rest
prefix: /api/ga
resource: "@ErsahGABundle/Controller/ApiController.php"
安装JMSSerializerBundle
http://jmsyst.com/bundles/JMSSerializerBundle#installation
安装NelmioApiDocBundle
https://symfony.com.cn/doc/current/bundles/NelmioApiDocBundle/index.html
您可以在/api/doc中查看API端点和文档
附加功能:使用Sonata Block Bundle进行主要报告
在app/config/config.yml中
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' }
在app/config/config.yml中
sonata_admin:
dashboard:
blocks:
-
position: left
type: sonata.admin.block.admin_list
-
position: right
type: ersah.block.service.ga
您可以在/admin/dashboard上看到您的GA块