Skip to content

Google Analytics

Analytics

Analytics(auth: GoogleAuth)

Read-only Google Analytics 4 client.

Admin API (analyticsadmin v1beta) for accounts and properties, Data API (analyticsdata v1beta) for reports. Needs Scopes.analytics().

Example

ga = Analytics(auth) ga.account_summaries() ga.run_report("123456", ["date"], ["activeUsers"], [("7daysAgo", "today")])

account_summaries

account_summaries(max_results: int | None = None) -> list[AccountSummary]

Accounts with their properties (the quickest way to find property IDs).

list_properties

list_properties(account_id: str, show_deleted: bool = False, max_results: int | None = None) -> list[Property]

Properties of an account ("123" or "accounts/123").

get_property

get_property(property_id: str | int) -> Property

One GA4 property by id (123 or properties/123).

run_report

run_report(property_id: str | int, dimensions: list[str] | None, metrics: list[str], date_ranges: list[tuple[str, str]] | None = None, dimension_filters: list[dict[str, Any]] | None = None, metric_filters: list[dict[str, Any]] | None = None, limit: int = 1000, offset: int = 0, order_by: str | None = None, descending: bool = True) -> Report

runReport. Dates are ISO days or "today"/"yesterday"/"NdaysAgo".

run_realtime_report

run_realtime_report(property_id: str | int, dimensions: list[str] | None, metrics: list[str], dimension_filters: list[dict[str, Any]] | None = None, limit: int = 100) -> Report

runRealtimeReport (last 30 minutes).

build_filter

build_filter(filters: list[dict[str, Any]] | None) -> dict[str, Any] | None

Simple filters ANDed together.

Each: {"field": "country", "value": "Argentina"} (EXACT), {"field": .., "operator": "CONTAINS", "value": ..}, {"field": .., "values": [..]} (in list), or a numeric {"operator": "GREATER_THAN", "value": 10}. "not": true negates.

property_name

property_name(property_id: str | int) -> str

"123", 123 or "properties/123" -> "properties/123".

AccountSummary dataclass

AccountSummary(name: str, account: str, display_name: str = '', properties: list[PropertySummary] = list(), raw: dict[str, Any] = dict())

An account with the properties the user can see in it.

PropertySummary dataclass

PropertySummary(name: str, display_name: str = '', property_type: str | None = None, parent: str | None = None, raw: dict[str, Any] = dict())

A property as listed in an account summary; name is "properties/123".

Property dataclass

Property(name: str, display_name: str = '', property_type: str | None = None, parent: str | None = None, time_zone: str | None = None, currency_code: str | None = None, industry_category: str | None = None, service_level: str | None = None, create_time: datetime | None = None, update_time: datetime | None = None, delete_time: datetime | None = None, raw: dict[str, Any] = dict())

A GA4 property ("properties/123").

Report dataclass

Report(dimensions: list[str], metrics: list[str], rows: list[ReportRow], row_count: int, totals: list[ReportRow] = list(), raw: dict[str, Any] = dict())

A runReport / runRealtimeReport response.

raw is the response without rows and totals (headers with their types, metadata, propertyQuota...).

ReportRow dataclass

ReportRow(dimensions: dict[str, str], metrics: dict[str, int | float | None])

One report row. Metric values are numbers (None if the API sent something else).