DEV Community

Ed Grows
Ed Grows

Posted on

Find pages where an AI answer took the clicks, with the Search Console API and Node.js

Short answer: pull query and page data for two equal 28-day windows from the Search Console API, join the rows, and flag the pairs where impressions and average position barely changed but CTR fell sharply. That pattern means the ranking held and something on the results page took the click. On many informational queries that something is now an AI Overview.

I wrote this because the Performance report makes the pattern hard to see. The site total hides it, and the UI only compares one dimension at a time. The script below takes a few minutes to set up and prints a short list of pages worth a closer look.

Why look for "same position, lower CTR"?

Because a ranking loss and a click loss need different fixes. If position fell, the page lost to other pages. If position held and CTR fell, the page still ranks but fewer people click it.

Google counts AI features in the same data. Its AI features documentation says sites appearing in AI Overviews and AI Mode are included in the overall Search Console traffic, under the "Web" search type. So the impression stays, and the click may not come. A Pew Research Center study of US browsing in March 2025 found people clicked a traditional result in 8% of visits to a results page with an AI summary, against 15% without one.

What do you need before running it?

  1. A Google Cloud project with the Google Search Console API enabled.
  2. A service account and its JSON key.
  3. The service account's email added as a user on your Search Console property (Settings, Users and permissions). Read access is enough.
  4. Node.js 18 or newer and npm i googleapis.

The API call that matters

The Search Analytics query reference has three details worth knowing before you write a loop:

Parameter What it does Why it matters here
rowLimit 1 to 25,000 rows, default 1,000 Ask for 25,000 or you will miss long-tail queries
startRow Zero-based offset Page through results until a short page comes back
dataState final by default, all adds fresh data Keep final for comparisons, fresh days are incomplete

Dates are in Pacific Time, so a window that ends "yesterday" in Europe may still be today in California. Ending the window three days ago avoids partial days.

The script

// ctr-drop.mjs : node ctr-drop.mjs sc-domain:example.com
import { google } from "googleapis";

const site = process.argv[2];
const auth = new google.auth.GoogleAuth({
  keyFile: "service-account.json",
  scopes: ["https://www.googleapis.com/auth/webmasters.readonly"],
});
const gsc = google.searchconsole({ version: "v1", auth });

const day = (offset) => {
  const d = new Date();
  d.setUTCDate(d.getUTCDate() - offset);
  return d.toISOString().slice(0, 10);
};

async function fetchAll(startDate, endDate) {
  const rows = [];
  for (let startRow = 0; ; startRow += 25000) {
    const { data } = await gsc.searchanalytics.query({
      siteUrl: site,
      requestBody: {
        startDate, endDate,
        dimensions: ["page", "query"],
        rowLimit: 25000, startRow, dataState: "final",
      },
    });
    rows.push(...(data.rows ?? []));
    if (!data.rows || data.rows.length < 25000) return rows;
  }
}

// two equal windows, ending three days ago
const recent = await fetchAll(day(30), day(3));
const before = await fetchAll(day(58), day(31));
const key = (r) => r.keys.join("\u0000");
const old = new Map(before.map((r) => [key(r), r]));

const flagged = recent
  .map((r) => ({ now: r, was: old.get(key(r)) }))
  .filter(({ now, was }) =>
    was &&
    was.impressions >= 200 &&                                  // enough data to mean something
    Math.abs(now.impressions / was.impressions - 1) <= 0.2 &&  // demand roughly flat
    Math.abs(now.position - was.position) <= 1 &&              // ranking held
    now.ctr <= was.ctr * 0.7                                    // CTR down 30% or more
  )
  .map(({ now, was }) => ({
    page: now.keys[0], query: now.keys[1],
    position: `${was.position.toFixed(1)} → ${now.position.toFixed(1)}`,
    ctr: `${(was.ctr * 100).toFixed(1)}% → ${(now.ctr * 100).toFixed(1)}%`,
    clicksLost: Math.round(was.clicks - now.clicks),
  }))
  .sort((a, b) => b.clicksLost - a.clicksLost);

console.table(flagged.slice(0, 25));
Enter fullscreen mode Exit fullscreen mode

The thresholds are a starting point, not a rule. A small site may need a lower impressions floor; a large one may want a stricter CTR cut.

What do you do with the list?

Search each flagged query yourself and look at what sits above your result. In a clean browser, from the country you target, note whether an AI Overview appears, which sites it cites, and whether you are one of them. One look is a sample: the same query can show a different answer later, so check on two or three days before deciding.

Then group the rows by page. A page that appears for five flagged queries is a better candidate for an update than one with a single row.

Common mistakes

  • Comparing windows of different length. Use equal windows with the same weekdays.
  • Using fresh data. dataState: "all" includes days still being processed and will show a fake drop at the end.
  • Reading the site total. Run the comparison per page and query, as above.
  • Forgetting pagination. Without startRow you only see the first page of rows.
  • Ignoring season. Run the same comparison for the same months last year before you blame AI answers.

FAQ

Does the API show AI Overview impressions separately?
No. Google includes AI Overviews and AI Mode in the overall Web data in Search Console, so this script infers the effect from position and CTR.

Can I use OAuth instead of a service account?
Yes. A service account is simply easier for a script that runs on a schedule.

How often should I run it?
Weekly is enough for most sites. Store the output so you can see whether a page keeps coming back.

Is there a tool that does this without code?
Several. I build one, citelity.com, which runs this comparison on Search Console data and also checks how six AI engines answer the queries. The script above gets you most of the way for free.

Top comments (0)