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

时间:2012-09-10 23:50:05

标签: php google-analytics-api

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

我的目标是修改以下代码(取自Google的HelloAnalyticsApi.php),以便从所有中循环并“打印”个人资料ID 列表我各自的谷歌分析帐户。

目前,以下代码会检查并查看其看到的第一个帐户的第一个个人资料ID。它首先获取帐户ID,然后获取该帐户ID的网络属性,最后获取帐户的配置文件ID。这是在 getFirstprofileId(& $ analytics)函数中完成的。原样,代码工作正常。

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

(我将跳过凭证/登录并获得工作逻辑)

if (!$client->getAccessToken()) {
  $authUrl = $client->createAuthUrl();
  print "<a class='login' href='$authUrl'>Connect Me!</a>";

} else {
    $analytics = new apiAnalyticsService($client);
    runMainDemo($analytics); 

}

function runMainDemo(&$analytics) {
  try {

    // Step 2. Get the user's first profile ID.
    $profileId = getFirstProfileId($analytics);

    if (isset($profileId)) {

      // Step 3. Query the Core Reporting API.
      $results = getResults($analytics, $profileId);

      // Step 4. Output the results.
      printResults($results);
    }

  } catch (apiServiceException $e) {
    // Error from the API.
    print 'There was an API error : ' . $e->getCode() . ' : ' . $e->getMessage();

  } catch (Exception $e) {
    print 'There wan a general error : ' . $e->getMessage();
  }
}

function getFirstprofileId(&$analytics) {

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


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

    $items = $accounts->getItems();
    $firstAccountId = $items[1]->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();
        return $items[0]->getId();

      } 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.');
  }
}



function getResults(&$analytics, $profileId) {

   return $analytics->data_ga->get(
       'ga:' . $profileId,
       '2010-03-03',
      '2011-03-03',
       'ga:visits');
}


function printResults(&$results) {

  if (count($results->getRows()) > 0) {

    $profileName = $results->getProfileInfo()->getProfileName();
    $profileId = $results->getProfileInfo()->getProfileId();

    $rows = $results->getRows();
    $visits = $rows[0][0];

    print "<p>First profile found: $profileName</p>";
    print "<p>First profileId found (not account id): $profileId</p>";
    print "<p>Total visits: $visits</p>";

  } else {
    print '<p>No results found.</p>';
  }
}

?>

1 个答案:

答案 0 :(得分:3)

简单...修改功能 - 添加循环。按帐户中的项目数量循环:

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.');
          }

      }

    }