1
votes

Goog day. When I try to get custom dimensions via API, I got error

Exception 'Google_Service_Exception' with message 'Error calling GET https://www.googleapis.com/analytics/v3/management/accounts/~all/webproperties/~all/customDimensions: (400) Cannot query by ~all for id webPropertyId'

My code

$service_account_name = '<Service Email>@developer.gserviceaccount.com';
$key_file_location = '<keyName>.p12';
$key = file_get_contents($key_file_location);
$cred = new Google_Auth_AssertionCredentials(
    $service_account_name,
    array(Google_Service_Analytics::ANALYTICS),
    $key,
    'notasecret',
    'http://oauth.net/grant_type/jwt/1.0/bearer',
    '<My email>'
);
$client->getAuth()->setAssertionCredentials($cred);
$service = new Google_Service_Analytics($client);
$result = $service->management_customDimensions->listManagementCustomDimensions('~all', '~all');
print_r($result);

Similar code for getting goals works correctly

$service_account_name = '<Service Email>@developer.gserviceaccount.com';
$key_file_location = '<keyName>.p12';
$key = file_get_contents($key_file_location);
$cred = new Google_Auth_AssertionCredentials(
    $service_account_name,
    array(Google_Service_Analytics::ANALYTICS),
    $key,
    'notasecret',
    'http://oauth.net/grant_type/jwt/1.0/bearer',
    '<My email>'
);
$client->getAuth()->setAssertionCredentials($cred);
$service = new Google_Service_Analytics($client);
$result = $service->management_profiles->listManagementProfiles('~all', '~all');
print_r($result);

Both methods listManagementProfiles and listManagementProfiles get parametrs $accountId and $webPropertyId . Could someone help, why I get error, while getting custom dimensions via API?

1
A note for others calling listManagementProfiles with the ~all parameter, If you want a list of All accounts, properties and views you should call the Account Summaries endpoint.Matt

1 Answers

1
votes

Looking at the documentation "~all" is specifically mentioned as valid parameter value for listManagementProfiles:

Account ID for the view (profiles) to retrieve. Can either be a specific account ID or '~all', which refers to all the accounts to which the user has access.

but not for listManagementCustomDimensions, here is says simply

Account ID for the custom dimensions to retrieve.

(same for property id). So your problem is quite literally what the error message says, you cannot use "~all" when querying custom dimensions.

So it seems that to list all custom dimensions you'd have to iterate through a list of property ids (as returned by the properties/list method) instead of using "~all".