Skip to content

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

feat(google-ads): run custom read-only GAQL reports

Rudy Celeklifd87ad9fab6e
diff --git a/tests/clis/google-ads-query.test.cjs b/tests/clis/google-ads-query.test.cjs
new file mode 100644
index 0000000..68cffd9
--- /dev/null
+++ b/tests/clis/google-ads-query.test.cjs
@@ -0,0 +1,38 @@
+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-ads.js')
+const rows = [{results:[{campaign:{id:'9007199254740993',name:'Owned'},metrics:{costMicros:'1500001'}}]}, {results:[{campaign:{id:'42'}}]}]
+function run(args, oracle = '') {
+ const code = `global.fetch = async (url, options) => {
+ const assert = require('node:assert/strict'); const body = JSON.parse(options.body);
+ assert.equal(url, 'https://googleads.googleapis.com/v24/customers/5556667777/googleAds:searchStream');
+ assert.equal(options.method, 'POST'); assert.equal(options.headers.Authorization, 'Bearer owned-token');
+ assert.equal(options.headers['developer-token'], 'owned-developer'); assert.equal(options.headers['login-customer-id'], '1234567890');
+ ${oracle}
+ return new Response(JSON.stringify(${JSON.stringify(rows)}), {status:200});
+ }; process.argv = ['node', ${JSON.stringify(cli)}, ...${JSON.stringify(args)}]; require(${JSON.stringify(cli)});`
+ return spawnSync(process.execPath, ['-e', code], {encoding:'utf8', timeout:10000,
+ env:{...process.env, GOOGLE_ADS_TOKEN:'owned-token', GOOGLE_ADS_DEVELOPER_TOKEN:'owned-developer', GOOGLE_ADS_CUSTOMER_ID:'5556667777', GOOGLE_ADS_LOGIN_CUSTOMER_ID:'123-456-7890'}})
+}
+function result(r) {assert.equal(r.status,0,r.stderr); return JSON.parse(r.stdout)}
+for(const query of ["SELECT campaign.id, segments.date, metrics.clicks FROM campaign WHERE segments.date BETWEEN '2026-01-01' AND '2026-01-03' LIMIT 20", "SELECT campaign.name\nFROM campaign WHERE campaign.name = 'Owned café' LIMIT 1"])
+ test('runs caller query verbatim and retains stream chunks and integer strings '+query.slice(0,30),()=>{
+ assert.deepEqual(result(run(['query','run','--query',query], `assert.deepEqual(body, {query:${JSON.stringify(query)}});`)), rows)
+ })
+test('query preview masks credentials without requesting account data',()=>{
+ const query='SELECT customer.id FROM customer LIMIT 1'
+ const preview=result(run(['query','run','--query',query,'--dry-run'], "throw new Error('unexpected fetch')"))
+ assert.equal(preview._dry_run,true);assert.equal(preview.headers.Authorization,'***');assert.equal(preview.headers['developer-token'],'***');assert.equal(preview.headers['login-customer-id'],'1234567890');assert.deepEqual(preview.body,{query})
+})
+for(const args of [['query','run'],['query','run','--query'],['query','run','--query',' '],['query','unknown','--query','SELECT customer.id FROM customer']])
+ test('invalid custom query fails before transport '+args.join(' '),()=>{
+ const r=run(args,"throw new Error('unexpected fetch')");assert.notEqual(r.status,0);assert.match(r.stderr,/query|subcommand/);assert.doesNotMatch(r.stderr,/unexpected fetch/)
+ })
+test('existing account request remains unchanged',()=>{
+ assert.deepEqual(result(run(['account','info'], "assert.deepEqual(body,{query:'SELECT customer.id, customer.descriptive_name FROM customer'});")),rows)
+})
+test('no-argument help remains successful and describes custom queries',()=>{
+ const help=result(run([],"throw new Error('unexpected fetch')"));assert.match(help.usage.query,/query.*run/)
+})
diff --git a/tools/clis/google-ads.js b/tools/clis/google-ads.js
index 2dd87b8..4dc8295 100755
--- a/tools/clis/google-ads.js
+++ b/tools/clis/google-ads.js
@@ -82,6 +82,12 @@ async function main() {
let result
switch (cmd) {
+ case 'query':
+ if (sub !== 'run') throw new Error('Unknown query subcommand. Use: run')
+ if (typeof args.query !== 'string' || !args.query.trim()) throw new Error('--query requires a non-empty GAQL query')
+ result = await gaql(args.query)
+ break
+
case 'account':
switch (sub) {
case 'info':
@@ -182,6 +188,7 @@ async function main() {
result = {
error: 'Unknown command',
usage: {
+ query: 'query run --query <GAQL> [--dry-run]',
account: 'account [info]',
campaigns: 'campaigns [list|performance|pause|enable] [--days 30] [--id <id>]',
adgroups: 'adgroups [performance] [--days 30] [--limit <n>]',
diff --git a/tools/integrations/google-ads.md b/tools/integrations/google-ads.md
index 890fa0b..a93d420 100644
--- a/tools/integrations/google-ads.md
+++ b/tools/integrations/google-ads.md
@@ -252,3 +252,28 @@ and sends `login-customer-id` on both report and mutation requests. Keep
`GOOGLE_ADS_CUSTOMER_ID` set to the target client account. Direct client access
does not require the manager variable. See
[Google Ads REST authorization](https://developers.google.com/google-ads/api/rest/auth).
+
+### Run custom reports with the CLI
+
+The built-in report commands cover common summaries. Use `query run` for the
+[analysis recipes above](#analysis-recipes), additional dimensions, and conversion
+actions without writing another client:
+
+```bash
+node tools/clis/google-ads.js query run --query \
+ "SELECT campaign.id, segments.date, metrics.clicks FROM campaign WHERE segments.date BETWEEN '2026-01-01' AND '2026-01-03' ORDER BY segments.date LIMIT 100"
+```
+
+The command forwards the supplied GAQL unchanged to the same authenticated
+`googleAds:searchStream` endpoint. Google validates resource and field
+compatibility; it does not call a mutation endpoint. `--dry-run` previews the
+request with credentials masked. Empty queries and unknown subcommands fail
+locally before transport.
+
+Select the fields you need, bound the result with `LIMIT`, and use explicit dates
+for reproducible reporting. Adding a segment changes the row grain: campaign
+plus date rows are not campaign totals. JSON output preserves every returned
+stream chunk and provider integer strings; do not coerce large IDs into JavaScript
+numbers. Cost fields in micros still require division by one million.
+
+See [Google's GAQL overview](https://developers.google.com/google-ads/api/docs/query/overview).