编程语言
首页 > 编程语言> > Google Analytics V3 API PHP

Google Analytics V3 API PHP

作者:互联网

我正在尝试从我的Google分析帐户中获取数据.我已经完成了以下步骤:

>开启Analytics的API访问权限
>创建一个OAuth2和服务器帐户来访问数据
>添加了电子邮件地址(在代码中指定)以访问我的分析.

我正在从这里使用PHP客户端:https://github.com/google/google-api-php-client(Alpha)

我收到的错误是:
发生错误:-(获取)未知参数:“开始日期”

将我的头撞在这里的墙上,任何帮助将不胜感激.

我在下面包含了我的代码.

`

set_include_path("../src/" . PATH_SEPARATOR . get_include_path());
require_once 'Google/Client.php';
require_once 'Google/Service/Analytics.php';

$client_id = 'xxx.apps.googleusercontent.com';
$service_account_name = 'xxx@developer.gserviceaccount.com';
$keyfile = 'xxx-privatekey.p12';
$redirect_url = 'http://xxx/tags/v1.0.0-alpha/examples/analytics.php';
$client_secret = 'xxx';

// Initialise the Google Client object
$client = new Google_Client();
$client->setApplicationName('Your product name');
$client->setRedirectUri($redirect_url);
$client->setClientSecret($client_secret);

$client->setAssertionCredentials(
        new Google_Auth_AssertionCredentials(
            $service_account_name,
            array('https://www.googleapis.com/auth/analytics'),
            file_get_contents($keyfile)
        )
);

// Get this from the Google Console, API Access page
$client->setClientId($client_id);
$client->setAccessType('offline_access');
$analytics = new Google_Service_Analytics($client);

// We have finished setting up the connection,
// now get some data and output the number of visits this week.

// Your analytics profile id. (Admin -> Profile Settings -> Profile ID)
$analytics_id   = 'ga:xxx';
$lastWeek       = date('Y-m-d', strtotime('-1 week'));
$today          = date('Y-m-d');

try {
    $results = $analytics->data_ga->get($analytics_id, $lastWeek, $today,'ga:visits');
    echo '<b>Number of visits this week:</b> ';
    echo $results['totalsForAllResults']['ga:visits'];
} catch(Exception $e) {
    echo 'There was an error : - ' . $e->getMessage();
}

`

解决方法:

不知道您是否自己找到了答案,但是我遇到了同样的问题,并通过在第93&行中编辑apiclient / src / Google / Service / Analytics.php文件解决了该问题. 98

          "start_date" => array(
              "location" => "query",
              "type" => "string",
              'required' => true,
          ),
            "end_date" => array(
              "location" => "query",
              "type" => "string",
              'required' => true,
          ),

需要成为:

06001

标签:api,google-analytics,google-analytics-api,php
来源: https://codeday.me/bug/20191030/1965493.html