PHP Google AnalyticsAPI - 简单示例

Ing*_*dak 8 php api google-analytics google-analytics-api

我正在尝试设置一个使用此库的Google Analytics的基本示例:https://github.com/google/google-api-php-client

对于初学者我有:

<?php

require_once 'Google/Client.php';
require_once 'Google/Service/Analytics.php';
$client = new Google_Client();
$client->setApplicationName("Client_Library_Examples");
$client->setDeveloperKey("MY_SECRET_API"); //security measures
$service = new Google_Service_Analytics($client);

$results = $service->data_ga;

echo '<pre>';
print_r($results);
echo '</pre>';
Run Code Online (Sandbox Code Playgroud)

问:如何从此查询中获取Google Analytics的数据?

/*
  https://www.googleapis.com/analytics/v3/data/
  ga?ids=ga%123456
  &dimensions=ga%3Acampaign
  &metrics=ga%3Atransactions
  &start-date=2013-12-25
  &end-date=2014-01-08
  &max-results=50
 */
Run Code Online (Sandbox Code Playgroud)

DaI*_*mTo 0

您需要执行 http get 来从 url 获取信息。

http://www.php.net/manual/en/function.http-get.php

请记住,您仍然需要将 Oauth2 身份验证代码添加到字符串中,然后才能发送该请求。如果您还没有授权码,此链接可能会有所帮助。 https://developers.google.com/analytics/solutions/articles/hello-analytics-api#authorize_access