我的Google Analytics Dashboard小部件几乎准备好了,我需要做的最后一件事就是提高性能。
我为服务帐户凭证创建了一个文件上传,以便用户可以将其移动到服务器,之后他应该决定他/她想要跟踪的项目,就像这里https://ga-dev-tools.appspot.com/embed-api/basic-dashboard/一样,但我想只创建一个选择字段,其中所有项目都是。
我通过ajax请求上传凭据,并希望返回具有所有配置文件id的json对象。问题是:只要有超过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 ?
非常感谢
很难说,但看起来你对api的不同部分进行了大量调用。
使用帐户摘要你的代码很慢,因为你向api发出了很多请求。帐户摘要将返回所有帐户及其相关的web属性和配置文件。对于此实例中当前经过身份验证的用户,服务帐户。在一个单一的调用谷歌分析管理API。