Page Summary
-
Use either the Data API or Admin API for this quickstart.
-
Authenticate with a user account or service account.
-
This quickstart demonstrates how to create and send a
runReportrequest using a service account. -
The steps involve setting up tools and access, enabling the API, installing an SDK, and making an API call.
-
You can find client library installation guides and code samples for various programming languages, including Java, PHP, Python, Node.js, .NET, Ruby, Go, and REST.
You can use the Data API or Admin API for this quickstart
You can authenticate with a user account or service account:
In this quickstart, you create and send a
runReport
request.
Here's a summary of the steps:
- Set up tools and access.
- Enable the API.
- Install an SDK.
- Make an API call.
Before you begin
Install and initialize the gcloud CLI.
To generate Application Default Credentials and give your account the necessary scopes, run the following:
gcloud auth application-default login --scopes="https://www.googleapis.com/auth/cloud-platform,https://www.googleapis.com/auth/analytics.readonly"In the Google Analytics UI, grant your user account access to a Google Analytics property.
Enable the Data API
To select or create a Google Cloud project, and enable the API, click Enable the Google Analytics Data API v1.
Enable the Google Analytics APIInstall an SDK
Install the SDK for your programming language.
Java
PHP
Python
Node.js
.NET
Ruby
Go
go get google.golang.org/genproto/googleapis/analytics/data/v1beta
REST
Configure your environment variables by entering the following.
Replace PROJECT_ID with the ID of your project
and PROPERTY_ID with the ID of your Google Analytics property.
export PROJECT_ID=PROJECT_IDexport PROPERTY_ID=PROPERTY_ID
Make an API call
To verify your setup and make an API call, run the following sample.
This sample calls the
runReport
method. The response lists active users for your property.
To install all the Analytics API code samples, see our GitHub.
Java
import com.google.analytics.data.v1beta.BetaAnalyticsDataClient; import com.google.analytics.data.v1beta.DateRange; import com.google.analytics.data.v1beta.Dimension; import com.google.analytics.data.v1beta.Metric; import com.google.analytics.data.v1beta.Row; import com.google.analytics.data.v1beta.RunReportRequest; import com.google.analytics.data.v1beta.RunReportResponse; /** * Google Analytics Data API sample quickstart application. * * <p>This application demonstrates the usage of the Analytics Data API using service account * credentials. * * <p>Before you start the application, please review the comments starting with "TODO(developer)" * and update the code to use correct values. * * <p>To run this sample using Maven: * * <pre>{@code * cd google-analytics-data * mvn compile exec:java -Dexec.mainClass="com.google.analytics.data.samples.QuickstartSample" * }</pre> */ public class QuickstartSample { public static void main(String... args) throws Exception { /** * TODO(developer): Replace this variable with your Google Analytics 4 property ID before * running the sample. */ String propertyId = "YOUR-GA4-PROPERTY-ID"; sampleRunReport(propertyId); } // This is an example snippet that calls the Google Analytics Data API and runs a simple report // on the provided GA4 property id. static void sampleRunReport(String propertyId) throws Exception { // Using a default constructor instructs the client to use the credentials // specified in GOOGLE_APPLICATION_CREDENTIALS environment variable. try (BetaAnalyticsDataClient analyticsData = BetaAnalyticsDataClient.create()) { RunReportRequest request = RunReportRequest.newBuilder() .setProperty("properties/" + propertyId) .addDimensions(Dimension.newBuilder().setName("city")) .addMetrics(Metric.newBuilder().setName("activeUsers")) .addDateRanges(DateRange.newBuilder().setStartDate("2020-03-31").setEndDate("today")) .build(); // Make the request. RunReportResponse response = analyticsData.runReport(request);