seo-supervisor 1.2.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude-plugin/icon.png +0 -0
- package/.claude-plugin/marketplace.json +2 -2
- package/.claude-plugin/plugin.json +16 -3
- package/CHANGELOG.md +430 -0
- package/NOTICE +7 -0
- package/README.md +230 -25
- package/agents/seo-auditor.md +16 -0
- package/agents/seo-reviewer.md +16 -0
- package/bin/agents.mjs +194 -29
- package/bin/extras.mjs +422 -0
- package/bin/prompt.mjs +34 -14
- package/bin/seo-supervisor.mjs +386 -53
- package/commands/seo-audit.md +29 -3
- package/commands/seo-brief.md +28 -0
- package/commands/seo-connect.md +27 -0
- package/commands/seo-fix.md +24 -3
- package/commands/seo-grow.md +26 -0
- package/commands/seo-launch.md +17 -3
- package/commands/seo-review.md +13 -1
- package/commands/seo-rewrite.md +25 -0
- package/commands/seo-setup.md +17 -1
- package/commands/seo-watch.md +28 -0
- package/hooks/hooks.json +117 -0
- package/package.json +20 -7
- package/skills/seo-supervisor/NOTICE +7 -0
- package/skills/seo-supervisor/SKILL.md +302 -21
- package/skills/seo-supervisor/audience.md +61 -0
- package/skills/seo-supervisor/examples/drafts/security.md +41 -0
- package/skills/seo-supervisor/examples/hooks/README.md +28 -0
- package/skills/seo-supervisor/examples/hooks/claude-code-settings.json +15 -0
- package/skills/seo-supervisor/examples/hooks/codex-hooks.json +17 -0
- package/skills/seo-supervisor/examples/hooks/copilot-hooks.json +15 -0
- package/skills/seo-supervisor/examples/hooks/cursor-hooks.json +11 -0
- package/skills/seo-supervisor/examples/hooks/github-actions.yml +21 -0
- package/skills/seo-supervisor/examples/hooks/pre-commit +17 -0
- package/skills/seo-supervisor/examples/hooks/reading-ease.yml +24 -0
- package/skills/seo-supervisor/examples/sample-audit.json +94 -2
- package/skills/seo-supervisor/examples/sample-report.html +111 -61
- package/skills/seo-supervisor/examples/sample-report.md +147 -44
- package/skills/seo-supervisor/examples/scheduled-runs.md +89 -0
- package/skills/seo-supervisor/examples/snippets.md +648 -1
- package/skills/seo-supervisor/grow.md +342 -0
- package/skills/seo-supervisor/og-images.md +85 -0
- package/skills/seo-supervisor/page-types.md +124 -0
- package/skills/seo-supervisor/planning.md +19 -7
- package/skills/seo-supervisor/profile-template.md +41 -0
- package/skills/seo-supervisor/report-format.md +177 -10
- package/skills/seo-supervisor/rewrite.md +59 -0
- package/skills/seo-supervisor/rules/ai-search.md +65 -12
- package/skills/seo-supervisor/rules/content.md +33 -9
- package/skills/seo-supervisor/rules/crawl-index.md +33 -6
- package/skills/seo-supervisor/rules/internal-linking.md +49 -6
- package/skills/seo-supervisor/rules/launch-monitoring.md +17 -8
- package/skills/seo-supervisor/rules/media.md +13 -1
- package/skills/seo-supervisor/rules/metadata.md +41 -9
- package/skills/seo-supervisor/rules/performance.md +50 -5
- package/skills/seo-supervisor/rules/rendering.md +39 -2
- package/skills/seo-supervisor/rules/search-intent.md +32 -0
- package/skills/seo-supervisor/rules/sitemaps-robots.md +24 -12
- package/skills/seo-supervisor/rules/structured-data.md +37 -14
- package/skills/seo-supervisor/rules/trust-security.md +25 -2
- package/skills/seo-supervisor/rules/urls.md +10 -2
- package/skills/seo-supervisor/scripts/ai-answers.mjs +314 -0
- package/skills/seo-supervisor/scripts/backlinks.mjs +412 -0
- package/skills/seo-supervisor/scripts/change-check.mjs +281 -0
- package/skills/seo-supervisor/scripts/checks/agents.mjs +488 -0
- package/skills/seo-supervisor/scripts/checks/assets.mjs +404 -0
- package/skills/seo-supervisor/scripts/checks/bots.mjs +146 -0
- package/skills/seo-supervisor/scripts/checks/business-type.mjs +193 -0
- package/skills/seo-supervisor/scripts/checks/content.mjs +391 -0
- package/skills/seo-supervisor/scripts/checks/coverage.mjs +184 -0
- package/skills/seo-supervisor/scripts/checks/crawl.mjs +95 -0
- package/skills/seo-supervisor/scripts/checks/hreflang.mjs +396 -0
- package/skills/seo-supervisor/scripts/checks/hygiene.mjs +227 -0
- package/skills/seo-supervisor/scripts/checks/lighthouse.mjs +271 -0
- package/skills/seo-supervisor/scripts/checks/link-strength.mjs +59 -0
- package/skills/seo-supervisor/scripts/checks/links.mjs +360 -0
- package/skills/seo-supervisor/scripts/checks/local.mjs +101 -0
- package/skills/seo-supervisor/scripts/checks/media.mjs +194 -0
- package/skills/seo-supervisor/scripts/checks/outbound.mjs +53 -0
- package/skills/seo-supervisor/scripts/checks/page.mjs +223 -0
- package/skills/seo-supervisor/scripts/checks/probes.mjs +130 -0
- package/skills/seo-supervisor/scripts/checks/render.mjs +552 -0
- package/skills/seo-supervisor/scripts/checks/robots.mjs +144 -0
- package/skills/seo-supervisor/scripts/checks/schema.mjs +206 -0
- package/skills/seo-supervisor/scripts/checks/site-agents.mjs +79 -0
- package/skills/seo-supervisor/scripts/checks/site-content.mjs +135 -0
- package/skills/seo-supervisor/scripts/checks/site-hygiene.mjs +202 -0
- package/skills/seo-supervisor/scripts/checks/site-pages.mjs +474 -0
- package/skills/seo-supervisor/scripts/checks/site-security.mjs +102 -0
- package/skills/seo-supervisor/scripts/checks/sitemap.mjs +473 -0
- package/skills/seo-supervisor/scripts/checks/speed.mjs +267 -0
- package/skills/seo-supervisor/scripts/checks/technical.mjs +368 -0
- package/skills/seo-supervisor/scripts/checks/tls.mjs +297 -0
- package/skills/seo-supervisor/scripts/checks/trust.mjs +421 -0
- package/skills/seo-supervisor/scripts/checks/visual.mjs +1132 -0
- package/skills/seo-supervisor/scripts/checks/writing.mjs +192 -0
- package/skills/seo-supervisor/scripts/commoncrawl.mjs +452 -0
- package/skills/seo-supervisor/scripts/connect-keys.mjs +422 -0
- package/skills/seo-supervisor/scripts/content-scan.mjs +515 -158
- package/skills/seo-supervisor/scripts/crawl/catalog.mjs +1248 -0
- package/skills/seo-supervisor/scripts/crawl/fetch.mjs +362 -0
- package/skills/seo-supervisor/scripts/crawl/page.mjs +741 -0
- package/skills/seo-supervisor/scripts/crawl/queue.mjs +190 -0
- package/skills/seo-supervisor/scripts/dashboard.mjs +140 -0
- package/skills/seo-supervisor/scripts/data/ai-bots.json +38 -0
- package/skills/seo-supervisor/scripts/data/build-schema-vocab.mjs +57 -0
- package/skills/seo-supervisor/scripts/data/business-types.json +338 -0
- package/skills/seo-supervisor/scripts/data/google-rich-results.json +79 -0
- package/skills/seo-supervisor/scripts/data/schema-vocab.json +1 -0
- package/skills/seo-supervisor/scripts/deploy-check.mjs +462 -0
- package/skills/seo-supervisor/scripts/disavow.mjs +176 -0
- package/skills/seo-supervisor/scripts/doctor.mjs +174 -0
- package/skills/seo-supervisor/scripts/geo-grid.mjs +371 -0
- package/skills/seo-supervisor/scripts/google/search-console.mjs +151 -0
- package/skills/seo-supervisor/scripts/google/session.mjs +167 -0
- package/skills/seo-supervisor/scripts/google-updates.mjs +189 -0
- package/skills/seo-supervisor/scripts/google.mjs +1341 -0
- package/skills/seo-supervisor/scripts/gsc-import.mjs +298 -0
- package/skills/seo-supervisor/scripts/guard/adapters/claude-code.mjs +149 -0
- package/skills/seo-supervisor/scripts/guard/adapters/cline.mjs +28 -0
- package/skills/seo-supervisor/scripts/guard/adapters/codex.mjs +26 -0
- package/skills/seo-supervisor/scripts/guard/adapters/copilot.mjs +36 -0
- package/skills/seo-supervisor/scripts/guard/adapters/cursor.mjs +33 -0
- package/skills/seo-supervisor/scripts/guard/adapters/devin.mjs +25 -0
- package/skills/seo-supervisor/scripts/guard/adapters/edits.mjs +124 -0
- package/skills/seo-supervisor/scripts/guard/mode.mjs +37 -0
- package/skills/seo-supervisor/scripts/guard/rules.mjs +931 -0
- package/skills/seo-supervisor/scripts/guard.mjs +74 -0
- package/skills/seo-supervisor/scripts/images.mjs +408 -0
- package/skills/seo-supervisor/scripts/indexnow.mjs +300 -0
- package/skills/seo-supervisor/scripts/issues.mjs +217 -0
- package/skills/seo-supervisor/scripts/keys/authority.mjs +135 -0
- package/skills/seo-supervisor/scripts/keys/bing.mjs +239 -0
- package/skills/seo-supervisor/scripts/keys/clarity.mjs +137 -0
- package/skills/seo-supervisor/scripts/keys/dataforseo.mjs +57 -0
- package/skills/seo-supervisor/scripts/keys/googleplaces.mjs +26 -0
- package/skills/seo-supervisor/scripts/keys/http.mjs +212 -0
- package/skills/seo-supervisor/scripts/keys/matomo.mjs +138 -0
- package/skills/seo-supervisor/scripts/keys/openai.mjs +26 -0
- package/skills/seo-supervisor/scripts/keys/perplexity.mjs +26 -0
- package/skills/seo-supervisor/scripts/keys/posthog.mjs +165 -0
- package/skills/seo-supervisor/scripts/keys/semrush.mjs +25 -0
- package/skills/seo-supervisor/scripts/keys/services.mjs +41 -0
- package/skills/seo-supervisor/scripts/keys/store.mjs +140 -0
- package/skills/seo-supervisor/scripts/keys/visits.mjs +58 -0
- package/skills/seo-supervisor/scripts/keys/youtube.mjs +84 -0
- package/skills/seo-supervisor/scripts/keyword-data.mjs +316 -0
- package/skills/seo-supervisor/scripts/keyword-ideas.mjs +503 -0
- package/skills/seo-supervisor/scripts/lib/args.mjs +56 -0
- package/skills/seo-supervisor/scripts/lib/business.mjs +27 -0
- package/skills/seo-supervisor/scripts/lib/cdp.mjs +242 -0
- package/skills/seo-supervisor/scripts/lib/cli.mjs +56 -0
- package/skills/seo-supervisor/scripts/lib/csv.mjs +38 -0
- package/skills/seo-supervisor/scripts/lib/findings.mjs +100 -0
- package/skills/seo-supervisor/scripts/lib/google-client.mjs +9 -0
- package/skills/seo-supervisor/scripts/lib/html.mjs +146 -0
- package/skills/seo-supervisor/scripts/lib/http.mjs +28 -0
- package/skills/seo-supervisor/scripts/lib/image-size.mjs +78 -0
- package/skills/seo-supervisor/scripts/lib/is-main.mjs +13 -0
- package/skills/seo-supervisor/scripts/lib/net.mjs +116 -0
- package/skills/seo-supervisor/scripts/lib/proxy.mjs +47 -0
- package/skills/seo-supervisor/scripts/lib/recheck.mjs +106 -0
- package/skills/seo-supervisor/scripts/lib/redact.mjs +78 -0
- package/skills/seo-supervisor/scripts/lib/robots.mjs +73 -0
- package/skills/seo-supervisor/scripts/lib/score.mjs +266 -0
- package/skills/seo-supervisor/scripts/lib/settings-dir.mjs +81 -0
- package/skills/seo-supervisor/scripts/links-import.mjs +603 -0
- package/skills/seo-supervisor/scripts/log-scan.mjs +597 -0
- package/skills/seo-supervisor/scripts/og-card.mjs +273 -0
- package/skills/seo-supervisor/scripts/osm.mjs +359 -0
- package/skills/seo-supervisor/scripts/rdap.mjs +164 -0
- package/skills/seo-supervisor/scripts/render-report.mjs +324 -690
- package/skills/seo-supervisor/scripts/report/client.js +37 -0
- package/skills/seo-supervisor/scripts/report/diff.mjs +264 -0
- package/skills/seo-supervisor/scripts/report/findings.mjs +165 -0
- package/skills/seo-supervisor/scripts/report/format.mjs +111 -0
- package/skills/seo-supervisor/scripts/report/markdown.mjs +250 -0
- package/skills/seo-supervisor/scripts/report/model.mjs +171 -0
- package/skills/seo-supervisor/scripts/report/overview-lines.mjs +158 -0
- package/skills/seo-supervisor/scripts/report/overview.mjs +224 -0
- package/skills/seo-supervisor/scripts/report/page.mjs +127 -0
- package/skills/seo-supervisor/scripts/report/panel.mjs +172 -0
- package/skills/seo-supervisor/scripts/report/panels/ai.mjs +211 -0
- package/skills/seo-supervisor/scripts/report/panels/connections.mjs +139 -0
- package/skills/seo-supervisor/scripts/report/panels/content.mjs +204 -0
- package/skills/seo-supervisor/scripts/report/panels/data-search.mjs +379 -0
- package/skills/seo-supervisor/scripts/report/panels/data-site.mjs +44 -0
- package/skills/seo-supervisor/scripts/report/panels/links.mjs +380 -0
- package/skills/seo-supervisor/scripts/report/panels/local.mjs +221 -0
- package/skills/seo-supervisor/scripts/report/panels/site.mjs +359 -0
- package/skills/seo-supervisor/scripts/report/panels/speed-media.mjs +267 -0
- package/skills/seo-supervisor/scripts/report/sources.mjs +163 -0
- package/skills/seo-supervisor/scripts/report/styles.css +223 -0
- package/skills/seo-supervisor/scripts/report/traffic.mjs +264 -0
- package/skills/seo-supervisor/scripts/report-plan.mjs +112 -33
- package/skills/seo-supervisor/scripts/schema-check.mjs +717 -0
- package/skills/seo-supervisor/scripts/seo-crawl.mjs +1086 -638
- package/skills/seo-supervisor/scripts/seo-diff.mjs +584 -0
- package/skills/seo-supervisor/scripts/update-check.mjs +82 -0
- package/skills/seo-supervisor/site-types/content.md +34 -1
- package/skills/seo-supervisor/site-types/ecommerce.md +38 -1
- package/skills/seo-supervisor/site-types/local-business.md +96 -6
- package/skills/seo-supervisor/site-types/services.md +46 -0
- package/skills/seo-supervisor/site-types/tools.md +3 -3
- package/skills/seo-supervisor/strategy.md +232 -0
- package/skills/seo-supervisor/troubleshooting.md +56 -0
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
// Shared by connect-keys.mjs's services: errors, checks on typed addresses and IDs, one request with a plain refusal,
|
|
3
|
+
// and helpers. Callers pass their SERVICES entry in, so service modules can use this without an import loop.
|
|
4
|
+
import { Fail, Refused } from "../lib/cli.mjs";
|
|
5
|
+
import { retryAfter } from "../lib/http.mjs";
|
|
6
|
+
|
|
7
|
+
const UA = "seo-supervisor-connect-keys";
|
|
8
|
+
// Fail is bad input (exit 2); Refused is a service saying no (noted, the rest still runs, exit 1). Re-exported so every
|
|
9
|
+
// instanceof check sees the same classes.
|
|
10
|
+
export { Fail, Refused };
|
|
11
|
+
export const fail = msg => {
|
|
12
|
+
throw new Fail("connect-keys: " + msg);
|
|
13
|
+
};
|
|
14
|
+
export const setLine = id => `node connect-keys.mjs set ${id}`;
|
|
15
|
+
// HTTP 401 and status 40100 mean the same.
|
|
16
|
+
export const DFS_AUTH = `DataForSEO didn't accept the API login and password. Both are on https://app.dataforseo.com/api-access; the API password isn't your account password. Then run: ${setLine("dataforseo")}`;
|
|
17
|
+
export const PASTE =
|
|
18
|
+
" Paste the key when it asks and press Enter. It isn't shown, so it stays out of chats and your command history.";
|
|
19
|
+
// A typed address, kept as origin and path ending in "/": https:// added when missing, a page such as index.php and
|
|
20
|
+
// anything after it dropped. Plain http only on this computer, since the token travels with it.
|
|
21
|
+
export function address(s, what) {
|
|
22
|
+
let x;
|
|
23
|
+
try {
|
|
24
|
+
x = new URL(/^[a-z][a-z0-9+.-]*:\/\//i.test(s) ? s : "https://" + s);
|
|
25
|
+
} catch {}
|
|
26
|
+
if (!x || !/^https?:$/.test(x.protocol)) fail(`${what} isn't a web address (got "${s}").`);
|
|
27
|
+
if (x.protocol === "http:" && !/^(localhost|127\.\d+\.\d+\.\d+|\[::1\])$/.test(x.hostname))
|
|
28
|
+
fail(`${what} must start with https://. Over plain http:// your token would travel unencrypted.`);
|
|
29
|
+
return x.origin + x.pathname.replace(/[^/]*\.php$/, "").replace(/\/*$/, "/");
|
|
30
|
+
}
|
|
31
|
+
export function idOf(s, what, where) {
|
|
32
|
+
return /^\d+$/.test(s) ? s : fail(`the ${what} is a number, like 1 or 3 (got "${s}"). Find it ${where}.`);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// ---------- requests ----------
|
|
36
|
+
const typedAddress = s => !!s.fields?.some(f => f.address);
|
|
37
|
+
// https://learn.microsoft.com/dotnet/api/microsoft.bing.webmaster.api.interfaces.apierrorcode
|
|
38
|
+
const BING_ERRORS = { 3: "key", 4: "busy", 5: "busy", 6: "blocked", 7: "url", 14: "site" };
|
|
39
|
+
// Matomo and PostHog live at a typed address, so a wrong address, site or project ID gets its own message.
|
|
40
|
+
// Matomo answers errors as { result: "error", message }, often with HTTP 200; PostHog as { detail } with the status.
|
|
41
|
+
function addressed(id, s, status, body, host, moved) {
|
|
42
|
+
const fix = `Check it with node connect-keys.mjs guide ${id}, then run: ${setLine(id)}`,
|
|
43
|
+
msg = String(body?.message || body?.detail || "");
|
|
44
|
+
if (status >= 300 && status < 400)
|
|
45
|
+
return `${s.name}: ${host} sent the request on to ${moved || "another address"}, so nothing was read. Set the address it goes to instead. ${fix}`;
|
|
46
|
+
if (body === null && status < 500)
|
|
47
|
+
return `${s.name}: ${host} didn't answer like ${s.name} (HTTP ${status}), so the address is probably wrong. Use the address you open ${s.name} at in your browser. ${fix}`;
|
|
48
|
+
if (id === "matomo") {
|
|
49
|
+
if (/authenticat|token/i.test(msg))
|
|
50
|
+
return `Matomo didn't accept the token: it's mistyped, expired or was deleted. Create a new one under ${s.where}, then run: ${setLine(id)}`;
|
|
51
|
+
if (/segment/i.test(msg))
|
|
52
|
+
return `Matomo hasn't processed the segment for visits from search engines, so it has no report yet. In Matomo, open the segment menu above any report → Add new segment, choose Channel Type, Is, search, and save it with its reports pre-processed. Matomo fills it in within a few hours; then fetch again.`;
|
|
53
|
+
if (/access|website id|idsite/i.test(msg))
|
|
54
|
+
return `Matomo says this token can't see the site ID you set (${msg}). Check the site ID under Administration → Websites → Manage, and that the token's user has at least view access to that site. Then run: ${setLine(id)}`;
|
|
55
|
+
if (body?.result === "error") return `Matomo said: ${msg}`;
|
|
56
|
+
}
|
|
57
|
+
if (id === "posthog") {
|
|
58
|
+
if (status === 401)
|
|
59
|
+
return `PostHog didn't accept the key (401): it's mistyped or was deleted, or it belongs to the other PostHog region (a US key doesn't work on eu.posthog.com, nor an EU key on us.posthog.com). ${fix}`;
|
|
60
|
+
if (status === 403 && /scope/i.test(msg))
|
|
61
|
+
return `PostHog says the key isn't allowed to read data (it lacks the Query Read permission). Create a personal API key with Query: Read under ${s.where}, then run: ${setLine(id)}`;
|
|
62
|
+
if (status === 403 || status === 404)
|
|
63
|
+
return `PostHog has no project with the ID you set that this key may use (${status}${msg ? `: ${msg}` : ""}). Check the project ID under Settings → Project, and that the key is allowed to use that project. ${fix}`;
|
|
64
|
+
if (status === 400)
|
|
65
|
+
return `PostHog couldn't run the query for visits from search engines (${msg || 400}). If it keeps happening, PostHog may have changed.`;
|
|
66
|
+
}
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
// s: the service's entry in SERVICES. retry: the Retry-After header, in seconds or as a date (lib/http.mjs).
|
|
70
|
+
export function refusal(id, s, status, body, retry, host, moved) {
|
|
71
|
+
if (id === "dataforseo" && status === 401) return DFS_AUTH;
|
|
72
|
+
if (id === "dataforseo" && status === 402)
|
|
73
|
+
return "DataForSEO says payment is required (402): the balance is too low. Top up at https://app.dataforseo.com, then run again.";
|
|
74
|
+
if (id === "semrush" && status === 401)
|
|
75
|
+
return `Semrush didn't accept the key (401): it's mistyped or was deleted, or it's a v3 key, which v4 doesn't take. Get a v4 key at ${s.where}, then run: ${setLine(id)}`;
|
|
76
|
+
if (typedAddress(s)) {
|
|
77
|
+
const m = addressed(id, s, status, body, host, moved);
|
|
78
|
+
if (m) return m;
|
|
79
|
+
}
|
|
80
|
+
const bing = id === "bing" ? BING_ERRORS[body?.ErrorCode] : null;
|
|
81
|
+
// Moz's -32670 (HTTP 403, insufficient-quota) is a used-up month; it's checked before the call runs, so it costs
|
|
82
|
+
// nothing. https://moz.com/api/docs/guides/error-handling
|
|
83
|
+
if (id === "moz" && (body?.error?.code === -32670 || /quota/i.test(body?.error?.data?.issue || "")))
|
|
84
|
+
return `Moz says the rows for this month are used up (the free plan has 50 rows a month, one per domain). Nothing was charged for this request. The rows refill next month, or you can upgrade at moz.com/products/api/pricing.`;
|
|
85
|
+
// quotaExceeded and rateLimitExceeded are YouTube's; a turned-off API and a bad key are Google Cloud's general
|
|
86
|
+
// errors, matched by reason or message as google.mjs does. https://developers.google.com/youtube/v3/docs/errors
|
|
87
|
+
if (id === "youtube") {
|
|
88
|
+
const why = String(body?.error?.errors?.[0]?.reason || body?.error?.status || ""),
|
|
89
|
+
msg = String(body?.error?.message || "");
|
|
90
|
+
if (/quotaExceeded|rateLimitExceeded/.test(why))
|
|
91
|
+
return `YouTube Data API says the key's quota is used up for now (search has 100 calls a day, everything else 10,000 units a day). Try again tomorrow.`;
|
|
92
|
+
if (/accessNotConfigured|SERVICE_DISABLED/.test(why) || /has not been used|is disabled/i.test(msg))
|
|
93
|
+
return `YouTube Data API v3 isn't turned on in the key's Google Cloud project. Open console.cloud.google.com → APIs & Services → Library, search "YouTube Data API v3" and click Enable, wait a minute, then fetch again.`;
|
|
94
|
+
if (/keyInvalid|API_KEY_INVALID/.test(why) || /API key not valid/i.test(msg))
|
|
95
|
+
return `YouTube Data API didn't accept the key: it's mistyped, deleted, or limited to other APIs. Check it at ${s.where}, then run: ${setLine(id)}`;
|
|
96
|
+
}
|
|
97
|
+
// Microsoft's error table: 401 a missing, invalid or expired token, 403 "Token not authorized for operation", 429
|
|
98
|
+
// "Exceeded daily limit" (10 requests per project per day).
|
|
99
|
+
if (id === "clarity") {
|
|
100
|
+
if (status === 401)
|
|
101
|
+
return `Microsoft Clarity didn't accept the token (401): it's mistyped, expired or was replaced. Make a new one under ${s.where}, then run: ${setLine(id)}`;
|
|
102
|
+
if (status === 403)
|
|
103
|
+
return `Microsoft Clarity says this token can't read the project's data (403). A project admin can make a new token under ${s.where}; then run: ${setLine(id)}`;
|
|
104
|
+
if (status === 429)
|
|
105
|
+
return `Microsoft Clarity allows 10 requests a day per project, and today's are used up (429). Fetch again tomorrow.`;
|
|
106
|
+
}
|
|
107
|
+
// Google Cloud's ErrorInfo reasons come in error.details. A Place Details 404 means an obsolete or wrong place ID.
|
|
108
|
+
// https://github.com/googleapis/googleapis/blob/master/google/api/error_reason.proto
|
|
109
|
+
// https://developers.google.com/maps/documentation/places/web-service/place-id
|
|
110
|
+
if (id === "googleplaces") {
|
|
111
|
+
const why = (Array.isArray(body?.error?.details) ? body.error.details : []).map(d => d?.reason).join(" "),
|
|
112
|
+
msg = String(body?.error?.message || "");
|
|
113
|
+
if (/API_KEY_INVALID/.test(why) || /API key not valid/i.test(msg))
|
|
114
|
+
return `Google Places API didn't accept the key: it's mistyped or was deleted. Check it at ${s.where}, then run: ${setLine(id)}`;
|
|
115
|
+
if (/API_KEY_SERVICE_BLOCKED/.test(why))
|
|
116
|
+
return `The key isn't allowed to use Places API (New). At ${s.where}, open the key and select Places API (New) under API restrictions, then run again.`;
|
|
117
|
+
if (/SERVICE_DISABLED/.test(why) || /has not been used|is disabled/i.test(msg))
|
|
118
|
+
return `Places API (New) isn't turned on in the key's Google Cloud project. Open console.cloud.google.com/apis/library/places.googleapis.com and click Enable, wait a few minutes, then run again.`;
|
|
119
|
+
if (/BILLING_DISABLED/.test(why) || /billing/i.test(msg))
|
|
120
|
+
return `Google says billing isn't turned on for the key's Google Cloud project. Google requires it for the Places API even when your use stays within the free amounts. Turn it on at console.cloud.google.com/billing, then run again.`;
|
|
121
|
+
if (status === 404)
|
|
122
|
+
return `Google has no place with this place ID (404): it's mistyped, or out of date (a place ID can change when a business moves or Google updates its data). Find the current one at https://developers.google.com/maps/documentation/javascript/examples/places-placeid-finder or with geo-grid.mjs --find.`;
|
|
123
|
+
}
|
|
124
|
+
// Perplexity's 402 is an account with no credits. An OpenAI 429 whose error.code names credits or a spend or usage
|
|
125
|
+
// limit, or whose type is insufficient_quota, isn't a rate limit, so retrying won't help.
|
|
126
|
+
// https://docs.perplexity.ai/docs/resources/faq
|
|
127
|
+
// https://developers.openai.com/api/docs/guides/error-codes
|
|
128
|
+
if (id === "perplexity" && status === 402)
|
|
129
|
+
return `Perplexity says the account has no API credits left (402). Buy credits on the Billing page (console.perplexity.ai/project/billing), or turn on auto top-up, then run again.`;
|
|
130
|
+
if (
|
|
131
|
+
id === "openai" &&
|
|
132
|
+
status === 429 &&
|
|
133
|
+
/credit|spend_limit|usage_limit|insufficient_quota/.test(`${body?.error?.code} ${body?.error?.type}`)
|
|
134
|
+
)
|
|
135
|
+
return `OpenAI says the account can't pay for more requests (${body.error.code || body.error.type}): it has no credits left, or reached a spend or usage limit. Add credits or raise the limit at platform.openai.com/settings/organization/billing, then run again. Trying again before that won't help.`;
|
|
136
|
+
if (status === 401 || bing === "key")
|
|
137
|
+
return `${s.name} didn't accept the key (${status === 401 ? 401 : "InvalidApiKey"}): it's mistyped, or it was deleted or replaced. Get the current key at ${s.where}, then run: ${setLine(id)}`;
|
|
138
|
+
if (id === "openpagerank" && status === 403)
|
|
139
|
+
return `Open PageRank refused the request (403): your IP address isn't the one registered for this key. Free keys only work from one registered IP address, so your internet connection or IP has probably changed. Update the IP in your Open PageRank dashboard (keywordseverywhere.com/openpagerank); it can change once a week.`;
|
|
140
|
+
const wait = Math.ceil((retryAfter(retry) ?? 0) / 1000);
|
|
141
|
+
if (status === 429 || bing === "busy")
|
|
142
|
+
return `${s.name} says too many requests (${status === 429 ? 429 : "throttled"}): the key's limit is used up for now. ${wait ? `Wait ${wait} second${wait === 1 ? "" : "s"}` : "Wait a while"} and run again.`;
|
|
143
|
+
if (bing === "site")
|
|
144
|
+
return `Bing Webmaster Tools says this key's account can't see the site (NotAuthorized). Verify the site in Bing Webmaster Tools with the same account the key belongs to.`;
|
|
145
|
+
if (bing === "blocked")
|
|
146
|
+
return `Bing Webmaster Tools says the account is blocked (UserBlocked). Sign in at bing.com/webmasters to see why.`;
|
|
147
|
+
if (bing === "url")
|
|
148
|
+
return `Bing Webmaster Tools refused a URL (InvalidUrl): every URL must be on the site exactly as it's verified in Bing Webmaster Tools (the same https:// and www). Check the site's address there and pass it as --site.`;
|
|
149
|
+
if (status === 403)
|
|
150
|
+
return `${s.name} refused the request (403): the key isn't allowed to do this. Check the key at ${s.where}, then run: ${setLine(id)}`;
|
|
151
|
+
const detail =
|
|
152
|
+
body?.Message ||
|
|
153
|
+
body?.message ||
|
|
154
|
+
body?.detail ||
|
|
155
|
+
body?.error?.message ||
|
|
156
|
+
(typeof body?.error === "string" ? body.error : "") ||
|
|
157
|
+
(Array.isArray(body) ? body.join(" ") : "");
|
|
158
|
+
return `${s.name} answered ${status}${detail ? ` (${detail})` : ""}. Try again later; if it keeps happening, the service may have changed.`;
|
|
159
|
+
}
|
|
160
|
+
// One call to a service; connect-keys.mjs's call(id, url, init) passes SERVICES[id] as s.
|
|
161
|
+
export async function request(id, s, url, init = {}) {
|
|
162
|
+
const host = new URL(url).host;
|
|
163
|
+
let r;
|
|
164
|
+
try {
|
|
165
|
+
r = await fetch(url, {
|
|
166
|
+
...init,
|
|
167
|
+
headers: { "user-agent": UA, ...init.headers },
|
|
168
|
+
signal: AbortSignal.timeout(s.timeout || 30000),
|
|
169
|
+
});
|
|
170
|
+
} catch (e) {
|
|
171
|
+
throw new Refused(
|
|
172
|
+
`${s.name}: couldn't reach ${host} (${e.cause?.code || e.message}). Check the internet connection${typedAddress(s) ? ` and the ${s.name} address (node connect-keys.mjs status shows it)` : ""} and try again.`,
|
|
173
|
+
);
|
|
174
|
+
}
|
|
175
|
+
const text = await r.text();
|
|
176
|
+
let body = null;
|
|
177
|
+
try {
|
|
178
|
+
body = JSON.parse(text);
|
|
179
|
+
} catch {}
|
|
180
|
+
if (!r.ok)
|
|
181
|
+
throw new Refused(refusal(id, s, r.status, body, r.headers.get("retry-after"), host, r.headers.get("location")));
|
|
182
|
+
return body;
|
|
183
|
+
}
|
|
184
|
+
// A redirect isn't followed: on a 307 or 308 the token would go along to wherever it points.
|
|
185
|
+
export const POST = { method: "POST", redirect: "manual" };
|
|
186
|
+
|
|
187
|
+
// ---------- helpers ----------
|
|
188
|
+
export const chunks = (list, n) =>
|
|
189
|
+
Array.from({ length: Math.ceil(list.length / n) }, (_, i) => list.slice(i * n, i * n + n));
|
|
190
|
+
export const bare = h => h.toLowerCase().replace(/^www\./, "");
|
|
191
|
+
export const domainOf = s => {
|
|
192
|
+
try {
|
|
193
|
+
const h = bare(new URL(/^[a-z][a-z0-9+.-]*:\/\//i.test(s) ? s : "https://" + s).hostname);
|
|
194
|
+
return /^[a-z0-9-]+(\.[a-z0-9-]+)+$/.test(h) ? h : null;
|
|
195
|
+
} catch {
|
|
196
|
+
return null;
|
|
197
|
+
}
|
|
198
|
+
};
|
|
199
|
+
// The site's domain and the competitors', each once; a site with no domain name (localhost) is left out.
|
|
200
|
+
export const domainsFor = (siteUrl, competitors) =>
|
|
201
|
+
[domainOf(siteUrl), ...competitors.map(domainOf)].filter((d, i, all) => d && all.indexOf(d) === i);
|
|
202
|
+
// The host as written, www kept ("" for a non-address). Use bare(hostOf(u)) where www and apex are one site.
|
|
203
|
+
export const hostOf = u => {
|
|
204
|
+
try {
|
|
205
|
+
return new URL(u).hostname;
|
|
206
|
+
} catch {
|
|
207
|
+
return "";
|
|
208
|
+
}
|
|
209
|
+
};
|
|
210
|
+
export const day = ms => new Date(ms).toISOString().slice(0, 10);
|
|
211
|
+
export const isDay = d => /^\d{4}-\d\d-\d\d$/.test(d);
|
|
212
|
+
export const num = n => n.toLocaleString("en-US");
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
// Matomo: the key's entry, search-engine visits by landing page with their splits, and the connection test.
|
|
3
|
+
import { Refused, setLine, address, idOf, refusal, request, POST, bare, isDay } from "./http.mjs";
|
|
4
|
+
import { ROWS, splits, once, addVisits, tooMany } from "./visits.mjs";
|
|
5
|
+
|
|
6
|
+
// Matomo and PostHog also need an address and an ID (`fields`), saved with the key: { url, siteId, key }.
|
|
7
|
+
export const matomo = {
|
|
8
|
+
name: "Matomo",
|
|
9
|
+
env: "MATOMO_TOKEN_AUTH",
|
|
10
|
+
adds: "visits from search engines to each page, from your Matomo",
|
|
11
|
+
where: "Administration (the gear icon) → Personal → Security → Auth tokens",
|
|
12
|
+
fields: [
|
|
13
|
+
{
|
|
14
|
+
id: "url",
|
|
15
|
+
flag: "url",
|
|
16
|
+
env: "MATOMO_URL",
|
|
17
|
+
address: true,
|
|
18
|
+
ask: "Your Matomo address (the one you open Matomo at, like https://analytics.example.com)",
|
|
19
|
+
check: s => address(s, "The Matomo address"),
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
id: "siteId",
|
|
23
|
+
flag: "site-id",
|
|
24
|
+
env: "MATOMO_SITE_ID",
|
|
25
|
+
ask: "The site ID (a number, like 1)",
|
|
26
|
+
check: s => idOf(s, "site ID", "in Matomo under Administration → Websites → Manage, in the ID column"),
|
|
27
|
+
},
|
|
28
|
+
],
|
|
29
|
+
about: k => `site ID ${k.siteId} at ${k.url}`,
|
|
30
|
+
guide: [
|
|
31
|
+
"Matomo adds the visits that came from search engines to each page, from your own Matomo (self-hosted or Matomo Cloud). You need three things: your Matomo address, the site ID and a token.",
|
|
32
|
+
"1. Sign in to Matomo. Its address is the one in your browser's address bar, like https://analytics.example.com or https://example.matomo.cloud.",
|
|
33
|
+
"2. The site ID: open Administration (the gear icon) → Websites → Manage. It's the number in the ID column next to your site.",
|
|
34
|
+
'3. The token: open Administration → Personal → Security, scroll to Auth tokens and click Create new token. Confirm your password, write "SEO Supervisor" as the description, tick "Only allow secure requests" if it\'s offered, and create it. Copy the token straight away: Matomo shows it only once.',
|
|
35
|
+
" The token reads whatever your Matomo user can, so a user with view access to this one site is enough.",
|
|
36
|
+
`4. In your own terminal run: ${setLine("matomo")}`,
|
|
37
|
+
" It asks for the address and the site ID (shown as you type), then the token. Paste the token and press Enter. It isn't shown, so it stays out of chats and your command history.",
|
|
38
|
+
"If fetch says the segment hasn't been created, your Matomo only reports on saved segments. Above any report, open the segment menu → Add new segment, choose Channel Type, Is, search, and save it with its reports pre-processed. Matomo fills it in within a few hours; then fetch again.",
|
|
39
|
+
"To check the connection first: node connect-keys.mjs test matomo --yes (it shows your Matomo's version).",
|
|
40
|
+
// https://matomo.org/faq/troubleshooting/faq_50/
|
|
41
|
+
"Matomo's numbers won't match Google Analytics exactly. Matomo doesn't sample data, and Google Analytics can. A visitor who accepts one tool in the consent banner but not the other is counted by only one. And the two tools spot bots and count visits in their own ways. Compare the trends, not the totals.",
|
|
42
|
+
],
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
// Visits from a search engine (segment referrerType==search) by entry page, most first. Only token_auth goes in the
|
|
46
|
+
// POST body; the other parameters aren't secret. The splits cover the whole site ID: the same visits by device type
|
|
47
|
+
// and country, every visit by channel (no segment), and search visits by engine. With period=day, VisitsSummary.get
|
|
48
|
+
// gives search visits per date, [] for a day with none.
|
|
49
|
+
// https://developer.matomo.org/api-reference/reporting-api-legacy
|
|
50
|
+
export const MATOMO_SPLITS = {
|
|
51
|
+
byDevice: "DevicesDetection.getType",
|
|
52
|
+
byCountry: "UserCountry.getCountry",
|
|
53
|
+
byChannel: "Referrers.getReferrerType",
|
|
54
|
+
byEngine: "Referrers.getSearchEngines",
|
|
55
|
+
};
|
|
56
|
+
const MATOMO_ALL = "Referrers.getReferrerType";
|
|
57
|
+
export const MATOMO_DAILY = "VisitsSummary.get";
|
|
58
|
+
export const matomoUrl = (k, p, method = "Actions.getEntryPageUrls") =>
|
|
59
|
+
`${k.url}index.php?${new URLSearchParams(
|
|
60
|
+
/** @type {any} */ ({
|
|
61
|
+
module: "API",
|
|
62
|
+
method,
|
|
63
|
+
idSite: k.siteId,
|
|
64
|
+
period: method === MATOMO_DAILY ? "day" : "range",
|
|
65
|
+
date: `${p.from},${p.to}`,
|
|
66
|
+
...(method === MATOMO_ALL ? {} : { segment: "referrerType==search" }),
|
|
67
|
+
...(method === "Actions.getEntryPageUrls"
|
|
68
|
+
? { flat: 1, filter_limit: ROWS, filter_sort_column: "entry_nb_visits", filter_sort_order: "desc" }
|
|
69
|
+
: method === MATOMO_DAILY
|
|
70
|
+
? {}
|
|
71
|
+
: { filter_limit: 10, filter_sort_column: "nb_visits", filter_sort_order: "desc" }),
|
|
72
|
+
format: "JSON",
|
|
73
|
+
}),
|
|
74
|
+
)}`;
|
|
75
|
+
|
|
76
|
+
export async function fetchMatomo(k, site, period, notes) {
|
|
77
|
+
const want = bare(new URL(site).hostname),
|
|
78
|
+
traffic = {};
|
|
79
|
+
// list: a report's rows; otherwise one object (the daily trend's dates).
|
|
80
|
+
const read = async (method, list = true) => {
|
|
81
|
+
const rows = await request("matomo", matomo, matomoUrl(k, period, method), {
|
|
82
|
+
...POST,
|
|
83
|
+
body: new URLSearchParams({ token_auth: k.key }),
|
|
84
|
+
});
|
|
85
|
+
if (list ? !Array.isArray(rows) : !rows || typeof rows !== "object" || rows.result === "error")
|
|
86
|
+
throw new Refused(refusal("matomo", matomo, 200, rows, null, new URL(k.url).host));
|
|
87
|
+
return rows;
|
|
88
|
+
};
|
|
89
|
+
const rows = await read();
|
|
90
|
+
// Rows without a URL (Matomo's "Others") and other sites tracked in the same Matomo are left out.
|
|
91
|
+
for (const r of rows) {
|
|
92
|
+
let x;
|
|
93
|
+
try {
|
|
94
|
+
x = new URL(r?.url);
|
|
95
|
+
} catch {
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
const n = Number(r.entry_nb_visits) || 0;
|
|
99
|
+
if (n > 0 && bare(x.hostname) === want) addVisits(traffic, x.pathname, n);
|
|
100
|
+
}
|
|
101
|
+
if (rows.length >= ROWS) notes.push(tooMany("Matomo"));
|
|
102
|
+
// Countries by code, upper case as PostHog's; "xx" is Matomo's Unknown, not a country.
|
|
103
|
+
const name = (r, field) =>
|
|
104
|
+
field !== "byCountry" ? r?.label : r?.code !== "xx" && typeof r?.code === "string" ? r.code.toUpperCase() : null;
|
|
105
|
+
return {
|
|
106
|
+
period,
|
|
107
|
+
traffic,
|
|
108
|
+
...(await splits(
|
|
109
|
+
"Matomo",
|
|
110
|
+
MATOMO_SPLITS,
|
|
111
|
+
async (method, field) => (await read(method)).map(r => [name(r, field), r?.nb_visits]),
|
|
112
|
+
notes,
|
|
113
|
+
)),
|
|
114
|
+
...(await once(
|
|
115
|
+
"Matomo",
|
|
116
|
+
"daily",
|
|
117
|
+
async () =>
|
|
118
|
+
Object.fromEntries(
|
|
119
|
+
Object.entries(await read(MATOMO_DAILY, false))
|
|
120
|
+
.filter(([d]) => isDay(d))
|
|
121
|
+
.map(([d, v]) => [d, Number(v?.nb_visits) || 0]),
|
|
122
|
+
),
|
|
123
|
+
notes,
|
|
124
|
+
)),
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// The connection test (test matomo --yes): API.getMatomoVersion answers { value: "5.x" } to a valid token.
|
|
129
|
+
export async function testMatomo(k) {
|
|
130
|
+
const b = await request(
|
|
131
|
+
"matomo",
|
|
132
|
+
matomo,
|
|
133
|
+
`${k.url}index.php?${new URLSearchParams({ module: "API", method: "API.getMatomoVersion", format: "JSON" })}`,
|
|
134
|
+
{ ...POST, body: new URLSearchParams({ token_auth: k.key }) },
|
|
135
|
+
);
|
|
136
|
+
if (typeof b?.value !== "string") throw new Refused(refusal("matomo", matomo, 200, b, null, new URL(k.url).host));
|
|
137
|
+
return `Matomo ${b.value} at ${k.url} accepted the token.`;
|
|
138
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
import { setLine, PASTE } from "./http.mjs";
|
|
3
|
+
|
|
4
|
+
// answers: ai-answers.mjs uses it; fetch doesn't. Each answer searches the web first, so it gets 2 minutes, not the
|
|
5
|
+
// usual 30 seconds.
|
|
6
|
+
// https://developers.openai.com/api/docs/quickstart
|
|
7
|
+
export const openai = {
|
|
8
|
+
name: "OpenAI",
|
|
9
|
+
env: "OPENAI_API_KEY",
|
|
10
|
+
endpointEnv: "OPENAI_ENDPOINT",
|
|
11
|
+
endpoint: "https://api.openai.com/v1/responses",
|
|
12
|
+
optional: true,
|
|
13
|
+
answers: true,
|
|
14
|
+
timeout: 120000,
|
|
15
|
+
adds: "whether the model ChatGPT uses, searching the web, cites your site when it answers your customers' questions (node ai-answers.mjs)",
|
|
16
|
+
where: "platform.openai.com/api-keys",
|
|
17
|
+
guide: [
|
|
18
|
+
"OpenAI's API lets ai-answers.mjs ask the model ChatGPT uses, with web search, the questions your customers ask, and count how often its answers cite your site. It uses your own key, and OpenAI bills each answer to your account.",
|
|
19
|
+
"1. Sign in at https://platform.openai.com.",
|
|
20
|
+
"2. Open https://platform.openai.com/api-keys and create a new secret key. Copy it and keep it private.",
|
|
21
|
+
"3. The API needs credits: add them on the billing page (https://platform.openai.com/account/billing/overview).",
|
|
22
|
+
`4. In your own terminal run: ${setLine("openai")}`,
|
|
23
|
+
PASTE,
|
|
24
|
+
"Then node ai-answers.mjs --site <your address> --questions <file> shows what it would send and how many requests that is. Nothing is sent until you add --yes.",
|
|
25
|
+
],
|
|
26
|
+
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
import { setLine, PASTE } from "./http.mjs";
|
|
3
|
+
|
|
4
|
+
// answers: ai-answers.mjs uses it; fetch doesn't. Each answer searches the web first, so it gets 2 minutes, not the
|
|
5
|
+
// usual 30 seconds.
|
|
6
|
+
// https://docs.perplexity.ai/docs/getting-started/quickstart
|
|
7
|
+
export const perplexity = {
|
|
8
|
+
name: "Perplexity",
|
|
9
|
+
env: "PERPLEXITY_API_KEY",
|
|
10
|
+
endpointEnv: "PERPLEXITY_ENDPOINT",
|
|
11
|
+
endpoint: "https://api.perplexity.ai/v1/agent",
|
|
12
|
+
optional: true,
|
|
13
|
+
answers: true,
|
|
14
|
+
timeout: 120000,
|
|
15
|
+
adds: "whether Perplexity's answers to your customers' questions cite your site (node ai-answers.mjs)",
|
|
16
|
+
where: "console.perplexity.ai → API Keys",
|
|
17
|
+
guide: [
|
|
18
|
+
"Perplexity's API lets ai-answers.mjs ask Perplexity the questions your customers ask, and count how often its answers cite your site. It uses your own key, and Perplexity bills each answer to your account.",
|
|
19
|
+
"1. Sign in at https://console.perplexity.ai and create or pick a project.",
|
|
20
|
+
"2. Open API Keys (https://console.perplexity.ai/project/keys) and click + Generate API Key. Copy the key straight away: the console shows it only once.",
|
|
21
|
+
"3. The API needs credits: sign-up credit alone isn't enough. Buy credits on the Billing page (https://console.perplexity.ai/project/billing).",
|
|
22
|
+
`4. In your own terminal run: ${setLine("perplexity")}`,
|
|
23
|
+
PASTE,
|
|
24
|
+
"Then node ai-answers.mjs --site <your address> --questions <file> shows what it would send and how many requests that is. Nothing is sent until you add --yes.",
|
|
25
|
+
],
|
|
26
|
+
};
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
// PostHog: the key's entry, search-engine visits by landing page with their splits (HogQL), and the connection test.
|
|
3
|
+
import { Refused, setLine, address, idOf, refusal, request, POST, bare, isDay, num } from "./http.mjs";
|
|
4
|
+
import { ROWS, splits, once, addVisits, tooMany } from "./visits.mjs";
|
|
5
|
+
|
|
6
|
+
// Like Matomo, it also needs an address and an ID (`fields`), saved with the key: { host, projectId, key }.
|
|
7
|
+
export const posthog = {
|
|
8
|
+
name: "PostHog",
|
|
9
|
+
env: "POSTHOG_PERSONAL_API_KEY",
|
|
10
|
+
adds: "visits from search engines to each page, from your PostHog",
|
|
11
|
+
where: "PostHog → Settings → User → Personal API keys",
|
|
12
|
+
fields: [
|
|
13
|
+
{
|
|
14
|
+
id: "host",
|
|
15
|
+
flag: "host",
|
|
16
|
+
env: "POSTHOG_HOST",
|
|
17
|
+
address: true,
|
|
18
|
+
ask: "Your PostHog address: us, eu, or your own PostHog's address",
|
|
19
|
+
check: s =>
|
|
20
|
+
/^(us|eu)$/i.test(s)
|
|
21
|
+
? `https://${s.toLowerCase()}.posthog.com/`
|
|
22
|
+
: address(s, "The PostHog address").replace(
|
|
23
|
+
/^https:\/\/(us|eu)\.i\.posthog\.com\/$/,
|
|
24
|
+
"https://$1.posthog.com/",
|
|
25
|
+
),
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
id: "projectId",
|
|
29
|
+
flag: "project-id",
|
|
30
|
+
env: "POSTHOG_PROJECT_ID",
|
|
31
|
+
ask: "The project ID (a number)",
|
|
32
|
+
check: s => idOf(s, "project ID", "in PostHog under Settings → Project"),
|
|
33
|
+
},
|
|
34
|
+
],
|
|
35
|
+
about: k => `project ID ${k.projectId} at ${k.host}`,
|
|
36
|
+
guide: [
|
|
37
|
+
"PostHog adds the visits that came from search engines to each page (the sessions PostHog counts as Organic Search), from your own PostHog. You need three things: the PostHog address, the project ID and a personal API key.",
|
|
38
|
+
"1. Sign in to PostHog. The address is https://us.posthog.com (US cloud), https://eu.posthog.com (EU cloud) or your own PostHog's address.",
|
|
39
|
+
"2. The project ID: open Settings → Project. It's the number shown as Project ID (it's also in the address bar, after /project/).",
|
|
40
|
+
'3. The key: open Settings → User → Personal API keys and click Create personal API key. Name it "SEO Supervisor", let it use only this project, and give it one permission: Query: Read. Copy the key straight away: it starts with phx_ and PostHog shows it only once.',
|
|
41
|
+
" Not the project key in your site's tracking code (it starts with phc_): that one can only send data, not read it.",
|
|
42
|
+
`4. In your own terminal run: ${setLine("posthog")}`,
|
|
43
|
+
" It asks for the address (type us, eu or your own address) and the project ID, shown as you type, then the key. Paste the key and press Enter. It isn't shown, so it stays out of chats and your command history.",
|
|
44
|
+
"To check the connection first: node connect-keys.mjs test posthog --yes (it counts the project's pageviews in the last 7 days).",
|
|
45
|
+
],
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
// Sessions PostHog files as Organic Search (a search engine as referrer or utm_source, paid search left out, as in
|
|
49
|
+
// GA4), by entry page. The site's host is matched here, not sent.
|
|
50
|
+
// Without $channel_type, the fallback counts sessions referred by a search engine. These LIKE patterns can count a
|
|
51
|
+
// little paid search and Google's other sites; port PostHog's channel_definitions.json if that matters.
|
|
52
|
+
// https://posthog.com/docs/data/channel-type
|
|
53
|
+
const ENGINES = [
|
|
54
|
+
"%google.%",
|
|
55
|
+
"%bing.com",
|
|
56
|
+
"%duckduckgo.com",
|
|
57
|
+
"%search.yahoo.%",
|
|
58
|
+
"%yandex.%",
|
|
59
|
+
"%baidu.com",
|
|
60
|
+
"%ecosia.org",
|
|
61
|
+
"%qwant.com",
|
|
62
|
+
"%startpage.com",
|
|
63
|
+
"%search.brave.com",
|
|
64
|
+
"%naver.com",
|
|
65
|
+
"%seznam.cz",
|
|
66
|
+
];
|
|
67
|
+
const organic = (fallback, pre = "") =>
|
|
68
|
+
fallback
|
|
69
|
+
? `(${ENGINES.map(e => `${pre}$entry_referring_domain LIKE '${e}'`).join(" OR ")})`
|
|
70
|
+
: `${pre}$channel_type = 'Organic Search'`;
|
|
71
|
+
const between = (p, field) => `toDate(${field}) BETWEEN toDate('${p.from}') AND toDate('${p.to}')`;
|
|
72
|
+
export const posthogQuery = (p, fb) =>
|
|
73
|
+
`SELECT domainWithoutWWW($entry_current_url) AS host, $entry_pathname AS path, count() AS visits FROM sessions WHERE ${organic(fb)} AND ${between(p, "$start_timestamp")} GROUP BY host, path ORDER BY visits DESC LIMIT ${ROWS}`;
|
|
74
|
+
// sessions has no device or country, so the splits use its pageviews' $device_type and $geoip_country_code through
|
|
75
|
+
// events' `session` join. The events' dates bound the scan; a session starting in the period has its first pageview
|
|
76
|
+
// in it.
|
|
77
|
+
// https://posthog.com/docs/data/sessions
|
|
78
|
+
const posthogSplit = (p, prop, fb) =>
|
|
79
|
+
`SELECT domainWithoutWWW(session.$entry_current_url) AS host, ${prop} AS k, count(DISTINCT $session_id) AS visits FROM events WHERE event = '$pageview' AND ${organic(fb, "session.")} AND ${between(p, "session.$start_timestamp")} AND ${between(p, "timestamp")} GROUP BY host, k ORDER BY visits DESC LIMIT 1000`;
|
|
80
|
+
// byReferrer: every session by its referring domain ($direct when there was none), not only those from search.
|
|
81
|
+
export const POSTHOG_SPLITS = {
|
|
82
|
+
byDevice: (p, fb) => posthogSplit(p, "properties.$device_type", fb),
|
|
83
|
+
byCountry: (p, fb) => posthogSplit(p, "properties.$geoip_country_code", fb),
|
|
84
|
+
byReferrer: p =>
|
|
85
|
+
`SELECT domainWithoutWWW($entry_current_url) AS host, $entry_referring_domain AS k, count() AS visits FROM sessions WHERE ${between(p, "$start_timestamp")} GROUP BY host, k ORDER BY visits DESC LIMIT 1000`,
|
|
86
|
+
};
|
|
87
|
+
// Pageviews and visitors (distinct_id) per day, in the sessions from search.
|
|
88
|
+
export const posthogDaily = (p, fb) =>
|
|
89
|
+
`SELECT domainWithoutWWW(session.$entry_current_url) AS host, toString(toDate(timestamp)) AS d, count() AS pageviews, count(DISTINCT distinct_id) AS visitors FROM events WHERE event = '$pageview' AND ${organic(fb, "session.")} AND ${between(p, "session.$start_timestamp")} AND ${between(p, "timestamp")} GROUP BY host, d ORDER BY d LIMIT 1000`;
|
|
90
|
+
export const POSTHOG_TEST =
|
|
91
|
+
"SELECT count() FROM events WHERE event = '$pageview' AND timestamp > now() - INTERVAL 7 DAY";
|
|
92
|
+
export const posthogUrl = k => `${k.host}api/projects/${k.projectId}/query/`;
|
|
93
|
+
async function posthogRead(k, query) {
|
|
94
|
+
const b = await request("posthog", posthog, posthogUrl(k), {
|
|
95
|
+
...POST,
|
|
96
|
+
headers: { authorization: `Bearer ${k.key}`, "content-type": "application/json" },
|
|
97
|
+
body: JSON.stringify({ query: { kind: "HogQLQuery", query } }),
|
|
98
|
+
});
|
|
99
|
+
if (!Array.isArray(b?.results)) throw new Refused(refusal("posthog", posthog, 200, null, null, new URL(k.host).host));
|
|
100
|
+
return b.results;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export async function fetchPosthog(k, site, period, notes) {
|
|
104
|
+
const want = bare(new URL(site).hostname),
|
|
105
|
+
traffic = {},
|
|
106
|
+
read = q => posthogRead(k, q);
|
|
107
|
+
const mine = rows => rows.filter(r => Array.isArray(r) && bare(String(r[0] ?? "")) === want);
|
|
108
|
+
let fallback = false,
|
|
109
|
+
rows;
|
|
110
|
+
try {
|
|
111
|
+
rows = await read(posthogQuery(period));
|
|
112
|
+
} catch (e) {
|
|
113
|
+
if (!(e instanceof Refused) || !/channel_type/.test(e.message)) throw e;
|
|
114
|
+
fallback = true;
|
|
115
|
+
rows = await read(posthogQuery(period, true));
|
|
116
|
+
notes.push(
|
|
117
|
+
"PostHog couldn't use $channel_type here (an older PostHog?), so visits from search were counted from search engines' referring domains instead. That list is our own and may count a little paid search.",
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
for (const [, path, n] of mine(rows)) if (/^\//.test(path) && Number(n) > 0) addVisits(traffic, path, Number(n));
|
|
121
|
+
if (rows.length >= ROWS) notes.push(tooMany("PostHog"));
|
|
122
|
+
const split = await splits(
|
|
123
|
+
"PostHog",
|
|
124
|
+
POSTHOG_SPLITS,
|
|
125
|
+
async (q, field) =>
|
|
126
|
+
mine(await read(q(period, fallback))).map(([, v, n]) => [
|
|
127
|
+
field === "byCountry" && typeof v === "string" ? v.toUpperCase() : v,
|
|
128
|
+
n,
|
|
129
|
+
]),
|
|
130
|
+
notes,
|
|
131
|
+
);
|
|
132
|
+
const daily = await once(
|
|
133
|
+
"PostHog",
|
|
134
|
+
"daily",
|
|
135
|
+
async () =>
|
|
136
|
+
Object.fromEntries(
|
|
137
|
+
mine(await read(posthogDaily(period, fallback)))
|
|
138
|
+
.filter(r => isDay(r[1]))
|
|
139
|
+
.map(([, d, pv, v]) => [d, { pageviews: Number(pv) || 0, visitors: Number(v) || 0 }]),
|
|
140
|
+
),
|
|
141
|
+
notes,
|
|
142
|
+
);
|
|
143
|
+
// Zero visits from search mean something only if PostHog records referrers at all ($direct is its value for none).
|
|
144
|
+
const check = {};
|
|
145
|
+
if (!Object.keys(traffic).length && split.byReferrer) {
|
|
146
|
+
const seen = Object.keys(split.byReferrer);
|
|
147
|
+
check.referrersCaptured = seen.some(r => r !== "$direct");
|
|
148
|
+
if (!check.referrersCaptured)
|
|
149
|
+
notes.push(
|
|
150
|
+
seen.length
|
|
151
|
+
? "PostHog shows zero visits from search, but no visit to this site in the period has a referrer ($direct only), so PostHog can't tell visits from search apart. Check that the site's PostHog setup keeps the referrer; until then, don't trust the zero."
|
|
152
|
+
: "PostHog shows zero visits from search, but it recorded no visits to this site at all in the period, so the zero says nothing. Check that PostHog's code is on the site and that the project ID is this site's, and that referrers reach it.",
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
return { period, organicBy: fallback ? "referrer" : "channel", traffic, ...split, ...daily, ...check };
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// The connection test (test posthog --yes): the project's pageviews in the last 7 days.
|
|
159
|
+
export async function testPosthog(k) {
|
|
160
|
+
const n = Number((await posthogRead(k, POSTHOG_TEST))[0]?.[0]) || 0,
|
|
161
|
+
who = `PostHog project ${k.projectId} at ${k.host} accepted the key`;
|
|
162
|
+
return n
|
|
163
|
+
? `${who}: ${num(n)} pageviews in the last 7 days.`
|
|
164
|
+
: `${who}, but has no $pageview events in the last 7 days. Check that PostHog's code is on the site and sends pageviews.`;
|
|
165
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
// The Semrush API key, for keyword-data.mjs.
|
|
3
|
+
import { setLine, PASTE } from "./http.mjs";
|
|
4
|
+
|
|
5
|
+
// v4 only: v3 is deprecated and its keys don't work with v4.
|
|
6
|
+
// https://developer.semrush.com/api/v4
|
|
7
|
+
export const semrush = {
|
|
8
|
+
name: "Semrush",
|
|
9
|
+
env: "SEMRUSH_API_KEY",
|
|
10
|
+
endpointEnv: "SEMRUSH_ENDPOINT",
|
|
11
|
+
endpoint: "https://api.semrush.com/apis/v4",
|
|
12
|
+
optional: true,
|
|
13
|
+
script: "keyword-data.mjs",
|
|
14
|
+
adds: "monthly searches, keyword difficulty, cost per click and intent for your keyword ideas (node keyword-data.mjs)",
|
|
15
|
+
where: "https://www.semrush.com/accounts/api-keys/active",
|
|
16
|
+
guide: [
|
|
17
|
+
"Semrush lets keyword-data.mjs read monthly searches, keyword difficulty, cost per click and intent for your keyword ideas, from your own Semrush account. Each keyword costs 20 API units.",
|
|
18
|
+
"1. The API needs the SEO Toolkit Business plan (or Semrush Advanced or SEO Enterprise), and API units, which Semrush sells separately: a Business plan starts with none. Buy them under Subscription info → API units.",
|
|
19
|
+
"2. Open https://www.semrush.com/accounts/api-keys/active and copy a v4 key, or create one. Semrush shows a new key in full only once. A key made for the older v3 API doesn't work with v4.",
|
|
20
|
+
`3. In your own terminal run: ${setLine("semrush")}`,
|
|
21
|
+
PASTE,
|
|
22
|
+
"Then node keyword-data.mjs --site <your address> --ideas seo-audit/keyword-ideas.json --service semrush shows what it would send and how many units that is. Nothing is sent until you add --yes.",
|
|
23
|
+
"Semrush's terms don't allow keeping its data for more than a month without their written consent, so run it again rather than reusing an old keyword-data.json.",
|
|
24
|
+
],
|
|
25
|
+
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
// Every connection connect-keys.mjs knows, in the order status, fetch and the dry run list them. Each entry lives in
|
|
3
|
+
// its own module.
|
|
4
|
+
import { fail, request } from "./http.mjs";
|
|
5
|
+
import { bing } from "./bing.mjs";
|
|
6
|
+
import { openpagerank, ahrefs, moz } from "./authority.mjs";
|
|
7
|
+
import { matomo } from "./matomo.mjs";
|
|
8
|
+
import { posthog } from "./posthog.mjs";
|
|
9
|
+
import { youtube } from "./youtube.mjs";
|
|
10
|
+
import { clarity } from "./clarity.mjs";
|
|
11
|
+
import { perplexity } from "./perplexity.mjs";
|
|
12
|
+
import { openai } from "./openai.mjs";
|
|
13
|
+
import { googleplaces } from "./googleplaces.mjs";
|
|
14
|
+
import { dataforseo } from "./dataforseo.mjs";
|
|
15
|
+
import { semrush } from "./semrush.mjs";
|
|
16
|
+
|
|
17
|
+
export const SERVICES = {
|
|
18
|
+
bing,
|
|
19
|
+
openpagerank,
|
|
20
|
+
ahrefs,
|
|
21
|
+
matomo,
|
|
22
|
+
posthog,
|
|
23
|
+
moz,
|
|
24
|
+
youtube,
|
|
25
|
+
clarity,
|
|
26
|
+
perplexity,
|
|
27
|
+
openai,
|
|
28
|
+
googleplaces,
|
|
29
|
+
dataforseo,
|
|
30
|
+
semrush,
|
|
31
|
+
};
|
|
32
|
+
export const serviceOf = id =>
|
|
33
|
+
Object.hasOwn(SERVICES, id)
|
|
34
|
+
? id
|
|
35
|
+
: fail(
|
|
36
|
+
`there's no "${id}" connection. Use ${Object.keys(SERVICES)
|
|
37
|
+
.join(", ")
|
|
38
|
+
.replace(/, (\w+)$/, " or $1")}.`,
|
|
39
|
+
);
|
|
40
|
+
// ai-answers.mjs and geo-grid.mjs call services through this too.
|
|
41
|
+
export const call = (id, url, init) => request(id, SERVICES[id], url, init);
|