Reputation: 846
My Google Analytics Dashboard widget is nearly ready the last thing I need to do is improving the performance.
I created a file upload for a service account credential so the user can move it to the server and after that he should decide which project he/she wants to track just like here https://ga-dev-tools.appspot.com/embed-api/basic-dashboard/ but I want to create only one single select field where all projects are.
I upload the credential via an ajax request and want to return a json object with all profile IDs. The problem is: as soon as there are more than about 40 accounts/properties/views connected to the account I get an exception because it takes more than 30s to load.
This is the code but it is too slow or did I make any mistakes?
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;
}
I know its a little bit tricky to understand but to make it simple this code just loops through the entire account and searches for all projects, properties and so on to find all profile IDs that are connected to the service account credential and saves the needed values in a $arrSelectionList
but in some point it becomes way too slow and I don't know how to make it faster.
Could you please help me and make my code faster or tell me if there is a better way to get all profile ids?
Thank you very much
Upvotes: 1
Views: 341
Reputation: 116918
Its hard to tell but it looks like you are making a lot of calls to the different parts of the api.
Use account summaries your code is slow because you are making so many requests to the api. Account summaries will return all the accounts and their associated web properties and profiles. For the current authenticated user in this instance the service account. In one single call to the Google Analytics management API.
Upvotes: 1