Sub topic 4
Pulling Query-Level Data with the Search Console API
Assume you already understand Search Console’s Performance report, Python with pandas, and basic API authentication. You also need a verified Search Console property and credentials with permission to read it.
The Search Console API lets you export the rows behind the Performance report. Each row can contain a query, page, country, device, date, or search appearance. This makes it possible to create repeatable analyses instead of relying on manual downloads.
Choose dimensions and metrics
A dimension is a field used to split results into rows. Common dimensions include:
datequerypagecountrydevicesearchAppearance
The API returns these metrics for each row:
clicks: visits from Google Searchimpressions: times a result was shownctr: clicks divided by impressionsposition: average position of the result
For an AI Overview analysis, start with date, query, and page. The date allows pre/post comparisons. The query helps identify exposed and comparison cohorts. The page lets you connect search performance to landing-page behavior and revenue later.
Send a query request
The request body specifies the date range, dimensions, and maximum number of rows. siteUrl must exactly identify the property. A domain property uses a value such as sc-domain:example.com. A URL-prefix property uses its full URL, such as https://www.example.com/.
from googleapiclient.discovery import build
# `creds` must be an authorized google.oauth2 credentials object
# with Search Console read access.
service = build("searchconsole", "v1", credentials=creds)
body = {
"startDate": "2026-01-01",
"endDate": "2026-03-31",
"dimensions": ["date", "query", "page"],
"rowLimit": 25_000,
"startRow": 0,
}
response = service.searchanalytics().query(
siteUrl="sc-domain:example.com",
body=body,
).execute()
rows = response.get("rows", [])
for row in rows[:3]:
print(row)A returned row has a keys array whose order matches the requested dimensions:
{
"keys": ["2026-01-15", "how to choose a laptop", "https://example.com/guide"],
"clicks": 42,
"impressions": 1200,
"ctr": 0.035,
"position": 4.8
}The API aggregates data at the dimensions you request. If you omit page, a query can have one row per date rather than one row per query-page combination.
Handle row limits and aggregation
The API does not guarantee that one request returns every possible row. Use pagination when the result set may exceed rowLimit. Increase startRow by the number of rows received until a page is shorter than the requested limit.
all_rows = []
start_row = 0
page_size = 25_000
while True:
body["startRow"] = start_row
body["rowLimit"] = page_size
page = service.searchanalytics().query(
siteUrl="sc-domain:example.com",
body=body,
).execute()
batch = page.get("rows", [])
all_rows.extend(batch)
if len(batch) < page_size:
break
start_row += len(batch)
print(f"Downloaded {len(all_rows)} rows")Search Console may omit low-volume or anonymized queries. Therefore, the exported rows are not necessarily a complete log of every search. Record the property, date range, dimensions, filters, and extraction time with each export so later analyses remain reproducible.
Prepare rows for analysis
Convert the keys array into named columns before grouping. Keep clicks and impressions as additive measures. Recalculate CTR after aggregation.
import pandas as pd
data = pd.DataFrame(all_rows)
key_columns = ["date", "query", "page"]
data[key_columns] = pd.DataFrame(
data["keys"].tolist(),
index=data.index,
)
data["date"] = pd.to_datetime(data["date"])
data["ctr_check"] = data["clicks"] / data["impressions"]
daily = (
data.groupby("date", as_index=False)[["clicks", "impressions"]]
.sum()
)
daily["ctr"] = daily["clicks"] / daily["impressions"]
print(daily)This dataset is now ready for the next step: assigning queries to exposed and comparison cohorts.
Exercise
Why should aggregated CTR be calculated from summed clicks and impressions instead of averaging the ctr column?
Show answer
CTR is a ratio. Summing clicks and dividing by summed impressions weights each row by its search volume, while a simple average gives tiny and large rows equal influence.