php - Google Analytics 数据导出 API V3

标签 php api google-analytics

我正在尝试使用 php 来掌握与 GA API v3 的交互。作为 php 的新手,我有些挣扎。这里有人有使用 php (v3) api 的经验吗?

http://code.google.com/intl/nl/apis/analytics/docs/index.html

Google 确实提供了一个小示例脚本,但它实际上毫无用处(以我有限的技能而言),因为它返回一个 api key ,但没有告诉您它需要去哪里或为什么需要它。

如果有人有任何知识,如果您能告诉我如何做,我将非常感激。

最佳答案

您需要确保通过 Google 的 API 控制台向 Google 注册您的 API。确保打开 Google Analytics(分析)并创建一个项目。

确保从 Google Code 下载完整的 api .

您想要进入位于 Analytics 文件夹(在示例下)的 simple.php 并取消注释第 11-14 行,并替换为 Google API 控制台中的信息:

$client->setClientId('xxxx.apps.googleusercontent.com');
$client->setClientSecret('xxxxxx');
$client->setRedirectUri('http://www.xxxx.com/xxx/examples/analytics/simple.php');
$client->setDeveloperKey('xxxxxxxxxxx');

这将让您连接并且您将看到基本数据。有关更多详细信息和精彩教程,您可以查看 here .

您的总页面应如下所示:

<?php
require_once '../../src/apiClient.php';
require_once '../../src/contrib/apiAnalyticsService.php';
session_start();

$client = new apiClient();
$client->setApplicationName("Google Analytics PHP Starter Application");

// Visit https://code.google.com/apis/console?api=analytics to generate your
// client id, client secret, and to register your redirect uri.
$client->setClientId('addyourshere');
$client->setClientSecret('addyourshere');
$client->setRedirectUri('addyourshere');
$client->setDeveloperKey('addyourshere');
$service = new apiAnalyticsService($client);

if (isset($_GET['logout'])) {
  unset($_SESSION['token']);
}

if (isset($_GET['code'])) {
  $client->authenticate();
  $_SESSION['token'] = $client->getAccessToken();
  header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
}

if (isset($_SESSION['token'])) {
  $client->setAccessToken($_SESSION['token']);
}

if ($client->getAccessToken()) {

  $props = $service->management_webproperties->listManagementWebproperties("~all");
 print "<h1>Web Properties</h1><pre>" . print_r($props, true) . "</pre>";

  $accounts = $service->management_accounts->listManagementAccounts();
  print "<h1>Accounts</h1><pre>" . print_r($accounts, true) . "</pre>";

  $segments = $service->management_segments->listManagementSegments();
  print "<h1>Segments</h1><pre>" . print_r($segments, true) . "</pre>";

  $goals = $service->management_goals->listManagementGoals("~all", "~all", "~all");
 print "<h1>Segments</h1><pre>" . print_r($goals, true) . "</pre>";



  $_SESSION['token'] = $client->getAccessToken();
} else {
  $authUrl = $client->createAuthUrl();
  print "<a class='login' href='$authUrl'>Connect Me!</a>";
}

关于php - Google Analytics 数据导出 API V3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9066544/

相关文章:

google-analytics - Android 实时分析

javascript - 第三方 JavaScript 小部件的分析

php - 如何在 PHP 的某些要求下增加版本号

php - PHP 数组值的存储位置

php - $_SERVER ['REMOTE_ADDR'] 无法正常工作

javascript - 为什么我的 addPoint() 在我的 highcharts 代码中没有被识别为函数?

php - fatal error :调用未定义的方法 mysqli::error()

java - 使用 Java(特别是 Android)发布到 REST api

android - 使用 FFmpeg C API 调整视频大小

google-analytics - Google Analytics gtag 如何在 SPA 中工作?