Google Analytics Data API via PHP
I used PHP to write programs that call Google Analytics Data API. Here are typical steps I will go through when I have to use it again in a new VPS from a hosting company.
Step 1: Enable the Google Analytics Data API v1
You have to create a new Google Cloud project, enable the Google Analytics Data API v1 and create the service account needed. Visit this Google Analytics API Quickstart page, there is a button for you to quickly do this step. The most important result is that you get credentials.json for your PHP programs later.
Step 2: Add Service Account to the Google Analytics property
Using a text editor, open the credentials.json file downloaded in the previous step and search for client_email field to obtain the service account email address, such as "quickstart@PROJECT-ID.iam.gserviceaccount.com".
Use this email address to add a user to the Google Analytics property you want to access using the Google Analytics Data API v1. For the access right, only Viewer permissions are needed.
How? Go to your Google Analytics account. Then, from the leftmost panel, click and go to the Admin area. From there, click the "Property access management" under the Property settings: Property. Finally, add a new user and add access permissions.
Step 3: Download and Install Composer
Composer is an application-level package manager for the PHP programming language.
Step 4: Download the PHP Client Library
Create a new folder for your project (say ga4-programs). Within the folder, run this Linux command:
$ composer require google/analytics-data
You should observe that a "composer.json" file and a "vendor" folder have been created within the folder.
Step 5: Create an example PHP file for testing
To test the Google Analytics Data API, create a new file example.php and paste below code in it.
<?php
require 'vendor/autoload.php';
use Google\Analytics\Data\V1beta\Client\BetaAnalyticsDataClient;
use Google\Analytics\Data\V1beta\DateRange;
use Google\Analytics\Data\V1beta\Dimension;
use Google\Analytics\Data\V1beta\Metric;
use Google\Analytics\Data\V1beta\RunReportRequest;
putenv("GOOGLE_APPLICATION_CREDENTIALS=/your_folder/credentials.json");
/**
* TODO(developer): Replace this variable with your Google Analytics 4
* property ID before running the sample.
*/
$property_id = 'YOUR-GA4-PROPERTY-ID';
// Using a default constructor instructs the client to use the credentials
// specified in GOOGLE_APPLICATION_CREDENTIALS environment variable.
$client = new BetaAnalyticsDataClient();
// Make an API call.
$request = (new RunReportRequest())
->setProperty('properties/' . $property_id)
->setDateRanges([
new DateRange([
'start_date' => '2024-04-31',
'end_date' => 'today',
]),
])
->setDimensions([new Dimension([
'name' => 'city',
]),
])
->setMetrics([new Metric([
'name' => 'activeUsers',
])
]);
$response = $client->runReport($request);
// Print results of an API call.
print 'Report result: ' . PHP_EOL;
foreach ($response->getRows() as $row) {
print $row->getDimensionValues()[0]->getValue()
. ' ' . $row->getMetricValues()[0]->getValue() . PHP_EOL;
}
In your PHP programs, you need to input the GA4 Property ID and the location where you put the credentials.json file downloaded in the previous step.
Step 6: Explore how to use Google Analytics Data API
You are now able to make API calls for your needs. Go to this: Google Analytics Data API Overview, to explore yourself.