![](/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.