Sub topic 7
Designing a daily table you can query
This chapter assumes you can pull Search Console data through the API, read basic SQL or pandas, and know what clicks, impressions, CTR and position mean. Here you design the table that stores that data so later chapters can aggregate it correctly.
Decide the grain first
The grain is what one row represents. For Search Console data, a practical grain is one row per date, query, page, country and device. A finer grain lets you roll up to any coarser one. You cannot go the other way.
The data you pull must match this grain. If you request only date and query, you cannot later ask which page earned the clicks. The collection code from the previous chapter requested date, query and page. This chapter adds country and device, which Search Console also supports as dimensions.
Store counts, not ratios
Store clicks and impressions as integers. Store position as the value the API returns, which is the average position for that row. Do not treat ctr as a stored fact. You can always compute it as clicks / impressions. A stored ratio tempts you to average it later, and an average of ratios is wrong when rows have different impression counts.
Position is different. You cannot rebuild it from other columns, so keep it. When you aggregate, weight it by impressions, as the next chapter explains. Many people also store position * impressions as a helper column so that sums are easy.
A table definition
-- PostgreSQL syntax
CREATE TABLE gsc_daily (
date date NOT NULL,
query text NOT NULL,
page text NOT NULL,
country text NOT NULL,
device text NOT NULL,
clicks integer NOT NULL CHECK (clicks >= 0),
impressions integer NOT NULL CHECK (impressions >= 0),
position real NOT NULL,
PRIMARY KEY (date, query, page, country, device)
);
CREATE INDEX gsc_daily_page_idx ON gsc_daily (page, date);
CREATE INDEX gsc_daily_query_idx ON gsc_daily (query, date);The primary key enforces the grain. Loading the same day twice then fails instead of silently doubling your numbers. To make reloads safe, use an upsert (INSERT ... ON CONFLICT (...) DO UPDATE). Reloads are useful because Search Console revises recent days.
Common errors
- Mixed grains in one table. Rows with
queryand rows without it cannot be summed together. Keep a separate table for each grain. - Summing across dimensions that overlap. Clicks summed over
deviceare fine. But if you pull a query-level report and a page-level report separately, their totals can differ, because Search Console drops some rare queries for privacy. Totals from the page-level pull are more complete. - Storing dates as text. Use a real date type so
DATE_TRUNCand window functions work. - Averaging stored
positionwithAVG. This weights a day with 3 impressions the same as a day with 3,000.
Add a segment column
Brand versus non-brand is a question you will ask often. Rather than repeat pattern matching in every query, add a view that labels each row.
CREATE VIEW gsc_daily_seg AS
SELECT
*,
CASE WHEN query ~* '(example|examplecorp)' THEN 'brand' ELSE 'non-brand' END AS segment
FROM gsc_daily;Replace the pattern with your own brand names and common misspellings. Keeping the rule in one view means a correction applies everywhere.
Exercise
Why should the table store clicks and impressions but not rely on a stored CTR column when you roll up to weekly totals?
Show answer
CTR is a ratio, so averaging daily CTR values gives each day equal weight regardless of its impressions. Summing clicks and impressions first, then dividing, gives the correct weekly CTR. The ratio can always be recomputed from the two counts.