Sub topic 2
Grouping queries into exposed and comparison cohorts
This chapter assumes you know the Search Console Performance report (clicks, impressions, CTR, position), pandas groupby, basic SQL, and GA4 events. It uses rows from the Search Console API query you already ran.
Building the cohorts
An exposed query is one where a SERP tracker recorded an AI Overview during the window before your change. A comparison query has similar traffic and rankings but no AI Overview. Fix both lists before you look at outcomes.
import numpy as np
import pandas as pd
exposed_queries = {"best running shoes", "how to tie a tie"}
comparison_queries = {"trail running socks", "wool dress shirt"}
df = pd.DataFrame(rows)
df[["date", "query", "page"]] = pd.DataFrame(df["keys"].tolist(), index=df.index)
# Keep only the two cohorts, so unmatched queries cannot leak in.
df = df[df["query"].isin(exposed_queries | comparison_queries)].copy()
df["cohort"] = np.where(df["query"].isin(exposed_queries), "exposed", "comparison")
df["period"] = np.where(pd.to_datetime(df["date"]) >= "2026-02-01", "post", "pre")> Note: Do not change the lists after you see the results. Doing so is a common way to bias the estimate.
Reading CTR, impressions and position by cohort
Sum clicks and impressions first, then divide. Averaging the per-row CTR gives a query with 3 impressions the same weight as one with 30,000.
df["weighted_pos"] = df["position"] * df["impressions"]
summary = df.groupby(["cohort", "period"]).agg(
clicks=("clicks", "sum"),
impressions=("impressions", "sum"),
weighted_pos=("weighted_pos", "sum"),
)
summary["ctr"] = summary["clicks"] / summary["impressions"]
summary["avg_position"] = summary["weighted_pos"] / summary["impressions"]
print(summary[["ctr", "impressions", "avg_position"]])Measuring sessions and engagement in GA4
GA4 counts a session as engaged when it lasts longer than 10 seconds, has a key event, or has at least two page or screen views. Use the same landing pages as your cohorts, and compare engaged sessions per organic session, not raw totals.
Tying organic landing pages to revenue with BigQuery
This query joins organic session starts to purchases in the GA4 BigQuery export.
WITH sessions AS (
SELECT
user_pseudo_id,
(SELECT value.int_value FROM UNNEST(event_params) WHERE key = 'ga_session_id') AS session_id,
(SELECT value.string_value FROM UNNEST(event_params) WHERE key = 'page_location') AS landing_page,
(SELECT value.string_value FROM UNNEST(event_params) WHERE key = 'medium') AS medium
FROM `project.analytics_123456.events_*`
WHERE event_name = 'session_start'
AND _TABLE_SUFFIX BETWEEN '20260101' AND '20260331'
),
purchases AS (
SELECT
user_pseudo_id,
(SELECT value.int_value FROM UNNEST(event_params) WHERE key = 'ga_session_id') AS session_id,
ecommerce.purchase_revenue AS revenue
FROM `project.analytics_123456.events_*`
WHERE event_name = 'purchase'
AND _TABLE_SUFFIX BETWEEN '20260101' AND '20260331'
)
SELECT
s.landing_page,
COUNT(DISTINCT CONCAT(s.user_pseudo_id, '.', CAST(s.session_id AS STRING))) AS sessions,
SUM(p.revenue) AS revenue
FROM sessions AS s
LEFT JOIN purchases AS p
ON s.user_pseudo_id = p.user_pseudo_id AND s.session_id = p.session_id
WHERE s.medium = 'organic'
GROUP BY s.landing_page
ORDER BY revenue DESC;Exercise
Why sum clicks and impressions before dividing, rather than averaging the per-row CTR values?
Show answer
Averaging per-row CTR gives every query equal weight, so low-impression queries distort the rate. Summing first produces an impression-weighted CTR that reflects real traffic.