如何获取 Google Analytics GA4 帐户的所有媒体资源

Vic*_*anu 4 php google-analytics google-analytics-api google-api-php-client google-analytics-4

如何通过 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();
        [...]
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,此方法仅允许我检索通用分析属性,而不是新的 GA4 属性。官方文档根本没有任何帮助。

DaI*_*mTo 5

Google Analytics GA4与通用分析不同。

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

$accounts = $analytics->management_accounts->listManagementAccounts();
Run Code Online (Sandbox Code Playgroud)

您将需要使用管理 API 来列出 Ga4 帐户。

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

  "accountSummaries": [
    {
      object (AccountSummary)
    }
  ],
  "nextPageToken": string
}
Run Code Online (Sandbox Code Playgroud)

在撰写本文时,他们尚未发布用于 Admin api 的 PHP 客户端库。当它发布时,我将更新它的链接。

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