Google Analytics Admin API Client Library

0.27.0 · active · verified Sat Mar 28

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

Install

Imports

Quickstart

This quickstart demonstrates how to instantiate the `AnalyticsAdminServiceClient` and list all accessible Google Analytics account summaries. Authentication is typically handled automatically if the `GOOGLE_APPLICATION_CREDENTIALS` environment variable is set to the path of a service account key file. Ensure the service account has appropriate permissions (e.g., 'Google Analytics Administrator').

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()

view raw JSON →