Google Analytics API quickstartStay organized with collectionsSave and categorize content based on your preferences.
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 arunReportrequest 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.
fromgoogle.analytics.data_v1betaimportBetaAnalyticsDataClientfromgoogle.analytics.data_v1beta.typesimport(DateRange,Dimension,Metric,RunReportRequest,)defsample_run_report(property_id="YOUR-GA4-PROPERTY-ID"):"""Runs a simple report on a Google Analytics 4 property."""# TODO(developer): Uncomment this variable and replace 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=BetaAnalyticsDataClient()request=RunReportRequest(property=f"properties/{property_id}",dimensions=[Dimension(name="city")],metrics=[Metric(name="activeUsers")],date_ranges=[DateRange(start_date="2020-03-31",end_date="today")],)response=client.run_report(request)print("Report result:")forrowinresponse.rows:print(row.dimension_values[0].value,row.metric_values[0].value)
/*** TODO(developer): Uncomment this variable and replace with your* Google Analytics 4 property ID before running the sample.*/// propertyId = 'YOUR-GA4-PROPERTY-ID';// Imports the Google Analytics Data API client library.const{BetaAnalyticsDataClient}=require('@google-analytics/data');// Using a default constructor instructs the client to use the credentials// specified in GOOGLE_APPLICATION_CREDENTIALS environment variable.constanalyticsDataClient=newBetaAnalyticsDataClient();// Runs a simple report.asyncfunctionrunReport(){const[response]=awaitanalyticsDataClient.runReport({property:`properties/${propertyId}`,dateRanges:[{startDate:'2020-03-31',endDate:'today',},],dimensions:[{name:'city',},],metrics:[{name:'activeUsers',},],});console.log('Report result:');response.rows.forEach((row)=>{console.log(row.dimensionValues[0],row.metricValues[0]);});}runReport();
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2026-05-04 UTC."],[],[]]