Learn business growth with Google Analytics 4 › Forums › Google Analytics 4 › . How to retrieve the property_id for a Google Analytics account using the GA4 Data API and Python › Reply To: . How to retrieve the property_id for a Google Analytics account using the GA4 Data API and Python
-
Hey, let’s start by setting your credentials:
`python
import osos.environ[‘GOOGLE_APPLICATION_CREDENTIALS’] = ‘path_to_your_client_secrets.json’
`
This step tells our Google SDKs to look for credentials in the specified location.Next, to list all the ‘Analytics accounts’ and their associated ‘property_id,’ check out this resource for getting property_ids using the Google Analytics 4 API – [here](https://developers.google.com/analytics/devguides/migration/api/management-ua-to-ga4).
Here’s a helpful Python script that I pulled together:
`python
from google.analytics.admin import AnalyticsAdminServiceClientdef list_account_summaries(transport: str = ‘rest’):
“””
Prints summaries of all accounts accessible by the caller.Args:
transport(str): The transport to use. Could be “grpc” or “rest”.
“””
client = AnalyticsAdminServiceClient(transport=transport)
results = client.list_account_summaries()print(“Result:”)
for account_summary in results:
print(“– Account –“)
print(f”Resource name: {account_summary.name}”)
print(f”Account name: {account_summary.account}”)
print(f”Display name: {account_summary.display_name}”)
print()
for property_summary in account_summary.property_summaries:
print(“– Property –“)
print(f”Property resource name: {property_summary.property}”)
print(f”Property display name: {property_summary.display_name}”)
print()# In the GA4 API, ‘Account’ refers to the old ‘UA’, and ‘Property’ refers to the GA4 equivalent.
list_account_summaries()
`
Give this a shot and let me know how it goes!