Sub topic 5
Collecting data on a schedule and keeping history
This chapter assumes you know what clicks, impressions, CTR and position mean, and that you can read basic Python. You also need access to a Search Console property and a way to authorize API calls.
Why collect your own copy
The Search Console interface shows a limited window of data, roughly the last 16 months. Anything older is gone. If you want year-over-year comparisons or a record of how a page ranked before a redesign, you must copy the data into a table you own while it is still available.
A scheduled job that runs every day and stores the result gives you that history. It also lets you query at a finer grain than the interface allows.
The collection loop
The Search Analytics query method returns at most 25,000 rows per request. To get everything, request a page of rows, then move startRow forward until a response comes back empty.
# `service` is an authorized Search Console API client, for example built with
# googleapiclient.discovery.build("searchconsole", "v1", credentials=creds)
site = "sc-domain:example.com"
rows_all = []
start_row = 0
while True:
body = {
"startDate": "2026-01-01",
"endDate": "2026-01-31",
"dimensions": ["date", "query", "page"],
"rowLimit": 25000,
"startRow": start_row,
}
response = service.searchanalytics().query(siteUrl=site, body=body).execute()
rows = response.get("rows", [])
if not rows:
break
for row in rows:
date, query, page = row["keys"]
rows_all.append(
(date, query, page, row["clicks"], row["impressions"], row["ctr"], row["position"])
)
start_row += len(rows)Each row is one combination of date, query and page. That combination is the grain of your table, which the data model chapter covers in detail.
Scheduling the job
Run the job once a day with a scheduler such as cron, a CI pipeline, or a cloud scheduler. Do not request today's date. Search Console data arrives with a delay of a day or two, and recent days can be revised. A safe pattern is to request a rolling window of the last 3 to 5 days that ends two days ago.
Making the job safe to rerun
A job that fails halfway, or runs twice, should not damage your history. Make the write step idempotent, meaning that running it again with the same input leaves the table in the same state.
- Define a unique key, for example
(date, query, page). - Write with an upsert, or delete the dates you are about to load and insert them again in one transaction.
- Record each run with its date range, row count and status.
The delete-and-reload approach is also how you pick up revisions to recent days.
Common errors
- Missing rows from one request. Forgetting to page through
startRowsilently truncates results at 25,000 rows. - Gaps in history. If the job fails for a week, you lose nothing as long as you rerun within the retention window. Check the run log for missing dates and backfill them.
- Duplicates. These come from overlapping windows combined with plain inserts.
- Wrong property. A domain property (
sc-domain:) and a URL-prefix property return different totals. Pick one and keep using it. - Storing only summaries. Daily rows can be aggregated later, but weekly summaries cannot be split back into days.
Backfilling
On the first run, load as much history as the API gives you, month by month, using the same loop. Then switch to the daily window. Store the raw rows unchanged and do calculations such as averaged position in queries, not at load time.
Exercise
Your job runs daily and requests only yesterday's date. Two weeks later you notice that totals for recent days in your table are lower than in the Search Console interface. What is the likely cause, and how do you fix the job?
Show answer
Data for the most recent days is incomplete when first collected and is revised later, and the job never reloads those days. Request a rolling window of the last 3 to 5 days ending two days ago, and replace those dates on each run (delete and reload in one transaction, or upsert on the unique key) so revised values overwrite the early ones. Then backfill the days that are already too low.