PHP谷歌Analytics(分析)API - 简单的例子 [英] PHP Google Analytics API - Simple example

查看:262
本文介绍了PHP谷歌Analytics(分析)API - 简单的例子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置使用谷歌Analytics(分析)与此库的一些基本的例子:<一href=\"https://github.com/google/google-api-php-client\">https://github.com/google/google-api-php-client

有关首发我有:

 &LT; PHPrequire_once'谷歌/ Client.php';
require_once'谷歌/服务/ Analytics.php';
$客户端=新Google_Client();
$客户 - &GT; setApplicationName(Client_Library_Examples);
$客户 - &GT; setDeveloperKey(MY_SECRET_API); //安防措施
$ =服务新Google_Service_Analytics($客户端);$结果= $服务 - &GT; data_ga;呼应'&LT; pre&GT;';
的print_r($结果);
呼应'&LT; / pre&GT;';

问:如何从这个查询获得谷歌Analytics的数据

  / *
  https://www.googleapis.com/analytics/v3/data/
  嘎?IDS =ガ%123456
  &安培;尺寸=ガ%3Acampaign
  &安培;指标=ガ%3Atransactions
  &功放;启动日期= 2013年12月25日
  &安培;结束日期=少儿模特
  &安培;最大结果= 50
 * /


解决方案

  $客户 - &GT; setDeveloperKey(MY_SECRET_API);

首先,对于只要我经历这不会进行认证工作,你需要使用的OAuth2验证。有两个选项要做到这一点,使用客户端ID为Web应用程序或使用服务帐户。 授权API

在你有这个,你可以做这样一个电话。
(我在这里使用一个服务帐户)

首先进行身份验证:

  $键=的file_get_contents($ KEY_FILE_LOCATION);
$ CRED =新Google_Auth_AssertionCredentials(
    $ service_account_name,
    阵列('https://www.googleapis.com/auth/analytics.readonly'),
    $关键
);
$客户 - &GT; setAssertionCredentials($ CRED)

拨打电话:

  $ IDS ='GA:123456'; //你的身份证
$的startDate ='2013年12月25日;
$结束日期='少儿模特;
$指标='GA:交易';$ optParams =阵列(
    '尺寸'=&GT; GA:运动',
    最大结果'=&GT; 50
);$结果= $服务 - &GT; data_ga-&GT;获得($ IDS,$的startDate,$结束日期,$指标,$ optParams);//转储结果
回声&LT; H3&GT;结果召唤:LT; / H3&gt;中;回声结果场;
后续代码var_dump($结果);回声结果['totalsForAllResults'];
后续代码var_dump($结果['totalsForAllResults']);回声结果['行'];
的foreach($结果['行']为$项目){
    后续代码var_dump($项目);
}

I am trying to set some basic example of using Google Analytics with this library: https://github.com/google/google-api-php-client

For starter I have:

<?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>';

Q: How to get data from Google Analytics from this query ?

/*
  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
 */

解决方案

$client->setDeveloperKey("MY_SECRET_API");

First of all, for as far as I experienced this won't work for authentication, you'll need to use a OAuth2 authentication. There are two options to do this, using client ID for web application or using a service account. Authorization api

After you have this, you can make a call like this. (I use a service account here)

First authenticate:

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

Make a call:

$ids = 'ga:123456'; //your id
$startDate = '2013-12-25';
$endDate = '2014-01-08';
$metrics = 'ga:transactions';

$optParams = array(
    'dimensions' => 'ga:campaign',
    'max-results' => '50'
);

$results = $service->data_ga->get($ids, $startDate, $endDate, $metrics, $optParams);

//Dump results
echo "<h3>Results Of Call:</h3>";

echo "dump of results";
var_dump($results);

echo "results['totalsForAllResults']";
var_dump($results['totalsForAllResults']);

echo "results['rows']";
foreach ($results['rows'] as $item) {
    var_dump($item);
}

这篇关于PHP谷歌Analytics(分析)API - 简单的例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