Reputation: 630
How do I list all properties for all account using Google Analytics GA4 through PHP? For universal analyitcs I'm using the following:
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();
[...]
}
}
However, this method only allows me to retrieve Universal analytics properties, not the new GA4 ones. The official documentation was of no help at all.
Upvotes: 4
Views: 4241
Reputation: 630
It has been a while and the documentation is sparse. To get all GA4 properties via the PHP Library, a working example would be:
<?php
use Google\Analytics\Admin\V1alpha\AnalyticsAdminServiceClient;
use Google\Analytics\Data\V1beta\BetaAnalyticsDataClient;
use Google\Analytics\Data\V1beta\DateRange;
use Google\Analytics\Data\V1beta\Dimension;
use Google\Analytics\Data\V1beta\Metric;
putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/confing.json' );
$client = new AnalyticsAdminServiceClient();
$accounts = $client->listAccountSummaries();
foreach ($accounts as $account) {
$summary = $account->getPropertySummaries();
foreach ($summary as $sum) {
$clientGA4 = new BetaAnalyticsDataClient();
$response = $clientGA4->runReport([
'property' => $sum->getProperty(),
'dateRanges' => [new DateRange(['start_date' => '7daysAgo', 'end_date' => '1daysAgo'])],
'metrics' => [new Metric(['name' => 'sessions']), new Metric(['name' => 'totalUsers'])],
'dimensions' => [new Dimension(['name' => 'streamName'])],
]);
foreach ($response->getRows() as $row) {
echo $row->getDimensionValues()[0]->getValue() . "<br/>";
echo $row->getMetricValues()[0]->getValue() . "<br/>";
echo $row->getMetricValues()[1]->getValue() . "<br/>";
}
}
}
?>
This will list all properties from all accounts on which the config file has access. I've added 1 dimension and two metrics for this example. Don't forget to install via composer the libraries:
$ composer require google/analytics-data
$ composer require google/analytics-admin
You can find more about them here: https://github.com/googleapis/php-analytics-admin and here https://github.com/googleapis/php-analytics-data.
Took me a while to put all this together as the documentation is pretty bad as always. Hope this helps somebody.
Keep in mind that this only works with GA4, Universal Analytics will not work this way, for now at least. You will still need to use the old version until it will be deprecated in 2023.
Upvotes: 3
Reputation: 116918
Google analytics GA4 is not the same as universal analytics.
You can use the managment api to list all of the properties for Univeral analytics acocunts.
$accounts = $analytics->management_accounts->listManagementAccounts();
You will need to use the Admin api to list the Ga4 accounts.
GET https://analyticsadmin.googleapis.com/v1alpha/accountSummaries
"accountSummaries": [
{
object (AccountSummary)
}
],
"nextPageToken": string
}
At the time of writing they have not released a PHP client library for the Admin api yet. I will update with a link to it when it is released.
[update] The client libraries are available now at https://developers.google.com/analytics/devguides/config/admin/v1/client-libraries#php
Upvotes: 5