2016-11-07 86 views
1

我的谷歌Analytics(分析)控制台工具的所有配置文件的ID快到我需要做的是提高性能的最后一件事。

我创建了一个服务帐户凭据文件上传,使用户可以将其移动到服务器后,他应该决定他/她想要跟踪就像这里https://ga-dev-tools.appspot.com/embed-api/basic-dashboard/该项目,但我只希望创建一个单一选择字段所有项目都在哪里。谷歌分析得到的凭证

我通过Ajax请求上传的凭证,并希望返回一个JSON对象的所有个人资料的ID。问题是:只要有超过40个帐户/属性/视图连接到该帐户,我就会收到一个异常,因为它需要超过30秒才能加载。

这是代码,但实在是太慢了还是我犯任何错误?

public function getProfileIDs($analytics, $boolGetSelectionList = false){ 
    $arrProfileIds = []; 
    $arrSelectionList = array(); 
    $accounts = $analytics->management_accounts->listManagementAccounts(); 
    if (count($accounts->getItems()) > 0) { 
     $items = $accounts->getItems(); 
     foreach ($items as $item) { 
      $arrAccountName[] = $item->getName(); 
      $arrAccountIds = array(); 
      $arrAccountIds[] = $item->getId(); 
      foreach ($arrAccountIds as $accountId) { 
       $arrProperties = array(); 
       $arrProperties[] = $analytics->management_webproperties->listManagementWebproperties($accountId); 
       foreach ($arrProperties as $property) { 
        if (count($property->getItems()) > 0) { 
         $propertyItems = $property->getItems(); 
         foreach ($propertyItems as $propertyItem) { 
          $propertyId = $propertyItem->getId(); 
          $profiles = $analytics->management_profiles->listManagementProfiles($accountId, $propertyId); 
          if (count($profiles->getItems()) > 0) { 
           $profileitems = $profiles->getItems(); 
           foreach ($profileitems as $profileItem){ 
            $arrProfileIds[] = $profileItem->getId(); 
            $arrSelectionList[] = array(
             'URL' => $profileItem['websiteUrl'], 
             'name' => $profileItem['name'], 
             'profileID' => $profileItem->getId(), 
             'replacedURL' => str_replace('http://', '', $profileItem['websiteUrl']) . " " . $profileItem['name'] 
            ); 
           } 
          } else { 
           throw new Exception(Craft::t("Ungültiger Credential - keine Views gefunden")); 
          } 
         } 
        } else { 
         throw new Exception(Craft::t("Ungültiger Credential - keine Properties gefunden")); 
        } 
       } 
      } 
     } 
    } else { 
     throw new Exception(Craft::t("Ungültiger Credential - keine Accounts gefunden")); 
    } 
    return $arrSelectionList; 
} 

我知道它棘手的一点点理解,但做简单的代码只是通过整个帐户,并搜索所有项目,性质环等地发现连接到该服务的所有个人资料的ID帐户凭证并将所需的值保存在$arrSelectionList中,但在某些情况下,它变得太慢,我不知道如何使其更快。

你能帮我,让我的代码更快或告诉我,如果有更好的方式来获得所有配置文件ID?

非常感谢你

回答

1

很难说,但它看起来像你正在对api的不同部分进行大量的调用。

使用account summaries您的代码很慢,因为您向api发出如此多的请求。帐户摘要将返回所有帐户及其关联的网络媒体资源和配置文件。对于当前经过身份验证的用户在本例中是服务帐户。只需一次致电Google Analytics管理API即可。