![](/img/trans.png)
[英]How to use access tokens with Google Admin API for GA4 properties?
[英]How to get all properties for Google Analytics GA4 accounts
如何列出使用 Google Analytics GA4 到 PHP 的所有帐户的所有属性? 对于通用分析,我使用以下内容:
function initializeAnalyticsV3()
{
$client = new Google_Client();
$client->setApplicationName("Name");
$client->setAuthConfig($KEY_FILE_LOCATION);
$client->setScopes(['https://www.googleapis.com/auth/analytics.readonly']);
$analytics = new Google_Service_Analytics($client);
return $analytics;
}
$analyticsV3 = initializeAnalyticsV3();
try {
$accounts = $analyticsV3->management_accountSummaries
->listManagementAccountSummaries();
} catch (apiServiceException $e) {
print 'There was an Analytics API service error '
. $e->getCode() . ':' . $e->getMessage();
} catch (apiException $e) {
print 'There was a general API error '
. $e->getCode() . ':' . $e->getMessage();
}
foreach ($accounts->getItems() as $account) {
foreach ($account->getWebProperties() as $property) {
$profile = $property->getProfiles();
[...]
}
}
但是,这种方法只允许我检索通用分析属性,而不是新的 GA4 属性。 官方文档根本没有帮助。
您可以使用管理 api列出通用分析帐户的所有属性。
$accounts = $analytics->management_accounts->listManagementAccounts();
您将需要使用管理员 api列出 Ga4 帐户。
GET https://analyticsadmin.googleapis.com/v1alpha/accountSummaries
"accountSummaries": [
{
object (AccountSummary)
}
],
"nextPageToken": string
}
在撰写本文时,他们尚未为管理员 api 发布 PHP 客户端库。 当它发布时,我会更新它的链接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.