mediafigaro / google-analytics-api-symfony
Google Analytics API v4 Symfony
Requires
- google/apiclient: ^2.0
- symfony/framework-bundle: >=2.3
- symfony/twig-bundle: >=2.0
- twig/twig: >=1.6
Requires (Dev)
- phpunit/phpunit: 6.5.*
This package is not auto-updated.
Last update: 2024-09-19 10:31:37 UTC
README
使用
在MEDIA.figaro http://media.figaro.fr,法国报纸《费加罗》的广告部门和费加罗集团(CCM Benchmark)的一部分,我们使用此包来监控我们的数字平台,使用Google Analytics。
这是一个简单的包,它封装了Google Analytics API版本4,并提供获取GA主要指标所需的所有信息。
由Kendrick开发 https://github.com/kendrick-k.
要使用它,您需要在Google控制台中设置一个Google Analytics项目,获取json密钥,然后通过设置路径来配置此包。您需要将Google控制台中的开发者电子邮件添加到GA视图以授权它,否则视图无法通过API访问。
您可以使用调试路由上线并测试一个配置文件(例如id:111111111,这里使用 Docker)
http://symfony.dev/app_dev.php/analytics-api/111111111
安装
composer require mediafigaro/google-analytics-api-symfony
没有Flex,添加到 /app/AppKernel.php
$bundles = [
...
new MediaFigaro\GoogleAnalyticsApi\GoogleAnalyticsApi(),
];
版本
1.2
向getDataDateRangeMetricsDimensions方法添加filterMetric和filterDimension,该方法是一个简单的Google Api Client封装
$analyticsService = $this->get('google_analytics_api.api');
$data = $analyticsService->getDataDateRangeMetricsDimensions(
'myanalyticsviewid', // viewid
'2018-01-01', // date start
'today', // date end
['sessions','users','percentNewSessions','bounceRate'], // metric
['source','campaign','fullReferrer','sourceMedium','pagePath'], // dimension
[ // order metric and/or dimension
'fields' => ['sessions'],
'order' => 'descending'
],
[ // metric
'metric_name' => 'sessions',
'operator' => 'LESS_THAN',
'comparison_value' => '100'
],
[ // dimension
'dimension_name' => 'sourceMedium',
'operator' => 'EXACT',
'expressions' => [
'trading / native'
]
]
);
1.1
Symfony 4简单适配,使用公共服务和新的公共方法来处理指标和维度以及排序选项
getDataDateRangeMetricsDimensions($viewId,$dateStart,$dateEnd,$metrics='sessions',$dimensions=null,$sorting=null)
查询探索器 https://ga-dev-tools.appspot.com/query-explorer/ 来构建您的查询。
$viewId : https://developers.google.com/analytics/devguides/reporting/core/v3/reference#ids
$dateStart : https://developers.google.com/analytics/devguides/reporting/core/v3/reference#startDate
$dateEnd: https://developers.google.com/analytics/devguides/reporting/core/v3/reference#endDate
$metrics: https://developers.google.com/analytics/devguides/reporting/core/v3/reference#metrics (没有'ga:',数组或字符串)
$dimensions: https://developers.google.com/analytics/devguides/reporting/core/v3/reference#dimensions (没有'ga:',数组或字符串)
$sorting: https://developers.google.com/analytics/devguides/reporting/core/v3/reference#sort
没有'ga:',数组或字符串,例如
[
'fields' => ['pagePath','sessions'], // or : 'sessions'
'order' => 'descending'
]
示例
$analyticsService = $this->get('google_analytics_api.api');
$data = $analyticsService->getDataDateRangeMetricsDimensions(
'myanalyticsviewid',
'30daysAgo',
'today',
['sessions','users','percentNewSessions','bounceRate'],
['source','campaign','fullReferrer','sourceMedium','pagePath'],
[
'fields' => ['pagePath','sessions'],
'order' => 'descending'
]
)
$data
array(329) {
[0]=>
array(2) {
["metrics"]=>
array(4) {
["sessions"]=>
string(5) "16738"
["users"]=>
string(5) "15602"
["percentNewSessions"]=>
string(17) "88.39168359421676"
["bounceRate"]=>
string(17) "83.95268251881946"
}
["dimensions"]=>
array(5) {
["source"]=>
string(7) "trading"
["campaign"]=>
string(7) "my-campaign"
["fullReferrer"]=>
string(7) "trading"
["sourceMedium"]=>
string(16) "trading / native"
["pagePath"]=>
string(50) "/my-url"
}
}
[1]=>
array(2) {
["metrics"]=>
array(4) {
["sessions"]=>
string(4) "6506"
["users"]=>
string(4) "6200"
["percentNewSessions"]=>
string(17) "87.05810021518599"
["bounceRate"]=>
string(17) "87.74976944359054"
}
["dimensions"]=>
array(5) {
["source"]=>
string(7) "trading"
["campaign"]=>
string(7) "my-campaign-2"
["fullReferrer"]=>
string(7) "trading"
["sourceMedium"]=>
string(19) "trading / 320x480-1"
["pagePath"]=>
string(50) "/my-url-2"
}
}
[2]=>
...
会话 - 维度与指标探索器 : https://developers.google.com/analytics/devguides/reporting/core/dimsmets
1.0
第一个版本,使用getDataDateRange私有方法(仅处理指标)与Google API客户端快速连接以消费Google Analytics v4,并用一些公共方法快速访问主要指标,如会话(例如,getBounceRateDateRange($viewId,$dateStart,$dateEnd)))、跳出率、平均页面停留时间、每会话页面浏览量、新访问者、页面浏览量和平均页面加载时间。包括调试路由以进行简单设置和测试。
配置
google_analytics_api.google_analytics_json_key
设置您的json键的相对路径(在服务器上设置,最好不要放入您的仓库),例如:/data/analytics/analytics-27cef1a4c0fd.json。
/app/config/parameters.yml
google_analytics_json_key: "../data/analytics/analytics-27cef1a4c0fd.json"
/app/config/config.yml
google_analytics_api:
google_analytics_json_key: "%google_analytics_json_key%"
Google API密钥
从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。
Google Analytics API v4
使用搜索引擎构建报告的指标列表: https://developers.google.com/analytics/devguides/reporting/core/dimsmets 例如。ga:sessions, ga:visits, ga:bounceRate ...
对象: https://github.com/google/google-api-php-client-services/tree/master/AnalyticsReporting
(示例:ReportData对象: https://github.com/google/google-api-php-client-services/blob/master/AnalyticsReporting/ReportData.php)
示例: https://developers.google.com/analytics/devguides/reporting/core/v4/samples
调试
添加调试路由以用于开发目的
/app/config/routing_dev.yml
_google_analytics_api:
resource: "@GoogleAnalyticsApi/Resources/config/routing_dev.yml"
http://symfony.dev/app_dev.php/analytics-api/000000000
000000000 = 分析URL中可以找到的配置文件ID,p000000000
此调试页面的结果
错误
在此403错误情况下,点击链接并授权API v4。
...
"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"
示例
调用服务
$analyticsService = $this->get('google_analytics_api.api');
$analytics = $analyticsService->getAnalytics();
使用方法辅助工具在日期范围内获取主要指标
$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');
贡献
欢迎您为这个小巧的Google Analytics v4包装器做出贡献,创建更多辅助工具或更多。
更多工具
尝试使用Git的Symfony调试工具栏: https://github.com/kendrick-k/symfony-debug-toolbar-git 以及针对Symfony的面向服务的架构的Docker: https://github.com/mediafigaro/docker-symfony。
教程
Jérémy PERCHE,SUPINFO学生,提供的法语教程。