gpt4 book ai didi

php - Google Analytics 从凭据中获取所有配置文件 ID

转载 作者:行者123 更新时间:2023-12-03 16:16:53 25 4
gpt4 key购买 nike

我的 Google Analytics(分析)仪表板小部件几乎准备就绪,我需要做的最后一件事就是提高性能。
我为服务帐户凭据创建了文件上传,因此用户可以将其移动到服务器,然后他应该决定他/她想要跟踪的项目,就像这里 https://ga-dev-tools.appspot.com/embed-api/basic-dashboard/但我只想创建一个所有项目所在的选择字段。

我通过 ajax 请求上传凭据,并希望返回一个包含所有配置文件 ID 的 json 对象。问题是:一旦有超过 40 个帐户/属性/ View 连接到该帐户,我就会收到异常,因为加载需要 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 的不同部分进行大量调用。

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

关于php - Google Analytics 从凭据中获取所有配置文件 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40463524/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com