Google Analytics Admin API Client Library
The `google-analytics-admin` library provides a Python client for the Google Analytics Admin API, allowing programmatic management of Google Analytics 4 (GA4) account and property configurations. This includes tasks like managing accounts, properties, data streams, user links, custom dimensions, and metrics. It is currently at version 0.27.0 and is part of the actively developed Google Cloud Python client libraries, which typically see frequent updates.
Warnings
- breaking Breaking changes occur frequently in pre-1.0 versions. For example, version 0.2.0 introduced several changes including making `update_mask` required for update operations, renaming fields like `country_code` to `region_code` in the `Account` resource, and renaming `url_query_parameter` to `uri_query_parameter` in `EnhancedMeasurementSettings`.
- gotcha The Google Analytics Admin API is designed specifically for Google Analytics 4 (GA4) properties. It is not compatible with Universal Analytics (GA3) properties. Users migrating from GA3 will need to understand the GA4 data model and API structure.
- gotcha Resource names often follow specific formats (e.g., `accounts/{account_id}`, `properties/{property_id}`). Incorrectly formatted resource names will result in API errors.
- deprecated Support for Python 2 has been dropped for new client library versions as of January 1, 2020. This library requires Python 3.7 or newer.
- breaking A breaking change was introduced that updated the enum value for `KEY_EVENT` from 32 to 30 within the `ChangeHistoryResourceType`. This could affect code that relies on specific enum integer values.
Install
-
pip install google-analytics-admin
Imports
- AnalyticsAdminServiceClient
from google.analytics.admin_v1beta import AnalyticsAdminServiceClient
- AnalyticsAdminServiceAsyncClient
from google.analytics.admin_v1beta import AnalyticsAdminServiceAsyncClient
Quickstart
import os
from google.analytics.admin_v1beta import AnalyticsAdminServiceClient
# Ensure GOOGLE_APPLICATION_CREDENTIALS environment variable is set
# or pass credentials explicitly.
# Example: os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = '/path/to/your/key.json'
def list_account_summaries():
"""Lists all accessible account summaries for the authenticated user."""
try:
client = AnalyticsAdminServiceClient()
print("Listing account summaries:")
for account_summary in client.list_account_summaries():
print(f" Account: {account_summary.display_name} ({account_summary.account})")
for property_summary in account_summary.property_summaries:
print(f" Property: {property_summary.display_name} ({property_summary.property}) - Type: {property_summary.property_type}")
except Exception as e:
print(f"An error occurred: {e}")
print("Please ensure you have set up authentication (e.g., GOOGLE_APPLICATION_CREDENTIALS) and enabled the Analytics Admin API.")
if __name__ == '__main__':
list_account_summaries()