古吉日。当我尝试通过 API 获取自定义尺寸时,出现错误
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'
我的代码
$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);
用于获取目标的类似代码可以正常工作
$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);
listManagementProfiles 和 listManagementProfiles 两种方法都获取参数 $accountId 和 $webPropertyId 。 有人可以帮忙吗,为什么我在通过 API 获取自定义尺寸时会出现错误?
最佳答案
查看文档“~all”
是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.
但不适用于 listManagementCustomDimensions ,这里简单说一下
Account ID for the custom dimensions to retrieve.
(与属性 ID 相同)。因此,您的问题实际上就是错误消息所说的内容,查询自定义维度时不能使用 "~all"
。
因此,要列出所有自定义维度,您必须迭代属性 ID 列表(由 properties/list 方法返回),而不是使用 "~all"。
关于google-analytics - 通过 API 从 Google Analytics 获取自定义维度时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34523435/