Measuring SEO with the Search Console API and SQLite
The Search Console interface answers one question at a time. It cannot tell you which queries entered the top 20 this week, which pages lost impressions after a redirect, or whether a page earns 180 impressions from one query or from forty.
The API can. It returns rows, and rows belong in a database.
This is the pipeline I use for a small site: one Python script, one SQLite file, four queries that answer most SEO questions. About 300 lines total, no warehouse, no dashboard service.
Why SQLite
A content site produces tens of thousands of rows, not billions. SQLite handles that in a single file, runs on any machine, and supports the one feature that matters here: window functions. You can compute week-over-week deltas without exporting anything.
The file also travels well. Backups are cp. Inspecting a weird result is sqlite3 data.db. No connection string, no credentials, no service to keep alive.
Pulling the data
The Search Console API returns a maximum of 25,000 rows per request. Anything larger needs pagination with startRow, and the loop must stop when a page returns fewer rows than the limit.
body = { "startDate": start.isoformat(), "endDate": end.isoformat(), "dimensions": ["date", "query", "page", "device", "country"], "rowLimit": 25000, "startRow": 0, "dataState": "final", } rows = [] while True: payload = request(path, property_uri, body) batch = payload.get("rows", []) rows.extend(normalize(batch)) if len(batch) < body["rowLimit"]: break body["startRow"] += len(batch) Two details that are easy to miss:
-
dataState: "final"skips incomplete days. Fresh data looks better than it is and changes later. - The API lags. Data for today is usually absent, and yesterday may still be partial. Pull a window that ends two days back and treat the last days as provisional.
The schema
Five dimensions, four metrics, one run id:
CREATE TABLE gsc_metrics ( run_id TEXT NOT NULL, data_date TEXT NOT NULL, query TEXT NOT NULL, page TEXT NOT NULL, device TEXT NOT NULL, country TEXT NOT NULL, clicks REAL NOT NULL, impressions REAL NOT NULL, ctr REAL NOT NULL, position REAL NOT NULL, PRIMARY KEY (data_date, query, page, device, country) ); CREATE INDEX idx_gsc_date ON gsc_metrics(data_date); CREATE INDEX idx_gsc_page ON gsc_metrics(page); The primary key does the idempotency work. Re-running a collection for the same day overwrites rows instead of duplicating them, so a failed run can be repeated without cleanup:
INSERT INTO gsc_metrics (...) VALUES (...) ON CONFLICT(data_date, query, page, device, country) DO UPDATE SET clicks = excluded.clicks, impressions = excluded.impressions, ctr = excluded.ctr, position = excluded.position, run_id = excluded.run_id; run_id stays because you will want to know which run last touched a day when a number looks wrong.
Four queries that earn their place
Striking distance: pages ranking 8 to 20 with real impressions are the cheapest wins, because Google already considers them relevant.
SELECT page, query, SUM(impressions) AS impressions, ROUND(AVG(position), 1) AS position FROM gsc_metrics WHERE data_date >= date('now', '-28 day') AND position BETWEEN 8 AND 20 GROUP BY page, query HAVING impressions >= 20 ORDER BY impressions DESC LIMIT 50; New queries: what appeared this week that did not exist two weeks ago. These show which pages are finding new intent.
WITH this_week AS ( SELECT DISTINCT query FROM gsc_metrics WHERE data_date >= date('now', '-7 day') ), before AS ( SELECT DISTINCT query FROM gsc_metrics WHERE data_date BETWEEN date('now', '-28 day') AND date('now', '-8 day') ) SELECT query FROM this_week WHERE query NOT IN (SELECT query FROM before); Page movers: compare two windows on impressions, not position, because an average position across queries hides real movement.
WITH recent AS ( SELECT page, SUM(impressions) AS imp FROM gsc_metrics WHERE data_date >= date('now', '-7 day') GROUP BY page ), prior AS ( SELECT page, SUM(impressions) AS imp FROM gsc_metrics WHERE data_date BETWEEN date('now', '-14 day') AND date('now', '-8 day') GROUP BY page ) SELECT r.page, p.imp AS prior, r.imp AS recent, r.imp - p.imp AS delta FROM recent r LEFT JOIN prior p USING (page) ORDER BY delta DESC; Brand split: separate branded queries from the rest. A rising average position driven by your own name is not progress.
SELECT CASE WHEN query LIKE '%yourbrand%' THEN 'brand' ELSE 'non-brand' END AS kind, SUM(impressions) AS impressions, SUM(clicks) AS clicks FROM gsc_metrics WHERE data_date >= date('now', '-28 day') GROUP BY kind; Status per source, not per run
A daily collector pulls from more than Search Console: Bing, CrUX, the site crawl, Lighthouse, analytics. If one source fails, the run should not pretend everything is fine, and it should not throw away the data it did collect.
Store a status row per source:
CREATE TABLE source_status ( run_id TEXT, source TEXT, status TEXT, data_date TEXT, records_count INTEGER, message TEXT ); Then a run can finish as partial. That word is useful. On my own pipeline the CRM export and CrUX are unconfigured, so those rows say blocked and unavailable, while Search Console, Bing and the crawl say ok. The run reports partial, and I know exactly which numbers are missing without opening a log.
What broke in practice
Redirected URLs keep reporting. After a 301, Search Console continues to attribute impressions to the old URL for days or weeks. A page-level report will show traffic "lost" on the old URL and nothing on the new one, even though the redirect works. Do not fix the redirect twice.
Slash variants split rows. /page and /page/ appear as different pages when the crawler sees both before the redirect settles. Canonical tags do not merge historical rows.
GSC position is not a rank tracker. It is an average across queries, devices and locations for the impressions you received. A page can show position 16 with six impressions and never appear in a rank check for the term you care about.
Zero clicks at position 60 is normal, because impressions at that depth produce almost no clicks. Watching CTR there is wasted attention; watch it only inside the top 20.
Where this pays off
Two things become visible that the UI hides: which pages are one push away from page one, and which queries appeared out of nowhere. On a small site that is most of the strategy. The rest is writing pages and earning links, and no query will do that for you.
If you want to see what the output looks like in practice, the audit I run on this data is described in the lead audit.
The definitions behind the terms, from impressions to position, are in the glossary.
原文:https://www.a8q9.cn/en/article/measuring-seo-with-the-search-console-api-and-sqlite-8bdc7a.html