google-analytics - 使用带有服务帐户的PHP的Google Analytics API

标签 google-analytics google-analytics-api

我已经搜索并查找了两天,但无法弄清楚我要去哪里。我正在尝试使用php从我的Google Analytics(分析)帐户中提取数据。我已经按照所有步骤设置了服务帐户并下载了JSON文件。这是我正在使用的代码:

// Load the Google API PHP Client Library
require_once ('/google-api-php-client-1-master/src/Google/autoload.php');
$KEY_FILE_LOCATION = '/inc/ac_key.json';
// Create and configure a new client object.
$client = new Google_Client();
$client->setApplicationName("Hello Analytics Reporting");
$client->setAuthConfig($KEY_FILE_LOCATION);
$client->setScopes(['https://www.googleapis.com/auth/analytics.readonly']);
$analytics = new Google_Service_Analytics($client);
function getResults($analytics, $profileId) {
  // Calls the Core Reporting API and queries for the number of sessions
  // for the last seven days.
   return $analytics->data_ga->get(
       'ga:' . $profileId,
       '30daysAgo',
       'today',
       'ga:pageviews'
      );
}
$profile = $r['google_analytics'];
$results = getResults($analytics, $profile);
$month = date('n');
$year = date('Y');
$results->setMonth($month,$year);
$visits = $results->getVisitors();
$views = $results->getPageviews();
/* build tables */
if(count($visits)) {
    foreach($visits as $day=>$visit) { 
        $flot_datas_visits[] = '['.$day.','.$visit.']';
        $flot_datas_views[] = '['.$day.','.$views[$day].']';
    }
    $flot_data_visits = '['.implode(',',$flot_datas_visits).']';
    $flot_data_views = '['.implode(',',$flot_datas_views).']';
}


我收到无效的client_secret.json文件错误,但我尝试使用服务帐户进行身份验证,因此我不确定这里发生了什么。然后,我尝试将数据绘制到表中,但是我并不担心那部分,因为我仍在尝试克服第一个障碍。任何帮助,将不胜感激!

最佳答案

我不太了解Google PHP API客户端的最新信息,但显然提供了不同的版本-克隆存储库时,我得到了您应用程序使用的版本,但我没有使用它来处理我的凭据文件。然后,我通过composer安装了该库的当前版本:

composer require google/apiclient:^2.0


然后,我可以使用以下代码访问Google Analytics(分析):

<?php
include_once __DIR__ . '/vendor/autoload.php';
date_default_timezone_set('America/Los_Angeles');
$client = new Google_Client();

$client->setAuthConfig('/path/to/credentials.json');

$client->setApplicationName("Client_Library_Examples");
$client->setScopes(['https://www.googleapis.com/auth/analytics.readonly']);
$service = new Google_Service_Analytics($client);

function getResults($service, $profileId) {
   return $service->data_ga->get(
       'ga:' . $profileId,
       '30daysAgo',
       'today',
       'ga:pageviews'
      );
}

$profile= "80493610";
$results = getResults($service, $profile);

print_r($results);


经过一番摸索,我设法从您的示例中获取具有旧版本的分析服务的实例。显然,您必须使用loadServiceAccountJson函数而不是setAuthConfig

<?php

// Load the Google API PHP Client Library
require_once ('google-api-php-client/src/Google/autoload.php');
$KEY_FILE_LOCATION =  '/path/to/credentials.json';
// Create and configure a new client object.
$client = new Google_Client();
$client->setApplicationName("Hello Analytics Reporting");
$scopes = ['https://www.googleapis.com/auth/analytics.readonly'];
$client->loadServiceAccountJson($KEY_FILE_LOCATION, $scopes);

$analytics = new Google_Service_Analytics($client);

print_r($analytics);

关于google-analytics - 使用带有服务帐户的PHP的Google Analytics API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39800817/

相关文章:

javascript - Google Universal Analytics - 设置自定义属性

google-analytics - 可以从 Google Analytics Reporting API V4 中提取客户 ID 吗?

google-analytics - Google Analytics Sync

google-analytics - 谷歌分析 API : Include profile domain in Profile Feed

google-analytics - Jwplayer 使用 GTM 进行视频跟踪

java - 致命异常 : main java. lang.NoClassDefFoundError : com. google.analytics.tracking.android.EasyTracker

javascript - 如何判断每个用户访问每个页面的时间?

android - Flurry Session 数据对比 Google Analytics

google-analytics - 您认为哪些 Google Analytics 信息最有用?

javascript - Google Analytics 自定义变量的使用