PHP和Google分析核心报告api v3-获取配置文件ID列表-而不是帐户ID

PHP和Google分析核心报告api v3-获取配置文件ID列表-而不是帐户ID,php,google-analytics-api,Php,Google Analytics Api,这个问题更多地涉及PHP的机制,而不是谷歌核心报告API的复杂性 我的目标是修改下面的代码(取自Google的HelloAnalyticsApi.php),这样它将循环并“打印”我各自的Google analytics帐户的所有配置文件ID的列表 目前,下面的代码将遍历并获取它看到的第一个帐户的第一个配置文件ID。它首先获取帐户Id,然后获取该帐户Id的web属性,最后获取帐户的配置文件Id。这是在getFirstprofileId(&$analytics)函数中完成的。按原样,代码运行良好 在

这个问题更多地涉及PHP的机制,而不是谷歌核心报告API的复杂性

我的目标是修改下面的代码(取自Google的HelloAnalyticsApi.php),这样它将循环并“打印”我各自的Google analytics帐户的所有配置文件ID的列表

目前,下面的代码将遍历并获取它看到的第一个帐户的第一个配置文件ID。它首先获取帐户Id,然后获取该帐户Id的web属性,最后获取帐户的配置文件Id。这是在getFirstprofileId(&$analytics)函数中完成的。按原样,代码运行良好

在我的例子中,我必须跳过第一个(数组中的位置0),因为尽管它是一个列出的帐户,但它没有任何webproperties。因此,我必须从1开始计数,而不是从0开始

(我将跳过凭证/登录,只获取工作逻辑)

if(!$client->getAccessToken()){
$authUrl=$client->createAuthUrl();
打印“”;
}否则{
$analytics=新的apiAnalyticsService($client);
runMainDemo($analytics);
}
函数runMainDemo(&$analytics){
试一试{
//步骤2.获取用户的第一个配置文件ID。
$profileId=getFirstProfileId($analytics);
if(isset($profileId)){
//步骤3.查询核心报告API。
$results=getResults($analytics,$profileId);
//步骤4.输出结果。
打印结果($结果);
}
}捕获(apiServiceException$e){
//来自API的错误。
打印“出现API错误:”。$e->getCode()。:”。$e->getMessage();
}捕获(例外$e){
打印“出现一般错误:”。$e->getMessage();
}
}
函数getFirstprofileId(&$analytics){
$accounts=$analytics->management_accounts->listManagementAccounts();
如果(计数($accounts->getItems())>0){
$items=$accounts->getItems();
$firstAccountId=$items[1]->getId();
//项目0是一个已列出但没有任何webproperties的帐户。
//此帐户未显示在我们的分析中
$webproperties=$analytics->management\u webproperties->listManagementWebproperties($firstAccountId);
如果(计数($webproperties->getItems())>0){
$items=$webproperties->getItems();
$firstWebpropertyId=$items[0]->getId();
$profiles=$analytics->management\u profiles->listManagementProfiles($firstAccountId,$firstWebpropertyId);
如果(计数($profiles->getItems())>0){
$items=$profiles->getItems();
返回$items[0]->getId();
}否则{
抛出新异常(“未找到此用户的配置文件”);
}
}否则{
抛出新异常(“未找到此用户的webproperties”);
}
}否则{
抛出新异常(“未找到此用户的帐户”);
}
}
函数getResults(&$analytics,$profileId){
返回$analytics->data_ga->get(
“ga:”.$profileId,
'2010-03-03',
'2011-03-03',
“ga:访问”);
}
函数printResults(&$results){
如果(计数($results->getRows())>0){
$profileName=$results->getProfileInfo()->getProfileName();
$profileId=$results->getProfileInfo()->getProfileId();
$rows=$results->getRows();
$visions=$rows[0][0];
打印“找到的第一个配置文件:$profileName

”; 打印“找到的第一个profileId(不是帐户id):$profileId

”; 打印“总访问量:$visions

”; }否则{ 打印“未找到结果。

”; } } ?>
简单…修改功能-为循环添加。按帐户中的项目数循环:

