Sub topic 10
Comparing Before and After Against a Control Group
This chapter assumes you can pull Search Console query data into pandas and group it into exposed and comparison query cohorts, as covered earlier. You also need to be comfortable reading CTR (click-through rate, clicks divided by impressions) and basic regression output. It builds the comparison that turns those cohorts into an impact estimate.
Why a simple before/after comparison fails
Suppose CTR for queries that show an AI Overview fell from 4.0% in January to 3.0% in February. It is tempting to say the AI Overview cost you one percentage point. That conclusion assumes nothing else changed between the two months.
Many other things change at the same time:
- Seasonal demand shifts alter which queries people type and how likely they are to click.
- Your rankings move up or down.
- Google ships a core update.
- Your tracking or site templates change.
A before/after comparison on the exposed queries alone mixes all of these effects with the effect of the AI Overview. You need a second group that experienced the same background changes but was not exposed. That group is the control, which this tutorial calls the comparison cohort.
The difference-in-differences idea
Difference-in-differences (DiD) works in four steps:
- Measure the change in CTR for the exposed cohort, post minus pre.
- Measure the same change for the comparison cohort.
- Subtract the second change from the first.
The comparison cohort's change estimates what would have happened to the exposed cohort without the AI Overview. Anything that hit both groups equally cancels out.
| Cohort | CTR pre | CTR post | Change |
|---|---|---|---|
| Exposed | 4.0% | 3.0% | -1.0 points |
| Comparison | 5.0% | 4.6% | -0.4 points |
| Difference-in-differences | -0.6 points |
The comparison cohort fell by 0.4 points anyway, so only 0.6 points of the exposed drop is attributed to the AI Overview. The raw before/after number overstated the effect by about two thirds.
Computing the estimate
The block below is self-contained. It builds the four-cell summary with the numbers from the table, then applies the same subtraction used on real data. In your own analysis, summary comes from the groupby in the previous chapter.
import pandas as pd
index = pd.MultiIndex.from_product(
[["comparison", "exposed"], ["pre", "post"]], names=["cohort", "period"]
)
summary = pd.DataFrame(
{
"clicks": [50000, 46000, 40000, 30000],
"impressions": [1_000_000, 1_000_000, 1_000_000, 1_000_000],
},
index=index,
)
summary["ctr"] = summary["clicks"] / summary["impressions"]
ctr = summary["ctr"]
exposed_change = ctr.loc[("exposed", "post")] - ctr.loc[("exposed", "pre")]
comparison_change = ctr.loc[("comparison", "post")] - ctr.loc[("comparison", "pre")]
did = exposed_change - comparison_change
print(f"Exposed change: {exposed_change:+.4f}")
print(f"Comparison change: {comparison_change:+.4f}")
print(f"Difference-in-differences in CTR: {did:+.4f}")This prints a DiD of -0.0060, which is 0.6 percentage points.
Always compute CTR as total clicks divided by total impressions within each cell. Averaging per-query CTR values gives tiny queries the same weight as large ones. It also breaks on queries with zero impressions.
Report the effect in both forms. The absolute change is -0.6 points. The relative change is that figure divided by what the exposed cohort's CTR would have been without the AI Overview. That counterfactual is the pre CTR plus the comparison change, here 4.0% - 0.4% = 3.6%. The relative effect is -0.6 / 3.6, about -17%. Relative effects are easier for stakeholders to read, but they depend on the baseline you choose, so state it.
The assumptions behind the number
DiD only gives a causal estimate if some conditions hold. Check each one before you report a figure.
- Parallel trends. Without the AI Overview, both cohorts would have moved in parallel. You cannot test this directly, but you can check that the gap between the cohorts was stable before the change window.
- Exposure defined before the window. Assign cohorts using SERP tracking from before the change. If you assign them afterwards, based on which queries lost clicks, you build the answer into the design.
- No spillover. Comparison queries should not be affected by the treatment. If the AI Overview on one query changes how users search for related queries, the control is contaminated.
- Stable composition. The mix of queries inside each cohort should not change between periods. A new page that ranks for many new queries in only one cohort shifts the averages without any change in user behaviour.
Checking pre-trends
A quick diagnostic uses weekly data. Compute the weekly gap between the cohorts and fit a line through the pre-period weeks. A slope near zero supports parallel trends. A clear slope means the cohorts were already diverging.
The next block uses synthetic weekly data with a built-in effect of -0.006. It checks the pre-trend, then estimates the DiD with a regression, which also gives a confidence interval.
import numpy as np
import pandas as pd
import statsmodels.formula.api as smf
rng = np.random.default_rng(0)
rows = []
for exposed in (0, 1):
for week in range(16):
post = int(week >= 8)
ctr = (
0.05
- 0.010 * exposed
- 0.004 * post
- 0.006 * exposed * post
+ rng.normal(0, 0.002)
)
rows.append({"week": week, "exposed": exposed, "post": post, "ctr": ctr})
df = pd.DataFrame(rows)
# Pre-trend check: slope of the exposed-minus-comparison gap before week 8.
wide = df.pivot(index="week", columns="exposed", values="ctr")
gap = wide[1] - wide[0]
pre_gap = gap[gap.index < 8]
slope = np.polyfit(pre_gap.index, pre_gap.values, 1)[0]
print(f"Pre-period gap slope per week: {slope:+.5f}")
# Regression form of DiD: the exposed:post coefficient is the estimate.
model = smf.ols("ctr ~ exposed * post", data=df).fit(cov_type="HC1")
print(model.params["exposed:post"])
print(model.conf_int().loc["exposed:post"].values)The exposed:post coefficient equals the four-cell subtraction when the design is balanced. The regression adds a standard error, so you can say how precise the estimate is.
Weekly points from one site are autocorrelated, meaning neighbouring weeks resemble each other. The standard errors above are therefore too optimistic. Treat the interval as a lower bound on uncertainty. The later chapter on uncertainty covers better options, such as resampling by query.
Choosing the windows
The pre and post periods need thought.
- Length. Use at least four to eight weeks on each side, so day-of-week and week-to-week noise average out.
- Same calendar shape. Compare periods with the same number of weekdays and weekends. Search behaviour differs across the week.
- Buffer. AI Overview exposure usually rolls out gradually. Leave out a transition period around the change date, or your post window contains a mix of treated and untreated days.
- Year-over-year as a second control. If the site has strong seasonality, compare the same weeks one year earlier. This adds a further layer of control, but it can introduce confounders of its own, such as last year's ranking changes.
Common errors
| Error | Why it misleads | Fix |
|---|---|---|
| Choosing the comparison cohort by "queries that did not drop" | Selection on the outcome guarantees a flattering control | Define cohorts from SERP data before the window |
| Comparing cohorts with very different positions | Top-3 queries and page-two queries respond differently to any shock | Match cohorts on average position and query intent |
| Ignoring impressions changes | CTR can move because the denominator changed, not because clicks did | Report clicks and impressions next to CTR |
| Pooling branded and non-branded queries | Branded queries rarely show AI Overviews and click at much higher rates | Split them, or analyse non-branded queries alone |
| Reporting one number with no range | A point estimate hides how noisy the data was | Give the interval and the assumptions checked |
What the estimate means
The DiD figure is the average effect on exposed queries in your cohort, compared with the comparison cohort. It does not say how much traffic Google took from you overall. It also says nothing about queries that were never in either cohort. Treat it as an input to the revenue step. Multiply the CTR effect by exposed impressions to get clicks lost. Then connect those clicks to sessions and revenue per landing page, as the GA4 chapters describe.
Exercise
An exposed cohort's CTR falls from 6.0% to 5.0%. A comparison cohort's CTR falls from 7.0% to 6.5%. What is the difference-in-differences estimate in percentage points, and what is the relative effect against the exposed counterfactual CTR?
Show answer
The exposed change is -1.0 points and the comparison change is -0.5 points, so the DiD is -1.0 - (-0.5) = -0.5 points. The counterfactual exposed CTR is 6.0% - 0.5% = 5.5%, so the relative effect is -0.5 / 5.5, about -9%.
Exercise
Your team builds the exposed cohort from queries whose clicks fell the most after February 1, and uses everything else as the comparison cohort. Why does DiD on these cohorts give a misleading answer?
Show answer
The cohorts are selected on the outcome. Queries with the biggest click drops land in the exposed group whatever caused the drop, such as rank loss or random noise. The estimate then looks like a large AI Overview effect even if the AI Overview did nothing. Cohorts must be defined from exposure data, such as SERP tracking from before the window.
Exercise
The pre-period gap between exposed and comparison CTR shrinks by 0.1 points every week for eight weeks, then the AI Overview change happens. Which DiD assumption is in doubt, and what is one way to respond?
Show answer
Parallel trends is in doubt, because the cohorts were already converging before the change. One response is to rebuild the comparison cohort so it matches the exposed cohort on position and intent. Another is to add a trend term to the regression, and to report the estimate as less certain.