Sub topic 11
Checking for data loss with the Data API
Assume you know GA4 dimensions, metrics, events, and users. You should also know basic Python and how to authenticate a Google Cloud client. This chapter shows how to use the GA4 Data API to check whether a report lost detail because of thresholding or an (other) row.
What the API can tell you
A normal API response contains rows and values. It also contains metadata about how Google produced those rows. Check this metadata before treating the result as a complete breakdown.
| Metadata field | What it indicates | Main concern |
|---|---|---|
| `subject_to_thresholding` | Google withheld some data to protect user privacy | Small audience groups or sensitive dimensions may be incomplete |
| `data_loss_from_other_row` | Some dimension values were combined into an `(other)` row | A high-cardinality breakdown is incomplete |
| `sampling_metadatas` | The report used a sample of events | Metrics are estimates rather than full counts |
The API does not turn an incomplete report into a complete one. It helps you detect the problem programmatically, so your pipeline can label, reject, or replace the result.
Read the response metadata
The following example requests active users by page path and prints the two most important diagnostic flags.
from google.analytics.data_v1beta import BetaAnalyticsDataClient
from google.analytics.data_v1beta.types import (
DateRange,
Dimension,
Metric,
RunReportRequest,
)
client = BetaAnalyticsDataClient()
request = RunReportRequest(
property="properties/123456789",
dimensions=[Dimension(name="pagePath")],
metrics=[Metric(name="activeUsers")],
date_ranges=[
DateRange(start_date="2026-01-01", end_date="2026-01-31")
],
)
response = client.run_report(request)
print("Rows returned:", len(response.rows))
print(
"Data lost to (other) row:",
response.metadata.data_loss_from_other_row,
)
print(
"Subject to thresholding:",
response.metadata.subject_to_thresholding,
)
for sampling in response.metadata.sampling_metadatas:
print("Samples read:", sampling.samples_read_count)
print("Sampling space:", sampling采.samples_space_size)The last line contains an invalid identifier if copied as written because it includes a non-English character. Use this corrected loop instead:
for sampling in response.metadata.sampling_metadatas:
print("Samples read:", sampling.samples_read_count)
print("Sampling space:", sampling.samples_space_size)If sampling_metadatas is empty, the response does not report sampling metadata. If it contains values, compare the number of samples read with the sampling space size. A smaller sample count means the report estimated the result from part of the available data.
Turn flags into a reporting decision
A metadata flag does not always mean the entire report is unusable. It tells you which claims require caution.
- If thresholding is active, avoid publishing small audience segments as exact counts. Try a broader audience, a less sensitive dimension, or an aggregated metric.
- If data was lost to the
(other)row, remove unnecessary dimensions or split a very detailed request into smaller, filtered requests. - If sampling appears, shorten the date range or use a different reporting surface for an exact-data requirement.
- If the report must contain unsampled event-level data, use the BigQuery export when it is available.
The Data API is useful for automated checks, but it does not expose every limitation as a visible table row. A successful HTTP response means the request worked. It does not mean that every underlying value is present.
Exercise
Exercise
Your API response has 2,000 rows, data_loss_from_other_row is true, and subject_to_thresholding is false. What problem should you report, and what is a reasonable first change?
Show answer
Report that high-cardinality values were merged into an (other) row. Remove an unnecessary high-cardinality dimension or narrow the request with a filter.