gpt4 book ai didi

php - 如何获取 Google Analytics GA4 帐户的所有属性

转载 作者:行者123 更新时间:2023-12-05 02:02:22 24 4
gpt4 key购买 nike

如何通过 PHP 使用 Google Analytics GA4 列出所有帐户的所有属性?对于通用分析,我使用以下内容:

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 属性。官方文档根本没有帮助。

最佳答案

Google analytics GA4universal analytics 不同.

您可以使用 managment api列出通用分析帐户的所有属性。

$accounts = $analytics->management_accounts->listManagementAccounts();

您将需要使用 Admin api列出 Ga4 帐户。

GET https://analyticsadmin.googleapis.com/v1alpha/accountSummaries

"accountSummaries": [
{
object (AccountSummary)
}
],
"nextPageToken": string
}

在撰写本文时,他们还没有为 Admin api 发布 PHP 客户端库。当它发布时,我会更新它的链接。

[更新] 客户端库现已在 https://developers.google.com/analytics/devguides/config/admin/v1/client-libraries#php 可用。

关于php - 如何获取 Google Analytics GA4 帐户的所有属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65808186/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com