事情的起源来自,我想读到google adwords的每日花费,我很清楚使用adwords的接口,是最好不过的了,不过我没有超级权限,adwords只有账户创建者可以开通api,开通的人又在国外,交流起来不太方便,我发现google analytics打通了adwords以后用google anglytics就可以读取每日的adwords花费。
php 版本的application
https://developers.google.com/analytics/devguides/reporting/core/v4/quickstart/service-php
https://github.com/google/google-api-php-client
指标浏览器
https://ga-dev-tools.appspot.com/dimensions-metrics-explorer/
webserver
https://developers.google.com/analytics/devguides/reporting/core/v4/quickstart/web-php
composer 命令行
https://getcomposer.org/download/
php composer.phar require google/apiclient:^2.0
折腾完毕因为我在桌面路径完成的操作
php -S localhost:8080 -t /path/to/sample
php -S localhost:8080 -t /Users/ge/Desktop/Google
1020834977851-klmhco1lf94nibs4ffij9ilrtqoih3gg.apps.googleusercontent.com
SA100TPh8nHZuUDlRgHKBHCB
Fatal error: Uncaught exception 'GuzzleHttp\Exception\RequestException' with message 'cURL error 60: SSL certificate problem: unable to get local issuer certificate (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)' in /backup/www/leo2017/eye/g/vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php:186 Stack trace: #0 /backup/www/leo2017/eye/g/vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php(149): GuzzleHttp\Handler\CurlFactory::createRejection(Object(GuzzleHttp\Handler\EasyHandle), Array) #1 /backup/www/leo2017/eye/g/vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php(102): GuzzleHttp\Handler\CurlFactory::finishError(Object(GuzzleHttp\Handler\CurlHandler), Object(GuzzleHttp\Handler\EasyHandle), Object(GuzzleHttp\Handler\CurlFactory)) #2 /backup/www/leo2017/eye/g/vendor/guzzlehttp/guzzle/src/Handler/CurlHandler.php(43): GuzzleHttp\Handler\CurlFactory::finish(Object(GuzzleHttp\Handler\CurlHandler), Object(GuzzleHttp\Handler\EasyHandle), Object(GuzzleHttp\Handler\CurlFactory)) #3 /backup/www/leo2017/eye/g/vendor/gu in /backup/www/leo2017/eye/g/vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php on line 186
As detailed in many related questions here, I downloaded http://curl.haxx.se/ca/cacert.pem, moved it to my server's hard drive, and added the curl.cainfo setting to my php.ini:
1.php配置文件php.ini加入以下
curl.cainfo = "/backup/cacert.pem"
重启apache
2.链接的网络,必须能链接上google服务器,所以本地基本没戏
Fatal error: Uncaught exception 'Google_Service_Exception' with message '{ "error": { "code": 403, "message": "Analytics Reporting API has not been used in project 1020834977851 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/analyticsreporting.googleapis.com/overview?project=1020834977851 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.", "errors": [ { "message": "Analytics Reporting API has not been used in project 1020834977851 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/analyticsreporting.googleapis.com/overview?project=1020834977851 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.", "domain": "usageLimits", "reason": "accessNotConfigured", "extendedHelp": "https://console.developers.google.com" } ], "status": "PERMISSI in /backup/www/leo2017/eye/g/vendor/google/apiclient/src/Google/Http/REST.php on line 118
阅读说明去开启权限就打通了。
API
https://console.developers.google.com/apis/api/analyticsreporting.googleapis.com/overview?project=leo20180622&duration=PT1H
代码采用的
// Create the Metrics object.
$sessions = new Google_Service_AnalyticsReporting_Metric();
$sessions->setExpression("ga:sessions");
$sessions->setAlias("sessions");
DEMO
我想读取广告费
$sessions->setExpression("ga:adCost");
$sessions->setAlias("Fee");
https://developers.google.com/analytics/devguides/reporting/core/dimsmets#cats=session
对于我来说,如果每次需要授权来登陆,那么是无法满足自动化需求的。接下来,我会研究如何使用php service 的方式来读取数据