php调用google analytics API读取数据


事情的起源来自,我想读到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

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

折腾完毕因为我在桌面路径完成的操作

桌面多了三个文件

我把文件都放在Google 这个文件夹里

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 的方式来读取数据

如果你是一名技术人员可加我QQ 2651-0442-02,如果你是java技术人还可以加入QQ群 1784-9136-0
你将得到的不仅仅是技术的交流,还有职业机会,人生解惑.
首发地址:月小升博客https://java-er.com/blog/php-connect-google-analytics-api/
无特殊说明,文章均为月小升原创,欢迎转载,转载请注明本文地址,谢谢
此条目发表在 PHP 分类目录。将固定链接加入收藏夹。
既然来了,就评论一下,不会怀孕的

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">