Skip to content

Marketing skills for Claude Code and AI agents. CRO, copywriting, SEO, analytics, and growth engineering.

feat(gsc): expose Search Analytics result pages

Rudy Celekli138e737c16d5
diff --git a/tests/clis/gsc-query-pagination.test.cjs b/tests/clis/gsc-query-pagination.test.cjs
new file mode 100644
index 0000000..d6f7445
--- /dev/null
+++ b/tests/clis/gsc-query-pagination.test.cjs
@@ -0,0 +1,47 @@
+const { test } = require('node:test')
+const assert = require('node:assert/strict')
+const { spawnSync } = require('node:child_process')
+const path = require('node:path')
+const cli = path.resolve(__dirname, '../../tools/clis/google-search-console.js')
+function run(args, oracle='') {
+ const fixture = `global.fetch=async(url,options)=>{
+ const assert=require('node:assert/strict');const body=options.body?JSON.parse(options.body):undefined;${oracle}
+ assert.equal(url,'https://searchconsole.googleapis.com/webmasters/v3/sites/sc-domain%3Aexample.com/searchAnalytics/query');
+ assert.equal(options.method,'POST');assert.equal(options.headers.Authorization,'Bearer fixture-token');
+ const index=body.startRow||0;
+ return new Response(JSON.stringify({rows:index>1?[]:[{keys:[index?'second':'first'],clicks:index?5:10,impressions:100,ctr:0.1,position:2}],responseAggregationType:'auto',request:body}),{status:200});
+ };process.argv=['node',${JSON.stringify(cli)},...${JSON.stringify(args)}];require(${JSON.stringify(cli)});`
+ return spawnSync(process.execPath,['-e',fixture],{encoding:'utf8',timeout:10000,env:{...process.env,GSC_ACCESS_TOKEN:'fixture-token'}})
+}
+function output(result){assert.equal(result.status,0,result.stderr);return JSON.parse(result.stdout)}
+const query=sub=>['search',sub,'--site-url','sc-domain:example.com','--start-date','2026-09-01','--end-date','2026-09-30']
+for(const [sub,dimension] of [['query','query'],['pages','page'],['countries','country']])test(`${sub} reads a later page with the same date range and dimension`,()=>{
+ const first=output(run([...query(sub),'--limit','1']))
+ const next=output(run([...query(sub),'--limit','1','--start-row','1']))
+ assert.equal(next.request.startRow,1);assert.deepEqual(next.request.dimensions,[dimension]);assert.equal(next.rows[0].keys[0],'second')
+ const {startRow,...request}=next.request;assert.deepEqual(request,first.request)
+})
+test('empty page preserves provider success and aggregation metadata',()=>{
+ const result=output(run([...query('query'),'--start-row','2']))
+ assert.deepEqual(result.rows,[]);assert.equal(result.responseAggregationType,'auto')
+})
+test('zero start row and maximum page size are visible in masked preview',()=>{
+ const result=output(run([...query('pages'),'--limit','25000','--start-row','0','--dry-run'],"throw new Error('unexpected request')"))
+ assert.equal(result.body.rowLimit,25000);assert.equal(result.body.startRow,0);assert.equal(result.headers.Authorization,'***')
+})
+test('default page retains row limit 100 and omits optional offset',()=>{
+ const request=output(run(query('query'))).request
+ assert.equal(request.rowLimit,100);assert.equal(Object.hasOwn(request,'startRow'),false)
+})
+for(const field of ['start-row','limit'])test(`invalid ${field} fails before a request`,()=>{
+ const bad=field==='limit'?['0','-1','25001','1.5','1x']:['-1','1.5','1x','9007199254740993']
+ for(const value of bad){const result=run([...query('query'),`--${field}`,value],"throw new Error('unexpected request')");assert.equal(result.status,1);assert.match(JSON.parse(result.stderr).error,new RegExp(`--${field}`))}
+})
+test('URL-prefix properties remain encoded as one path segment',()=>{
+ const result=output(run(['search','query','--site-url','https://example.com/','--start-row','10','--dry-run'],"throw new Error('unexpected request')"))
+ assert.equal(result.url,'https://searchconsole.googleapis.com/webmasters/v3/sites/https%3A%2F%2Fexample.com%2F/searchAnalytics/query');assert.equal(result.body.startRow,10)
+})
+test('inspection stays independent of search page flags',()=>{
+ const result=output(run(['inspect','url','--site-url','sc-domain:example.com','--url','https://example.com/page','--start-row','bad','--limit','bad','--dry-run'],"throw new Error('unexpected request')"))
+ assert.deepEqual(result.body,{inspectionUrl:'https://example.com/page',siteUrl:'sc-domain:example.com'})
+})
diff --git a/tools/clis/google-search-console.js b/tools/clis/google-search-console.js
index 23e0f2d..6c8cdfd 100755
--- a/tools/clis/google-search-console.js
+++ b/tools/clis/google-search-console.js
@@ -63,6 +63,15 @@ function getDefaultDates() {
}
}
+function pageInteger(name, minimum, maximum = Number.MAX_SAFE_INTEGER) {
+ const value = args[name]
+ const number = Number(value)
+ if (typeof value !== 'string' || !/^\d+$/.test(value) || !Number.isSafeInteger(number) || number < minimum || number > maximum) {
+ throw new Error(`--${name} must be an integer between ${minimum} and ${maximum}`)
+ }
+ return number
+}
+
async function main() {
let result
const siteUrl = args['site-url']
@@ -78,9 +87,11 @@ async function main() {
const body = {
startDate: args['start-date'] || defaults.startDate,
endDate: args['end-date'] || defaults.endDate,
- rowLimit: parseInt(args.limit || '100', 10),
+ rowLimit: args.limit === undefined ? 100 : pageInteger('limit', 1, 25000),
}
+ if (args['start-row'] !== undefined) body.startRow = pageInteger('start-row', 0)
+
switch (sub) {
case 'query':
body.dimensions = ['query']
@@ -148,9 +159,9 @@ async function main() {
result = {
error: 'Unknown command',
usage: {
- 'search query': 'search query --site-url <url> [--start-date <date>] [--end-date <date>] [--limit <n>]',
- 'search pages': 'search pages --site-url <url> [--start-date <date>] [--end-date <date>] [--limit <n>]',
- 'search countries': 'search countries --site-url <url> [--start-date <date>] [--end-date <date>] [--limit <n>]',
+ 'search query': 'search query --site-url <url> [--start-date <date>] [--end-date <date>] [--limit <1-25000>] [--start-row <n>]',
+ 'search pages': 'search pages --site-url <url> [--start-date <date>] [--end-date <date>] [--limit <1-25000>] [--start-row <n>]',
+ 'search countries': 'search countries --site-url <url> [--start-date <date>] [--end-date <date>] [--limit <1-25000>] [--start-row <n>]',
'inspect url': 'inspect url --site-url <url> --url <page-url>',
'sitemaps list': 'sitemaps list --site-url <url>',
'sitemaps submit': 'sitemaps submit --site-url <url> --sitemap-url <sitemap-url>',
diff --git a/tools/integrations/google-search-console.md b/tools/integrations/google-search-console.md
index ee8b63a..874bdc0 100644
--- a/tools/integrations/google-search-console.md
+++ b/tools/integrations/google-search-console.md
@@ -145,3 +145,37 @@ POST https://indexing.googleapis.com/v3/urlNotifications:publish
- seo-audit
- programmatic-seo
- analytics
+
+## CLI result pages
+
+The local CLI supports `--start-row` for all three Search Analytics commands:
+`search query`, `search pages`, and `search countries`. The offset is zero-based;
+`--limit` accepts 1–25,000 rows. The CLI retains its default page size of 100 and
+omits `startRow` unless you specify it.
+
+```bash
+# First page
+node tools/clis/google-search-console.js search query \
+ --site-url sc-domain:example.com \
+ --start-date 2026-09-01 --end-date 2026-09-30 --limit 1000
+
+# Next page, after receiving 1,000 rows
+node tools/clis/google-search-console.js search query \
+ --site-url sc-domain:example.com \
+ --start-date 2026-09-01 --end-date 2026-09-30 --limit 1000 --start-row 1000
+```
+
+Keep the same property, command, explicit dates, and page size while reading
+pages. Advance the offset by the number of rows actually returned. An offset
+past the available results produces a successful empty response; do not treat
+that as an API failure. `--dry-run` previews the page body with masked credentials
+without making a request. Invalid offsets and page sizes fail locally.
+
+Paging exposes additional available rows, not a complete search-data export.
+Google returns top rows subject to internal limits, ordered by descending clicks;
+rows tied on clicks have arbitrary order. The API provides no stable tie-breaker
+or snapshot guarantee, so do not claim an exactly-once or exhaustive export.
+Use explicit dates rather than recalculating the CLI's moving default range
+between pages. URL Inspection and sitemap commands are unaffected.
+
+Reference: [Search Analytics query and pagination contract](https://developers.google.com/webmaster-tools/v1/searchanalytics/query).