Marketing skills for Claude Code and AI agents. CRO, copywriting, SEO, analytics, and growth engineering.
JavaScript agent-skillsai-agentsai-seoclaudeclaude-codeclaude-code-pluginclaude-skillscodexcontent-marketingconversion-rate-optimizationcopywritingcursoremail-marketinggenerative-engine-optimizationgrowth-marketingmarketingmarketing-automationsaas-marketingseoskill-md
fix(dub): request country and device analytics with grouping
Rudy Celekli4c72f7d4ff4b
diff --git a/tests/clis/dub-breakdowns.test.cjs b/tests/clis/dub-breakdowns.test.cjsnew file mode 100644index 0000000..1539252--- /dev/null+++ b/tests/clis/dub-breakdowns.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/dub.js')+function run(args, oracle = '') {+ const code = `global.fetch = async (url, options) => {+ const assert = require('node:assert/strict');+ const parsed = new URL(url);+ const body = options.body ? JSON.parse(options.body) : null;+ ${oracle}+ return new Response(JSON.stringify({accepted:true, url, body}), {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, DUB_API_KEY: 'fixture-token' },+ })+}+function result(r) { assert.equal(r.status, 0, r.stderr); return JSON.parse(r.stdout) }++for (const [command, dimension] of [['country','countries'],['device','devices']]) {+ test(`${command} breakdown uses the documented analytics grouping`, () => {+ const p = result(run(['analytics',command,'--domain','links.example.com','--key','summer sale','--interval','30d'], `+ assert.equal(options.method, 'GET');+ assert.equal(parsed.pathname, '/analytics');+ assert.equal(parsed.searchParams.get('groupBy'), ${JSON.stringify(dimension)});+ assert.equal(parsed.searchParams.get('domain'), 'links.example.com');+ assert.equal(parsed.searchParams.get('key'), 'summer sale');+ assert.equal(parsed.searchParams.get('interval'), '30d');+ assert.equal(options.body, undefined);+ `))+ assert.equal(p.accepted, true)+ })+ test(`${command} preview preserves grouping and redacts credentials`, () => {+ const p = result(run(['analytics',command,'--dry-run'], "throw new Error('unexpected fetch')"))+ assert.equal(new URL(p.url).pathname, '/analytics')+ assert.equal(new URL(p.url).searchParams.get('groupBy'), dimension)+ assert.equal(p.headers.Authorization, '***')+ })+}+test('total analytics retain their original endpoint and interval', () => {+ result(run(['analytics','get','--interval','7d'], `+ assert.equal(parsed.pathname, '/analytics');+ assert.equal(parsed.searchParams.has('groupBy'), false);+ assert.equal(parsed.searchParams.get('interval'), '7d');+ `))+})diff --git a/tools/clis/dub.js b/tools/clis/dub.jsindex c69404f..2bd7de8 100755--- a/tools/clis/dub.js+++ b/tools/clis/dub.js@@ -125,14 +125,18 @@ async function main() {const params = new URLSearchParams()if (args.domain) params.set('domain', args.domain)if (args.key) params.set('key', args.key)- result = await api('GET', `/analytics/country?${params}`)+ if (args.interval) params.set('interval', args.interval)+ params.set('groupBy', 'countries')+ result = await api('GET', `/analytics?${params}`)break}case 'device': {const params = new URLSearchParams()if (args.domain) params.set('domain', args.domain)if (args.key) params.set('key', args.key)- result = await api('GET', `/analytics/device?${params}`)+ if (args.interval) params.set('interval', args.interval)+ params.set('groupBy', 'devices')+ result = await api('GET', `/analytics?${params}`)break}default:diff --git a/tools/integrations/dub-co.md b/tools/integrations/dub-co.mdindex 409f4ec..3b1fe7b 100644--- a/tools/integrations/dub-co.md+++ b/tools/integrations/dub-co.md@@ -53,13 +53,13 @@ GET https://api.dub.co/analytics?domain=link.example.com&key=summer-sale&interva### Get clicks by location```bash-GET https://api.dub.co/analytics/country?domain=link.example.com&key=summer-sale+GET https://api.dub.co/analytics?groupBy=countries&domain=link.example.com&key=summer-sale```### Get clicks by device```bash-GET https://api.dub.co/analytics/device?domain=link.example.com&key=summer-sale+GET https://api.dub.co/analytics?groupBy=devices&domain=link.example.com&key=summer-sale```### Update link@@ -90,6 +90,10 @@ POST https://api.dub.co/links/bulk]```+The country and device breakdowns use the `groupBy` query parameter on+[`GET /analytics`](https://dub.co/docs/api-reference/analytics/retrieve).+The CLI accepts `--interval` for each analytics subcommand.+## TypeScript SDK### Install