大约一周后 - 当然,在同一天我发布这个问题,我终于设法自己解决这个问题。
这是我如何去:
我从their github下载了最新的谷歌客户端API(对于PHP)。
我在我的应用程序/ third_party文件夹中添加了Google文件夹(src)。
在我的控制我包括所需的文件做:
require_once(BASEPATH . '../application/third_party/Google/Client.php');
require_once(BASEPATH . '../application/third_party/Google/Service/Analytics.php');
然后我在下面,下面的代码进行授权与服务帐户(你从Google Console获取和选择项目>的API & AUTH >凭据>然后创建一个新的客户端ID,选择服务帐户,完成后,点击“生成新的P12键”和键添加到您的THIRD_PARTY /谷歌文件夹:
session_start();
$client_id = '<YOUR_CLIENT_ID>'; //Client ID
$service_account_name = '<YOUR_CLIENT_EMAIL>'; //Email Address
$key_file_location = BASEPATH . '../application/third_party/Google/<YOUR KEY.p12>'; //key.p12
$client = new Google_Client();
$client->setApplicationName("ApplicationName");
$service = new Google_Service_Analytics($client);
if (isset($_SESSION['service_token'])) {
$client->setAccessToken($_SESSION['service_token']);
}
$key = file_get_contents($key_file_location);
$cred = new Google_Auth_AssertionCredentials(
$service_account_name,
array(
'https://www.googleapis.com/auth/analytics',
),
$key,
'notasecret'
);
$client->setAssertionCredentials($cred);
if($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion($cred);
}
$_SESSION['service_token'] = $client->getAccessToken();
下面的代码是FO从上31天
$analytics = new Google_Service_Analytics($client);
$profileId = "ga:<YOUR_PROFILE_ID>";
$startDate = date('Y-m-d', strtotime('-31 days')); // 31 days from now
$endDate = date('Y-m-d'); // todays date
$metrics = "ga:sessions";
$optParams = array("dimensions" => "ga:date");
$results = $analytics->data_ga->get($profileId, $startDate, $endDate, $metrics, $optParams);
$data['report'] = $results->rows; //To send it to the view later
[R越来越会话(页面访问量)要获得所有可以使用的维度和指标,use this link。关于如何将其发送到视图 实施例:
$this->view->load('your_view', $data);
要写入它作为我只是用Google charts一个图表,在JS(在视图中)我刚刚从$循环数据的数据[“报告” ]绘制图表。
希望这将有助于未来这个问题的人。
我不明白为什么有人会低估这一点,因为更多的人可能有这个问题!我已经尝试过Sparks for Codeigniter的插件,但那个是两年前的,这意味着api已经过时了,并且所有在Google网站上的示例中的命名约定都是错误的... – Michael 2014-09-04 14:35:56