@rankcli/agent-runtime 0.0.1

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.
Files changed (178) hide show
  1. package/README.md +242 -0
  2. package/dist/analyzer-2CSWIQGD.mjs +6 -0
  3. package/dist/chunk-YNZYHEYM.mjs +774 -0
  4. package/dist/index.d.mts +4012 -0
  5. package/dist/index.d.ts +4012 -0
  6. package/dist/index.js +29672 -0
  7. package/dist/index.mjs +28602 -0
  8. package/package.json +53 -0
  9. package/scripts/build-deno.ts +134 -0
  10. package/src/audit/ai/analyzer.ts +347 -0
  11. package/src/audit/ai/index.ts +29 -0
  12. package/src/audit/ai/prompts/content-analysis.ts +271 -0
  13. package/src/audit/ai/types.ts +179 -0
  14. package/src/audit/checks/additional-checks.ts +439 -0
  15. package/src/audit/checks/ai-citation-worthiness.ts +399 -0
  16. package/src/audit/checks/ai-content-structure.ts +325 -0
  17. package/src/audit/checks/ai-readiness.ts +339 -0
  18. package/src/audit/checks/anchor-text.ts +179 -0
  19. package/src/audit/checks/answer-conciseness.ts +322 -0
  20. package/src/audit/checks/asset-minification.ts +270 -0
  21. package/src/audit/checks/bing-optimization.ts +206 -0
  22. package/src/audit/checks/brand-mention-optimization.ts +349 -0
  23. package/src/audit/checks/caching-headers.ts +305 -0
  24. package/src/audit/checks/canonical-advanced.ts +150 -0
  25. package/src/audit/checks/canonical-domain.ts +196 -0
  26. package/src/audit/checks/citation-quality.ts +358 -0
  27. package/src/audit/checks/client-rendering.ts +542 -0
  28. package/src/audit/checks/color-contrast.ts +342 -0
  29. package/src/audit/checks/content-freshness.ts +170 -0
  30. package/src/audit/checks/content-science.ts +589 -0
  31. package/src/audit/checks/conversion-elements.ts +526 -0
  32. package/src/audit/checks/crawlability.ts +220 -0
  33. package/src/audit/checks/directory-listing.ts +172 -0
  34. package/src/audit/checks/dom-analysis.ts +191 -0
  35. package/src/audit/checks/dom-size.ts +246 -0
  36. package/src/audit/checks/duplicate-content.ts +194 -0
  37. package/src/audit/checks/eeat-signals.ts +990 -0
  38. package/src/audit/checks/entity-seo.ts +396 -0
  39. package/src/audit/checks/featured-snippet.ts +473 -0
  40. package/src/audit/checks/freshness-signals.ts +443 -0
  41. package/src/audit/checks/funnel-intent.ts +463 -0
  42. package/src/audit/checks/hreflang.ts +174 -0
  43. package/src/audit/checks/html-compliance.ts +302 -0
  44. package/src/audit/checks/image-dimensions.ts +167 -0
  45. package/src/audit/checks/images.ts +160 -0
  46. package/src/audit/checks/indexnow.ts +275 -0
  47. package/src/audit/checks/interactive-tools.ts +475 -0
  48. package/src/audit/checks/internal-link-graph.ts +436 -0
  49. package/src/audit/checks/keyword-analysis.ts +239 -0
  50. package/src/audit/checks/keyword-cannibalization.ts +385 -0
  51. package/src/audit/checks/keyword-placement.ts +471 -0
  52. package/src/audit/checks/links.ts +203 -0
  53. package/src/audit/checks/llms-txt.ts +224 -0
  54. package/src/audit/checks/local-seo.ts +296 -0
  55. package/src/audit/checks/mobile.ts +167 -0
  56. package/src/audit/checks/modern-images.ts +226 -0
  57. package/src/audit/checks/navboost-signals.ts +395 -0
  58. package/src/audit/checks/on-page.ts +209 -0
  59. package/src/audit/checks/page-resources.ts +285 -0
  60. package/src/audit/checks/pagination.ts +180 -0
  61. package/src/audit/checks/performance.ts +153 -0
  62. package/src/audit/checks/platform-presence.ts +580 -0
  63. package/src/audit/checks/redirect-analysis.ts +153 -0
  64. package/src/audit/checks/redirect-chain.ts +389 -0
  65. package/src/audit/checks/resource-hints.ts +420 -0
  66. package/src/audit/checks/responsive-css.ts +247 -0
  67. package/src/audit/checks/responsive-images.ts +396 -0
  68. package/src/audit/checks/review-ecosystem.ts +415 -0
  69. package/src/audit/checks/robots-validation.ts +373 -0
  70. package/src/audit/checks/security-headers.ts +172 -0
  71. package/src/audit/checks/security.ts +144 -0
  72. package/src/audit/checks/serp-preview.ts +251 -0
  73. package/src/audit/checks/site-maturity.ts +444 -0
  74. package/src/audit/checks/social-meta.test.ts +275 -0
  75. package/src/audit/checks/social-meta.ts +134 -0
  76. package/src/audit/checks/soft-404.ts +151 -0
  77. package/src/audit/checks/structured-data.ts +238 -0
  78. package/src/audit/checks/tech-detection.ts +496 -0
  79. package/src/audit/checks/topical-clusters.ts +435 -0
  80. package/src/audit/checks/tracker-bloat.ts +462 -0
  81. package/src/audit/checks/tracking-verification.test.ts +371 -0
  82. package/src/audit/checks/tracking-verification.ts +636 -0
  83. package/src/audit/checks/url-safety.ts +682 -0
  84. package/src/audit/deno-entry.ts +66 -0
  85. package/src/audit/discovery/index.ts +15 -0
  86. package/src/audit/discovery/link-crawler.ts +232 -0
  87. package/src/audit/discovery/repo-routes.ts +347 -0
  88. package/src/audit/engine.ts +620 -0
  89. package/src/audit/fixes/index.ts +209 -0
  90. package/src/audit/fixes/social-meta-fixes.test.ts +329 -0
  91. package/src/audit/fixes/social-meta-fixes.ts +463 -0
  92. package/src/audit/index.ts +74 -0
  93. package/src/audit/runner.test.ts +299 -0
  94. package/src/audit/runner.ts +130 -0
  95. package/src/audit/types.ts +1953 -0
  96. package/src/content/featured-snippet.ts +367 -0
  97. package/src/content/generator.test.ts +534 -0
  98. package/src/content/generator.ts +501 -0
  99. package/src/content/headline.ts +317 -0
  100. package/src/content/index.ts +62 -0
  101. package/src/content/intent.ts +258 -0
  102. package/src/content/keyword-density.ts +349 -0
  103. package/src/content/readability.ts +262 -0
  104. package/src/executor.ts +336 -0
  105. package/src/fixer.ts +416 -0
  106. package/src/frameworks/detector.test.ts +248 -0
  107. package/src/frameworks/detector.ts +371 -0
  108. package/src/frameworks/index.ts +68 -0
  109. package/src/frameworks/recipes/angular.yaml +171 -0
  110. package/src/frameworks/recipes/astro.yaml +206 -0
  111. package/src/frameworks/recipes/django.yaml +180 -0
  112. package/src/frameworks/recipes/laravel.yaml +137 -0
  113. package/src/frameworks/recipes/nextjs.yaml +268 -0
  114. package/src/frameworks/recipes/nuxt.yaml +175 -0
  115. package/src/frameworks/recipes/rails.yaml +188 -0
  116. package/src/frameworks/recipes/react.yaml +202 -0
  117. package/src/frameworks/recipes/sveltekit.yaml +154 -0
  118. package/src/frameworks/recipes/vue.yaml +137 -0
  119. package/src/frameworks/recipes/wordpress.yaml +209 -0
  120. package/src/frameworks/suggestion-engine.ts +320 -0
  121. package/src/geo/geo-content.test.ts +305 -0
  122. package/src/geo/geo-content.ts +266 -0
  123. package/src/geo/geo-history.test.ts +473 -0
  124. package/src/geo/geo-history.ts +433 -0
  125. package/src/geo/geo-tracker.test.ts +359 -0
  126. package/src/geo/geo-tracker.ts +411 -0
  127. package/src/geo/index.ts +10 -0
  128. package/src/git/commit-helper.test.ts +261 -0
  129. package/src/git/commit-helper.ts +329 -0
  130. package/src/git/index.ts +12 -0
  131. package/src/git/pr-helper.test.ts +284 -0
  132. package/src/git/pr-helper.ts +307 -0
  133. package/src/index.ts +66 -0
  134. package/src/keywords/ai-keyword-engine.ts +1062 -0
  135. package/src/keywords/ai-summarizer.ts +387 -0
  136. package/src/keywords/ci-mode.ts +555 -0
  137. package/src/keywords/engine.ts +359 -0
  138. package/src/keywords/index.ts +151 -0
  139. package/src/keywords/llm-judge.ts +357 -0
  140. package/src/keywords/nlp-analysis.ts +706 -0
  141. package/src/keywords/prioritizer.ts +295 -0
  142. package/src/keywords/site-crawler.ts +342 -0
  143. package/src/keywords/sources/autocomplete.ts +139 -0
  144. package/src/keywords/sources/competitive-search.ts +450 -0
  145. package/src/keywords/sources/competitor-analysis.ts +374 -0
  146. package/src/keywords/sources/dataforseo.ts +206 -0
  147. package/src/keywords/sources/free-sources.ts +294 -0
  148. package/src/keywords/sources/gsc.ts +123 -0
  149. package/src/keywords/topic-grouping.ts +327 -0
  150. package/src/keywords/types.ts +144 -0
  151. package/src/keywords/wizard.ts +457 -0
  152. package/src/loader.ts +40 -0
  153. package/src/reports/index.ts +7 -0
  154. package/src/reports/report-generator.test.ts +293 -0
  155. package/src/reports/report-generator.ts +713 -0
  156. package/src/scheduler/alerts.test.ts +458 -0
  157. package/src/scheduler/alerts.ts +328 -0
  158. package/src/scheduler/index.ts +8 -0
  159. package/src/scheduler/scheduled-audit.test.ts +377 -0
  160. package/src/scheduler/scheduled-audit.ts +149 -0
  161. package/src/test/integration-test.ts +325 -0
  162. package/src/tools/analyzer.ts +373 -0
  163. package/src/tools/crawl.ts +293 -0
  164. package/src/tools/files.ts +301 -0
  165. package/src/tools/h1-fixer.ts +249 -0
  166. package/src/tools/index.ts +67 -0
  167. package/src/tracking/github-action.ts +326 -0
  168. package/src/tracking/google-analytics.ts +265 -0
  169. package/src/tracking/index.ts +45 -0
  170. package/src/tracking/report-generator.ts +386 -0
  171. package/src/tracking/search-console.ts +335 -0
  172. package/src/types.ts +134 -0
  173. package/src/utils/http.ts +302 -0
  174. package/src/wasm-adapter.ts +297 -0
  175. package/src/wasm-entry.ts +14 -0
  176. package/tsconfig.json +17 -0
  177. package/tsup.wasm.config.ts +26 -0
  178. package/vitest.config.ts +15 -0