function getProfileId(&$analytics) {

    //function getFirstprofileId(&$analytics) {

      $accounts = $analytics->management_accounts->listManagementAccounts();

        $num = count($accounts->getItems());


        for ($i=1; $i < $num; $i++) { 

          if (count($accounts->getItems()) > 0) {

            $items = $accounts->getItems();
            $firstAccountId = $items[$i]->getId(); 
            //item 0 is an account that gets listed but doesn't have any webproperties.  
            //This account doesn't show in our analytics

            $webproperties = $analytics->management_webproperties->listManagementWebproperties($firstAccountId);

            if (count($webproperties->getItems()) > 0) {

              $items = $webproperties->getItems();
              $firstWebpropertyId = $items[0]->getId();

              $profiles = $analytics->management_profiles->listManagementProfiles($firstAccountId, $firstWebpropertyId);

              if (count($profiles->getItems()) > 0) {

                $items = $profiles->getItems();

                $profileId = $items[0]->getId();

                print "<p>Profile ID (not account id): ".  $profileId."</p>";



              } else {
                throw new Exception('No profiles found for this user.');
              }
            } else {
              throw new Exception('No webproperties found for this user.');
            }
          } else {
            throw new Exception('No accounts found for this user.');
          }

      }

    }
函数getProfileId(&$analytics){
//函数getFirstprofileId(&$analytics){
$accounts=$analytics->management_accounts->listManagementAccounts();
$num=count($accounts->getItems());
对于($i=1;$i<$num;$i++){
如果(计数($accounts->getItems())>0){
$items=$accounts->getItems();
$firstAccountId=$items[$i]->getId();
//项目0是一个已列出但没有任何webproperties的帐户。
//此帐户未显示在我们的分析中
$webproperties=$analytics->management\u webproperties->listManagementWebproperties($firstAccountId);
如果(计数($webproperties->getItems())>0){
$items=$webproperties->getItems();
$firstWebpropertyId=$items[0]->getId();
$profiles=$analytics->management\u profiles->listManagementProfiles($firstAccountId,$firstWebpropertyId);
如果(计数($profiles->getItems())>0){
$items=$profiles->getItems();
$profileId=$items[0]->getId();
打印“配置文件ID(非帐户ID):”$profileId。“

”; }否则{ 抛出新异常(“未找到此用户的配置文件”); } }否则{ 抛出新异常(“未找到此用户的webproperties”); } }否则{ 抛出新异常(“未找到此用户的帐户”); } } }
非常感谢您的发帖,这是一个非常新的问题,我确实有,请您引导我完成这些步骤,我将不胜感激!您是否必须将开发人员电子邮件添加到用户配置文件中?非常感谢!好的,我意识到已经过去一年了。您问题的第一部分对我来说有点不清楚。您要采取哪些步骤参考?对于第二个问题:是的,您用于登录自定义应用程序的电子邮件必须具有对URL的读取权限,并且在您的分析中具有web属性。因此,如果您的Google分析中有多个URL,则每个URL(帐户)必须将该电子邮件与之关联。如果您尝试使用上述代码,并且分析中的某个帐户没有web属性,则该代码将崩溃。
function getProfileId(&$analytics) {

    //function getFirstprofileId(&$analytics) {

      $accounts = $analytics->management_accounts->listManagementAccounts();

        $num = count($accounts->getItems());


        for ($i=1; $i < $num; $i++) { 

          if (count($accounts->getItems()) > 0) {

            $items = $accounts->getItems();
            $firstAccountId = $items[$i]->getId(); 
            //item 0 is an account that gets listed but doesn't have any webproperties.  
            //This account doesn't show in our analytics

            $webproperties = $analytics->management_webproperties->listManagementWebproperties($firstAccountId);

            if (count($webproperties->getItems()) > 0) {

              $items = $webproperties->getItems();
              $firstWebpropertyId = $items[0]->getId();

              $profiles = $analytics->management_profiles->listManagementProfiles($firstAccountId, $firstWebpropertyId);

              if (count($profiles->getItems()) > 0) {

                $items = $profiles->getItems();

                $profileId = $items[0]->getId();

                print "<p>Profile ID (not account id): ".  $profileId."</p>";



              } else {
                throw new Exception('No profiles found for this user.');
              }
            } else {
              throw new Exception('No webproperties found for this user.');
            }
          } else {
            throw new Exception('No accounts found for this user.');
          }

      }

    }