我的谷歌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?
非常感谢你