2016-11-07 84 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即可。