google-analytics - 通过 API 从 Google Analytics 获取自定义维度时出现问题

标签 google-analytics google-analytics-api google-api-php-client

古吉日。当我尝试通过 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/

相关文章:

google-analytics - 从 Google 电子表格调用 Google Analytics API 时出现错误 'Login Required'

google-analytics - Google Analytics API-页面创建日期

php - 使用Google服务帐户时是否需要刷新 token

google-analytics - 谷歌分析多久更新一次流量数据

google-analytics - 冗余主机名 Google Analytics

google-analytics - 推荐排除列表不起作用(谷歌分析)

google-analytics - GA :dfpRevenue is Restricted

javascript - Google Embed API 图表类型?

google-analytics - 在 Google Analytics 嵌入 API 中正确使用过滤器

google-api-php-client - Google api php 客户端给出 "malformed auth code"错误