ga4_run_pivot_report
Google Analytics 4 read low risk
Part of the GA4 Analytics Agent.
What it does
Run a GA4 pivot report (properties.runPivotReport) that arranges metrics across one or more pivot axes for cross-tab analysis — e.g. keyEvents and sessions pivoted by sessionDefaultChannelGroup across date, or by deviceCategory across country. Returns pivot headers, aggregates, and rows of dimension/metric values. ACCURACY: same metric semantics as ga4_run_report — keyEvents is canonical (conversions is the deprecated alias), revenue is a decimal string in property currency (do NOT divide by 1e6), rates are 0..1 fractions. Each pivot has its OWN limit and offset, so high-cardinality pivots truncate independently and totals can exceed the visible cells; rely on the returned 'aggregates' for true per-pivot totals rather than summing rows. CAP: a pivot report is bounded by the PRODUCT of every pivot's limit (this is product-of-limits, NOT a single-request row count like runReport's row cap, so the semantics differ even where the numbers look similar). The current Pivot reference doc states this product must not exceed 250,000; NOTE the older pivots guide page still states 100,000, so the exact ceiling is in flux — do not hard-code it. Every dimension referenced in any pivot's fieldNames MUST also appear in the request's top-level dimensions list, otherwise that dimension is invisible / the request is rejected.
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
property_id | string | yes | GA4 property numeric id; normalize to 'properties/{id}'. |
dimensions | array<string> | yes | All dimensions used anywhere in pivots, plus filters. The union of every pivot's fieldNames must be a subset of this list. Max 9 total. |
metrics | array<string> | yes | Metric API names; prefer keyEvents over the deprecated conversions alias. At least 1 required. |
pivots | array<object> | yes | 1+ pivot specs: {fieldNames:[dim...], limit, offset, orderBys, metricAggregations}. Each pivot's limit caps that axis; the PRODUCT of all pivot limits is bounded by the API (current Pivot reference says 250,000; older pivots guide says 100,000). Don't hard-cap client-side — the API enforces and returns INVALID_ARGUMENT if exceeded. |
date_ranges | array<object> | yes | 1+ {startDate,endDate} (YYYY-MM-DD or relative). Two ranges => prior-period comparison; output rows carry a dateRange index. Max 4. |
dimension_filter | object | no | FilterExpression on dimensions; enum-normalize values. |
metric_filter | object | no | FilterExpression on metrics. |
currency_code | string | no | ISO-4217 override for revenue; does NOT convert to micros. |
keep_empty_rows | boolean | no | Return all-zero rows when true. Default false. |
Safeguards
Policy
Pivot reports run only against the user's authorized GA4 property under the analytics.readonly scope with per-tenant isolation; use keyEvents (not the deprecated conversions alias) and never present GA4 pivot cells alongside Google Ads numbers without labeling the source platform.
Reminder
Pivot cells are the user's own GA4 property data — label the platform, don't blend with Ads, and don't retain beyond the request.
Errors
| Error class | Means | What to do | Retry |
|---|---|---|---|
PROPERTY_NOT_FOUND | GA4 property {property_id} not found or inaccessible. | Verify property id and user access. | no |
PERMISSION_DENIED | Connected account lacks read access to property {property_id} (HTTP 403). | Grant Viewer or re-authorize the analytics.readonly scope. | no |
TOKEN_REVOKED | Analytics OAuth token invalid or revoked (HTTP 401). | needs_reauth: prompt reconnect. | no |
INVALID_ARGUMENT | GA4 rejected the pivot request (HTTP 400): {surfaced message} — commonly a pivot fieldName missing from the dimensions list, >9 dimensions, or the product of pivot limits exceeding the API maximum (250,000 per the current Pivot reference; 100,000 per the older guide). | Surface the GA4 message verbatim; ensure every pivot fieldName is in dimensions, total dimensions <=9, and lower pivot limits if the product was rejected. Do not guess names — validate with ga4_get_metadata. | no |
INCOMPATIBLE_FIELDS | Pivot dimensions/metrics are not compatible together. | Run ga4_check_compatibility and drop the incompatible field. | no |
RESOURCE_EXHAUSTED | GA4 Data API quota exhausted (HTTP 429) — pivot reports cost more tokens per query. | Back off 5s/10s/20s (cap 60s), retry once; reduce pivot count/limits to lower token cost. | yes |
PLATFORM_UNAVAILABLE | GA4 Data API transient server error (HTTP 500/503). | Retry once with backoff; report if persistent. | yes |
Edge cases
- No data: success with count:0 and empty pivot rows.
- Pivot fieldName not present in the dimensions list: deterministic 400 — validate and fix before sending.
- Product of pivot limits exceeds the API maximum: do NOT hard-fail client-side (the documented cap is in flux: 250,000 per the current Pivot reference vs 100,000 per the older guide) — pass the request through and surface the API's INVALID_ARGUMENT verbatim. Optionally emit a soft pre-flight warning when the product exceeds the conservative 100,000 bound so the user gets an actionable heads-up before the API rejects.
- Per-pivot limit truncates that axis independently; use returned 'aggregates' for true totals instead of summing visible cells.
- High-cardinality pivot yields an '(other)' bucket per axis — flag incompleteness.
- Pivot reports are token-expensive; near-cap requests can hit RESOURCE_EXHAUSTED sooner than runReport.
- Caller passes deprecated conversions metric — use keyEvents instead (Google accepts a deprecated name only while getMetadata lists it in deprecatedApiNames).
- Two dateRanges: pivot rows carry a dateRange index for prior-period comparison.
- Expired token: refresh once and retry transparently.