@@ -0,0 +1,265 @@
1
+ // Google Analytics 4 Integration
2
+ // Auto-inject GA4 tracking code into web projects
3
+
4
+ import * as fs from 'fs';
5
+ import * as path from 'path';
6
+
7
+ export interface GA4Config {
8
+ measurementId: string; // G-XXXXXXXXXX
9
+ }
10
+
11
+ export interface InjectionResult {
12
+ success: boolean;
13
+ file?: string;
14
+ message: string;
15
+ code?: string;
16
+ }
17
+
18
+ /**
19
+ * Generate GA4 tracking script
20
+ */
21
+ export function generateGA4Script(config: GA4Config): string {
22
+ return `<!-- Google Analytics 4 -->
23
+ <script async src="https://www.googletagmanager.com/gtag/js?id=${config.measurementId}"></script>
24
+ <script>
25
+ window.dataLayer = window.dataLayer || [];
26
+ function gtag(){dataLayer.push(arguments);}
27
+ gtag('js', new Date());
28
+ gtag('config', '${config.measurementId}');
29
+ </script>`;
30
+ }
31
+
32
+ /**
33
+ * Generate GA4 for React/Next.js (component-based)
34
+ */
35
+ export function generateGA4ReactComponent(config: GA4Config): string {
36
+ return `// components/GoogleAnalytics.tsx
37
+ import Script from 'next/script';
38
+
39
+ export function GoogleAnalytics() {
40
+ const measurementId = '${config.measurementId}';
41
+
42
+ return (
43
+ <>
44
+ <Script
45
+ src={\`https://www.googletagmanager.com/gtag/js?id=\${measurementId}\`}
46
+ strategy="afterInteractive"
47
+ />
48
+ <Script id="google-analytics" strategy="afterInteractive">
49
+ {\`
50
+ window.dataLayer = window.dataLayer || [];
51
+ function gtag(){dataLayer.push(arguments);}
52
+ gtag('js', new Date());
53
+ gtag('config', '\${measurementId}');
54
+ \`}
55
+ </Script>
56
+ </>
57
+ );
58
+ }`;
59
+ }
60
+
61
+ /**
62
+ * Generate GA4 for Vite/vanilla (index.html injection)
63
+ */
64
+ export function generateGA4ViteScript(config: GA4Config): string {
65
+ return ` <!-- Google Analytics 4 -->
66
+ <script async src="https://www.googletagmanager.com/gtag/js?id=${config.measurementId}"></script>
67
+ <script>
68
+ window.dataLayer = window.dataLayer || [];
69
+ function gtag(){dataLayer.push(arguments);}
70
+ gtag('js', new Date());
71
+ gtag('config', '${config.measurementId}');
72
+ </script>`;
73
+ }
74
+
75
+ /**
76
+ * Simple framework detection for tracking injection
77
+ */
78
+ function detectFrameworkSimple(projectPath: string): string {
79
+ // Check for Next.js
80
+ if (fs.existsSync(path.join(projectPath, 'next.config.js')) ||
81
+ fs.existsSync(path.join(projectPath, 'next.config.ts')) ||
82
+ fs.existsSync(path.join(projectPath, 'next.config.mjs'))) {
83
+ return 'nextjs';
84
+ }
85
+
86
+ // Check for Vite
87
+ if (fs.existsSync(path.join(projectPath, 'vite.config.js')) ||
88
+ fs.existsSync(path.join(projectPath, 'vite.config.ts'))) {
89
+ return 'vite';
90
+ }
91
+
92
+ // Check for plain HTML
93
+ if (fs.existsSync(path.join(projectPath, 'index.html'))) {
94
+ return 'html';
95
+ }
96
+
97
+ // Check for React (CRA)
98
+ const pkgPath = path.join(projectPath, 'package.json');
99
+ if (fs.existsSync(pkgPath)) {
100
+ try {
101
+ const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'));
102
+ if (pkg.dependencies?.react || pkg.devDependencies?.react) {
103
+ return 'react';
104
+ }
105
+ } catch {
106
+ // Ignore parse errors
107
+ }
108
+ }
109
+
110
+ return 'unknown';
111
+ }
112
+
113
+ /**
114
+ * Auto-detect framework and inject GA4
115
+ */
116
+ export async function injectGA4(projectPath: string, config: GA4Config): Promise<InjectionResult> {
117
+ const framework = detectFrameworkSimple(projectPath);
118
+
119
+ switch (framework) {
120
+ case 'nextjs':
121
+ return injectGA4NextJs(projectPath, config);
122
+ case 'vite':
123
+ case 'react':
124
+ return injectGA4Vite(projectPath, config);
125
+ case 'html':
126
+ return injectGA4Html(projectPath, config);
127
+ default:
128
+ return {
129
+ success: false,
130
+ message: `Unsupported framework: ${framework}. Please manually add GA4.`,
131
+ code: generateGA4Script(config),
132
+ };
133
+ }
134
+ }
135
+
136
+ async function injectGA4NextJs(projectPath: string, config: GA4Config): Promise<InjectionResult> {
137
+ // Check for app directory (App Router) or pages directory (Pages Router)
138
+ const appLayoutPath = path.join(projectPath, 'app', 'layout.tsx');
139
+ const pagesAppPath = path.join(projectPath, 'pages', '_app.tsx');
140
+ const srcAppLayoutPath = path.join(projectPath, 'src', 'app', 'layout.tsx');
141
+ const srcPagesAppPath = path.join(projectPath, 'src', 'pages', '_app.tsx');
142
+
143
+ // Try App Router first
144
+ for (const layoutPath of [appLayoutPath, srcAppLayoutPath]) {
145
+ if (fs.existsSync(layoutPath)) {
146
+ const content = fs.readFileSync(layoutPath, 'utf-8');
147
+
148
+ // Check if GA4 already exists
149
+ if (content.includes('googletagmanager.com/gtag')) {
150
+ return { success: true, file: layoutPath, message: 'GA4 already installed' };
151
+ }
152
+
153
+ // Create GoogleAnalytics component
154
+ const componentsDir = path.join(path.dirname(layoutPath), '..', 'components');
155
+ if (!fs.existsSync(componentsDir)) {
156
+ fs.mkdirSync(componentsDir, { recursive: true });
157
+ }
158
+
159
+ const gaComponentPath = path.join(componentsDir, 'GoogleAnalytics.tsx');
160
+ fs.writeFileSync(gaComponentPath, generateGA4ReactComponent(config));
161
+
162
+ // Update layout to import and use component
163
+ const importStatement = `import { GoogleAnalytics } from '@/components/GoogleAnalytics';\n`;
164
+ const componentUsage = '<GoogleAnalytics />';
165
+
166
+ // Add import at top
167
+ let newContent = content;
168
+ if (!newContent.includes('GoogleAnalytics')) {
169
+ // Find first import or add at top
170
+ const importMatch = newContent.match(/^import .+$/m);
171
+ if (importMatch) {
172
+ newContent = newContent.replace(importMatch[0], importMatch[0] + '\n' + importStatement);
173
+ } else {
174
+ newContent = importStatement + newContent;
175
+ }
176
+
177
+ // Add component inside <head> or <body>
178
+ if (newContent.includes('<head>')) {
179
+ newContent = newContent.replace('<head>', `<head>\n ${componentUsage}`);
180
+ } else if (newContent.includes('<body')) {
181
+ newContent = newContent.replace(/<body([^>]*)>/, `<body$1>\n ${componentUsage}`);
182
+ }
183
+
184
+ fs.writeFileSync(layoutPath, newContent);
185
+ }
186
+
187
+ return {
188
+ success: true,
189
+ file: layoutPath,
190
+ message: `GA4 component created at ${gaComponentPath} and imported in layout`,
191
+ };
192
+ }
193
+ }
194
+
195
+ // Try Pages Router
196
+ for (const appPath of [pagesAppPath, srcPagesAppPath]) {
197
+ if (fs.existsSync(appPath)) {
198
+ return {
199
+ success: false,
200
+ file: appPath,
201
+ message: 'Pages Router detected. Please manually add GA4 to _app.tsx',
202
+ code: generateGA4ReactComponent(config),
203
+ };
204
+ }
205
+ }
206
+
207
+ return {
208
+ success: false,
209
+ message: 'Could not find Next.js layout or _app file',
210
+ code: generateGA4ReactComponent(config),
211
+ };
212
+ }
213
+
214
+ async function injectGA4Vite(projectPath: string, config: GA4Config): Promise<InjectionResult> {
215
+ // Look for index.html
216
+ const indexPaths = [
217
+ path.join(projectPath, 'index.html'),
218
+ path.join(projectPath, 'public', 'index.html'),
219
+ path.join(projectPath, 'src', 'index.html'),
220
+ ];
221
+
222
+ for (const indexPath of indexPaths) {
223
+ if (fs.existsSync(indexPath)) {
224
+ let content = fs.readFileSync(indexPath, 'utf-8');
225
+
226
+ // Check if already installed
227
+ if (content.includes('googletagmanager.com/gtag')) {
228
+ return { success: true, file: indexPath, message: 'GA4 already installed' };
229
+ }
230
+
231
+ // Inject before </head>
232
+ const ga4Script = generateGA4ViteScript(config);
233
+ content = content.replace('</head>', `${ga4Script}\n </head>`);
234
+
235
+ fs.writeFileSync(indexPath, content);
236
+ return {
237
+ success: true,
238
+ file: indexPath,
239
+ message: `GA4 injected into ${indexPath}`,
240
+ };
241
+ }
242
+ }
243
+
244
+ return {
245
+ success: false,
246
+ message: 'Could not find index.html',
247
+ code: generateGA4Script(config),
248
+ };
249
+ }
250
+
251
+ async function injectGA4Html(projectPath: string, config: GA4Config): Promise<InjectionResult> {
252
+ return injectGA4Vite(projectPath, config); // Same logic
253
+ }
254
+
255
+ /**
256
+ * Generate environment variable template for GA4
257
+ */
258
+ export function generateGA4EnvTemplate(): string {
259
+ return `# Google Analytics 4
260
+ # Get your Measurement ID from: https://analytics.google.com
261
+ # Go to: Admin > Data Streams > Your Stream > Measurement ID
262
+ VITE_GA4_MEASUREMENT_ID=G-XXXXXXXXXX
263
+ # or for Next.js:
264
+ NEXT_PUBLIC_GA4_MEASUREMENT_ID=G-XXXXXXXXXX`;
265
+ }
@@ -0,0 +1,45 @@
1
+ // Tracking & Monitoring Module
2
+ // Google Analytics, Search Console integration, and automated reporting
3
+
4
+ // Google Analytics 4
5
+ export {
6
+ generateGA4Script,
7
+ generateGA4ReactComponent,
8
+ generateGA4ViteScript,
9
+ generateGA4EnvTemplate,
10
+ injectGA4,
11
+ type GA4Config,
12
+ type InjectionResult,
13
+ } from './google-analytics.js';
14
+
15
+ // Google Search Console
16
+ export {
17
+ generateGSCVerificationTag,
18
+ injectGSCVerification,
19
+ buildGSCApiRequest,
20
+ parseGSCResponse,
21
+ identifyQuickWins,
22
+ comparePeriods,
23
+ getGSCSetupInstructions,
24
+ type GSCConfig,
25
+ type GSCCredentials,
26
+ type GSCQueryResult,
27
+ type GSCPerformanceData,
28
+ type QuickWin,
29
+ } from './search-console.js';
30
+
31
+ // GitHub Action
32
+ export {
33
+ generateWorkflow,
34
+ generateSecretsDoc,
35
+ generateGitHubActionSetup,
36
+ writeGitHubActionFiles,
37
+ type WorkflowConfig,
38
+ } from './github-action.js';
39
+
40
+ // Report Generator
41
+ export {
42
+ generateMarkdownReport,
43
+ generateJsonReport,
44
+ type ReportData,
45
+ } from './report-generator.js';
@@ -0,0 +1,386 @@
1
+ // SEO Report Generator
2
+ // Creates formatted reports for GitHub Issues and dashboards
3
+
4
+ import type { AuditReport, AuditIssue } from '../audit/index.js';
5
+ import type { GSCPerformanceData, QuickWin } from './search-console.js';
6
+
7
+ export interface ReportData {
8
+ siteUrl: string;
9
+ generatedAt: string;
10
+ audit?: AuditReport;
11
+ tracking?: {
12
+ current: GSCPerformanceData;
13
+ previous?: GSCPerformanceData;
14
+ quickWins: QuickWin[];
15
+ };
16
+ comparison?: {
17
+ improved: any[];
18
+ declined: any[];
19
+ newKeywords: any[];
20
+ lostKeywords: any[];
21
+ summary: {
22
+ clicksChange: number;
23
+ impressionsChange: number;
24
+ avgPositionChange: number;
25
+ };
26
+ };
27
+ }
28
+
29
+ /**
30
+ * Generate comprehensive markdown report
31
+ */
32
+ export function generateMarkdownReport(data: ReportData): string {
33
+ const lines: string[] = [];
34
+ const date = new Date(data.generatedAt).toLocaleDateString('en-US', {
35
+ weekday: 'long',
36
+ year: 'numeric',
37
+ month: 'long',
38
+ day: 'numeric',
39
+ });
40
+
41
+ lines.push(`# 📊 SEO Report: ${data.siteUrl}`);
42
+ lines.push(`> Generated: ${date}`);
43
+ lines.push('');
44
+
45
+ // Executive Summary
46
+ lines.push('## 📋 Executive Summary');
47
+ lines.push('');
48
+ lines.push(generateExecutiveSummary(data));
49
+ lines.push('');
50
+
51
+ // Health Score (if audit data)
52
+ if (data.audit) {
53
+ lines.push('## 🏥 Site Health');
54
+ lines.push('');
55
+ lines.push(generateHealthSection(data.audit));
56
+ lines.push('');
57
+ }
58
+
59
+ // Traffic & Rankings (if tracking data)
60
+ if (data.tracking) {
61
+ lines.push('## 📈 Traffic & Rankings');
62
+ lines.push('');
63
+ lines.push(generateTrackingSection(data.tracking, data.comparison));
64
+ lines.push('');
65
+ }
66
+
67
+ // Quick Wins
68
+ if (data.tracking?.quickWins && data.tracking.quickWins.length > 0) {
69
+ lines.push('## 🎯 Quick Wins');
70
+ lines.push('');
71
+ lines.push(generateQuickWinsSection(data.tracking.quickWins));
72
+ lines.push('');
73
+ }
74
+
75
+ // Issues to Fix (if audit data)
76
+ if (data.audit && data.audit.issues.length > 0) {
77
+ lines.push('## 🔧 Issues to Fix');
78
+ lines.push('');
79
+ lines.push(generateIssuesSection(data.audit));
80
+ lines.push('');
81
+ }
82
+
83
+ // Action Items
84
+ lines.push('## ✅ Recommended Actions');
85
+ lines.push('');
86
+ lines.push(generateActionItems(data));
87
+ lines.push('');
88
+
89
+ // Footer
90
+ lines.push('---');
91
+ lines.push('');
92
+ lines.push('*Generated by [SEO Autopilot](https://github.com/seo-autopilot) 🤖*');
93
+
94
+ return lines.join('\n');
95
+ }
96
+
97
+ function generateExecutiveSummary(data: ReportData): string {
98
+ const lines: string[] = [];
99
+
100
+ if (data.audit) {
101
+ const score = data.audit.healthScore.overall;
102
+ const emoji = score >= 80 ? '🟢' : score >= 60 ? '🟡' : '🔴';
103
+ const totalIssues = data.audit.summary.errors + data.audit.summary.warnings + data.audit.summary.notices;
104
+ const totalChecks = totalIssues + data.audit.summary.passed;
105
+ lines.push(`| Metric | Value |`);
106
+ lines.push(`|--------|-------|`);
107
+ lines.push(`| ${emoji} Health Score | **${score}/100** |`);
108
+ lines.push(`| Issues Found | ${data.audit.issues.length} |`);
109
+ lines.push(`| Checks Passed | ${data.audit.summary.passed}/${totalChecks} |`);
110
+ }
111
+
112
+ if (data.tracking) {
113
+ if (!data.audit) {
114
+ lines.push(`| Metric | Value |`);
115
+ lines.push(`|--------|-------|`);
116
+ }
117
+ lines.push(`| Total Clicks | ${data.tracking.current.totalClicks.toLocaleString()} |`);
118
+ lines.push(`| Total Impressions | ${data.tracking.current.totalImpressions.toLocaleString()} |`);
119
+ lines.push(`| Avg Position | ${data.tracking.current.avgPosition.toFixed(1)} |`);
120
+ lines.push(`| Avg CTR | ${(data.tracking.current.avgCtr * 100).toFixed(2)}% |`);
121
+ }
122
+
123
+ if (data.comparison) {
124
+ lines.push('');
125
+ lines.push('### Week-over-Week Changes');
126
+ lines.push('');
127
+
128
+ const clicksChange = data.comparison.summary.clicksChange;
129
+ const impressionsChange = data.comparison.summary.impressionsChange;
130
+ const positionChange = data.comparison.summary.avgPositionChange;
131
+
132
+ const clicksEmoji = clicksChange > 0 ? '📈' : clicksChange < 0 ? '📉' : '➡️';
133
+ const impressionsEmoji = impressionsChange > 0 ? '📈' : impressionsChange < 0 ? '📉' : '➡️';
134
+ const positionEmoji = positionChange < 0 ? '📈' : positionChange > 0 ? '📉' : '➡️'; // Lower is better
135
+
136
+ lines.push(`- ${clicksEmoji} Clicks: ${clicksChange >= 0 ? '+' : ''}${clicksChange.toLocaleString()}`);
137
+ lines.push(`- ${impressionsEmoji} Impressions: ${impressionsChange >= 0 ? '+' : ''}${impressionsChange.toLocaleString()}`);
138
+ lines.push(`- ${positionEmoji} Avg Position: ${positionChange >= 0 ? '+' : ''}${positionChange.toFixed(1)}`);
139
+ }
140
+
141
+ return lines.join('\n');
142
+ }
143
+
144
+ function generateHealthSection(audit: AuditReport): string {
145
+ const lines: string[] = [];
146
+
147
+ // Visual score bar
148
+ const score = audit.healthScore.overall;
149
+ const filled = Math.round(score / 10);
150
+ const empty = 10 - filled;
151
+ const bar = '█'.repeat(filled) + '░'.repeat(empty);
152
+
153
+ lines.push(`### Score: ${score}/100`);
154
+ lines.push('');
155
+ lines.push(`\`${bar}\``);
156
+ lines.push('');
157
+
158
+ // Category breakdown
159
+ const categories = new Map<string, { passed: number; total: number }>();
160
+
161
+ for (const issue of audit.issues) {
162
+ const cat = issue.category;
163
+ if (!categories.has(cat)) {
164
+ categories.set(cat, { passed: 0, total: 0 });
165
+ }
166
+ categories.get(cat)!.total++;
167
+ }
168
+
169
+ if (categories.size > 0) {
170
+ lines.push('| Category | Issues |');
171
+ lines.push('|----------|--------|');
172
+
173
+ for (const [category, counts] of categories) {
174
+ const icon = counts.total === 0 ? '✅' : counts.total <= 2 ? '⚠️' : '❌';
175
+ lines.push(`| ${icon} ${category} | ${counts.total} |`);
176
+ }
177
+ }
178
+
179
+ return lines.join('\n');
180
+ }
181
+
182
+ function generateTrackingSection(
183
+ tracking: ReportData['tracking'],
184
+ comparison?: ReportData['comparison']
185
+ ): string {
186
+ if (!tracking) return '';
187
+
188
+ const lines: string[] = [];
189
+
190
+ // Top performing queries
191
+ lines.push('### Top Performing Queries');
192
+ lines.push('');
193
+ lines.push('| Query | Clicks | Impressions | Position | CTR |');
194
+ lines.push('|-------|--------|-------------|----------|-----|');
195
+
196
+ const topQueries = tracking.current.queries
197
+ .sort((a, b) => b.clicks - a.clicks)
198
+ .slice(0, 10);
199
+
200
+ for (const q of topQueries) {
201
+ lines.push(`| ${truncate(q.query, 40)} | ${q.clicks} | ${q.impressions} | ${q.position.toFixed(1)} | ${(q.ctr * 100).toFixed(1)}% |`);
202
+ }
203
+
204
+ // Comparison data
205
+ if (comparison) {
206
+ if (comparison.improved.length > 0) {
207
+ lines.push('');
208
+ lines.push('### 📈 Improved Rankings');
209
+ lines.push('');
210
+ for (const q of comparison.improved.slice(0, 5)) {
211
+ lines.push(`- **${q.query}**: Moved up to position ${q.position.toFixed(1)}`);
212
+ }
213
+ }
214
+
215
+ if (comparison.declined.length > 0) {
216
+ lines.push('');
217
+ lines.push('### 📉 Declined Rankings');
218
+ lines.push('');
219
+ for (const q of comparison.declined.slice(0, 5)) {
220
+ lines.push(`- **${q.query}**: Dropped to position ${q.position.toFixed(1)}`);
221
+ }
222
+ }
223
+
224
+ if (comparison.newKeywords.length > 0) {
225
+ lines.push('');
226
+ lines.push('### 🆕 New Keywords');
227
+ lines.push('');
228
+ for (const q of comparison.newKeywords.slice(0, 5)) {
229
+ lines.push(`- **${q.query}**: Position ${q.position.toFixed(1)} (${q.impressions} impressions)`);
230
+ }
231
+ }
232
+ }
233
+
234
+ return lines.join('\n');
235
+ }
236
+
237
+ function generateQuickWinsSection(quickWins: QuickWin[]): string {
238
+ const lines: string[] = [];
239
+
240
+ lines.push('These are high-impact opportunities requiring minimal effort:');
241
+ lines.push('');
242
+
243
+ for (const win of quickWins.slice(0, 10)) {
244
+ const typeIcon = win.opportunity === 'page-1-close' ? '🎯' :
245
+ win.opportunity === 'low-ctr' ? '📝' : '⬆️';
246
+
247
+ lines.push(`### ${typeIcon} ${truncate(win.query, 50)}`);
248
+ lines.push('');
249
+ lines.push(`- **Current Position**: ${win.currentPosition.toFixed(1)}`);
250
+ lines.push(`- **Impressions**: ${win.impressions.toLocaleString()}/month`);
251
+ lines.push(`- **Current CTR**: ${(win.ctr * 100).toFixed(1)}%`);
252
+ lines.push(`- **Potential Gain**: +${win.potentialGain} clicks/month`);
253
+ lines.push(`- **Action**: ${win.suggestedAction}`);
254
+ lines.push('');
255
+ }
256
+
257
+ return lines.join('\n');
258
+ }
259
+
260
+ function generateIssuesSection(audit: AuditReport): string {
261
+ const lines: string[] = [];
262
+
263
+ // Group by severity
264
+ const errors = audit.issues.filter((i: AuditIssue) => i.severity === 'error');
265
+ const warnings = audit.issues.filter((i: AuditIssue) => i.severity === 'warning');
266
+ const notices = audit.issues.filter((i: AuditIssue) => i.severity === 'notice');
267
+
268
+ if (errors.length > 0) {
269
+ lines.push('### ❌ Errors (Fix Immediately)');
270
+ lines.push('');
271
+ for (const issue of errors.slice(0, 10)) {
272
+ lines.push(`- **${issue.code}**: ${issue.title}`);
273
+ if (issue.affectedUrls && issue.affectedUrls.length > 0) {
274
+ lines.push(` - URL: \`${issue.affectedUrls[0]}\``);
275
+ }
276
+ }
277
+ lines.push('');
278
+ }
279
+
280
+ if (warnings.length > 0) {
281
+ lines.push('### ⚠️ Warnings (Should Fix)');
282
+ lines.push('');
283
+ for (const issue of warnings.slice(0, 10)) {
284
+ lines.push(`- **${issue.code}**: ${issue.title}`);
285
+ }
286
+ lines.push('');
287
+ }
288
+
289
+ if (notices.length > 0) {
290
+ lines.push('<details>');
291
+ lines.push('<summary>📝 Notices (Consider Fixing) - ' + notices.length + ' items</summary>');
292
+ lines.push('');
293
+ for (const issue of notices.slice(0, 20)) {
294
+ lines.push(`- ${issue.code}: ${issue.title}`);
295
+ }
296
+ lines.push('</details>');
297
+ lines.push('');
298
+ }
299
+
300
+ return lines.join('\n');
301
+ }
302
+
303
+ function generateActionItems(data: ReportData): string {
304
+ const lines: string[] = [];
305
+ const actions: string[] = [];
306
+
307
+ // From audit issues
308
+ if (data.audit) {
309
+ const errors = data.audit.issues.filter((i: AuditIssue) => i.severity === 'error');
310
+ if (errors.length > 0) {
311
+ actions.push(`Fix ${errors.length} critical SEO errors`);
312
+ }
313
+
314
+ const missingMeta = data.audit.issues.filter((i: AuditIssue) =>
315
+ i.code.includes('TITLE') || i.code.includes('META')
316
+ );
317
+ if (missingMeta.length > 0) {
318
+ actions.push(`Add missing meta tags on ${missingMeta.length} pages`);
319
+ }
320
+ }
321
+
322
+ // From quick wins
323
+ if (data.tracking?.quickWins) {
324
+ const pageOneClose = data.tracking.quickWins.filter(w => w.opportunity === 'page-1-close');
325
+ if (pageOneClose.length > 0) {
326
+ actions.push(`Optimize ${pageOneClose.length} keywords that are almost on page 1`);
327
+ }
328
+
329
+ const lowCtr = data.tracking.quickWins.filter(w => w.opportunity === 'low-ctr');
330
+ if (lowCtr.length > 0) {
331
+ actions.push(`Improve titles/descriptions for ${lowCtr.length} low-CTR pages`);
332
+ }
333
+ }
334
+
335
+ // From comparison
336
+ if (data.comparison?.declined && data.comparison.declined.length > 0) {
337
+ actions.push(`Investigate ${data.comparison.declined.length} keywords with declining rankings`);
338
+ }
339
+
340
+ if (actions.length === 0) {
341
+ actions.push('Continue monitoring - no urgent actions needed');
342
+ }
343
+
344
+ lines.push('Priority order:');
345
+ lines.push('');
346
+ for (let i = 0; i < actions.length; i++) {
347
+ lines.push(`${i + 1}. [ ] ${actions[i]}`);
348
+ }
349
+
350
+ return lines.join('\n');
351
+ }
352
+
353
+ function truncate(str: string, maxLength: number): string {
354
+ if (str.length <= maxLength) return str;
355
+ return str.slice(0, maxLength - 3) + '...';
356
+ }
357
+
358
+ /**
359
+ * Generate JSON report for API/dashboard
360
+ */
361
+ export function generateJsonReport(data: ReportData): object {
362
+ return {
363
+ meta: {
364
+ siteUrl: data.siteUrl,
365
+ generatedAt: data.generatedAt,
366
+ version: '1.0',
367
+ },
368
+ health: data.audit ? {
369
+ score: data.audit.healthScore.overall,
370
+ issueCount: data.audit.issues.length,
371
+ passed: data.audit.summary.passed,
372
+ errors: data.audit.summary.errors,
373
+ warnings: data.audit.summary.warnings,
374
+ notices: data.audit.summary.notices,
375
+ } : null,
376
+ traffic: data.tracking ? {
377
+ clicks: data.tracking.current.totalClicks,
378
+ impressions: data.tracking.current.totalImpressions,
379
+ avgPosition: data.tracking.current.avgPosition,
380
+ avgCtr: data.tracking.current.avgCtr,
381
+ } : null,
382
+ quickWins: data.tracking?.quickWins || [],
383
+ issues: data.audit?.issues || [],
384
+ comparison: data.comparison || null,
385
+ };
386
+ }