@pagebridge/core 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.
- package/.turbo/turbo-build.log +2 -0
- package/LICENSE +21 -0
- package/README.md +241 -0
- package/dist/decay-detector.d.ts +37 -0
- package/dist/decay-detector.d.ts.map +1 -0
- package/dist/decay-detector.js +175 -0
- package/dist/gsc-client.d.ts +40 -0
- package/dist/gsc-client.d.ts.map +1 -0
- package/dist/gsc-client.js +76 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +5 -0
- package/dist/sync-engine.d.ts +41 -0
- package/dist/sync-engine.d.ts.map +1 -0
- package/dist/sync-engine.js +333 -0
- package/dist/task-generator.d.ts +26 -0
- package/dist/task-generator.d.ts.map +1 -0
- package/dist/task-generator.js +101 -0
- package/dist/url-matcher.d.ts +49 -0
- package/dist/url-matcher.d.ts.map +1 -0
- package/dist/url-matcher.js +227 -0
- package/eslint.config.js +3 -0
- package/package.json +33 -0
- package/src/decay-detector.ts +254 -0
- package/src/gsc-client.ts +126 -0
- package/src/index.ts +31 -0
- package/src/sync-engine.ts +485 -0
- package/src/task-generator.ts +160 -0
- package/src/url-matcher.ts +307 -0
- package/tsconfig.json +9 -0
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
export class URLMatcher {
|
|
2
|
+
sanityClient;
|
|
3
|
+
config;
|
|
4
|
+
constructor(sanityClient, config) {
|
|
5
|
+
this.sanityClient = sanityClient;
|
|
6
|
+
this.config = config;
|
|
7
|
+
}
|
|
8
|
+
async matchUrls(gscUrls) {
|
|
9
|
+
const query = `*[_type in $types]{
|
|
10
|
+
_id,
|
|
11
|
+
_type,
|
|
12
|
+
"${this.config.slugField}": ${this.config.slugField}.current,
|
|
13
|
+
_createdAt
|
|
14
|
+
}`;
|
|
15
|
+
const documents = await this.sanityClient.fetch(query, {
|
|
16
|
+
types: this.config.contentTypes,
|
|
17
|
+
});
|
|
18
|
+
const slugToDoc = new Map();
|
|
19
|
+
const allSlugs = [];
|
|
20
|
+
for (const doc of documents) {
|
|
21
|
+
const slug = doc[this.config.slugField];
|
|
22
|
+
if (slug) {
|
|
23
|
+
const normalized = this.normalizeSlug(slug);
|
|
24
|
+
slugToDoc.set(normalized, doc);
|
|
25
|
+
allSlugs.push(normalized);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return gscUrls.map((url) => this.matchSingleUrl(url, slugToDoc, allSlugs));
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Get all available slugs from Sanity for diagnostic purposes
|
|
32
|
+
*/
|
|
33
|
+
async getAvailableSlugs() {
|
|
34
|
+
const query = `*[_type in $types]{
|
|
35
|
+
"${this.config.slugField}": ${this.config.slugField}.current
|
|
36
|
+
}`;
|
|
37
|
+
const documents = await this.sanityClient.fetch(query, {
|
|
38
|
+
types: this.config.contentTypes,
|
|
39
|
+
});
|
|
40
|
+
return documents
|
|
41
|
+
.map((doc) => doc[this.config.slugField])
|
|
42
|
+
.filter((slug) => !!slug)
|
|
43
|
+
.map((slug) => this.normalizeSlug(slug));
|
|
44
|
+
}
|
|
45
|
+
matchSingleUrl(gscUrl, slugToDoc, allSlugs) {
|
|
46
|
+
const normalized = this.normalizeUrl(gscUrl);
|
|
47
|
+
const extractionResult = this.extractSlugWithDiagnostics(normalized);
|
|
48
|
+
// Check if URL is outside path prefix
|
|
49
|
+
if (extractionResult.outsidePrefix) {
|
|
50
|
+
return {
|
|
51
|
+
gscUrl,
|
|
52
|
+
sanityId: undefined,
|
|
53
|
+
confidence: "none",
|
|
54
|
+
unmatchReason: "outside_path_prefix",
|
|
55
|
+
diagnostics: {
|
|
56
|
+
normalizedUrl: normalized,
|
|
57
|
+
pathAfterPrefix: null,
|
|
58
|
+
configuredPrefix: this.config.pathPrefix ?? null,
|
|
59
|
+
availableSlugsCount: slugToDoc.size,
|
|
60
|
+
similarSlugs: [],
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
const slug = extractionResult.slug;
|
|
65
|
+
if (!slug) {
|
|
66
|
+
return {
|
|
67
|
+
gscUrl,
|
|
68
|
+
sanityId: undefined,
|
|
69
|
+
confidence: "none",
|
|
70
|
+
unmatchReason: "no_slug_extracted",
|
|
71
|
+
diagnostics: {
|
|
72
|
+
normalizedUrl: normalized,
|
|
73
|
+
pathAfterPrefix: extractionResult.pathAfterPrefix,
|
|
74
|
+
configuredPrefix: this.config.pathPrefix ?? null,
|
|
75
|
+
availableSlugsCount: slugToDoc.size,
|
|
76
|
+
similarSlugs: [],
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
const exactMatch = slugToDoc.get(slug);
|
|
81
|
+
if (exactMatch) {
|
|
82
|
+
return {
|
|
83
|
+
gscUrl,
|
|
84
|
+
sanityId: exactMatch._id,
|
|
85
|
+
confidence: "exact",
|
|
86
|
+
matchedSlug: slug,
|
|
87
|
+
unmatchReason: "matched",
|
|
88
|
+
extractedSlug: slug,
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
const withoutTrailing = slug.replace(/\/$/, "");
|
|
92
|
+
const trailingMatch = slugToDoc.get(withoutTrailing);
|
|
93
|
+
if (trailingMatch) {
|
|
94
|
+
return {
|
|
95
|
+
gscUrl,
|
|
96
|
+
sanityId: trailingMatch._id,
|
|
97
|
+
confidence: "normalized",
|
|
98
|
+
matchedSlug: withoutTrailing,
|
|
99
|
+
unmatchReason: "matched",
|
|
100
|
+
extractedSlug: slug,
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
const withTrailing = slug + "/";
|
|
104
|
+
const addedTrailingMatch = slugToDoc.get(withTrailing);
|
|
105
|
+
if (addedTrailingMatch) {
|
|
106
|
+
return {
|
|
107
|
+
gscUrl,
|
|
108
|
+
sanityId: addedTrailingMatch._id,
|
|
109
|
+
confidence: "normalized",
|
|
110
|
+
matchedSlug: withTrailing,
|
|
111
|
+
unmatchReason: "matched",
|
|
112
|
+
extractedSlug: slug,
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
// No match found - find similar slugs for suggestions
|
|
116
|
+
const similarSlugs = this.findSimilarSlugs(slug, allSlugs, 3);
|
|
117
|
+
return {
|
|
118
|
+
gscUrl,
|
|
119
|
+
sanityId: undefined,
|
|
120
|
+
confidence: "none",
|
|
121
|
+
unmatchReason: "no_matching_document",
|
|
122
|
+
extractedSlug: slug,
|
|
123
|
+
diagnostics: {
|
|
124
|
+
normalizedUrl: normalized,
|
|
125
|
+
pathAfterPrefix: extractionResult.pathAfterPrefix,
|
|
126
|
+
configuredPrefix: this.config.pathPrefix ?? null,
|
|
127
|
+
availableSlugsCount: slugToDoc.size,
|
|
128
|
+
similarSlugs,
|
|
129
|
+
},
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
normalizeUrl(url) {
|
|
133
|
+
try {
|
|
134
|
+
const parsed = new URL(url);
|
|
135
|
+
parsed.hostname = parsed.hostname.replace(/^www\./, "");
|
|
136
|
+
parsed.search = "";
|
|
137
|
+
parsed.hash = "";
|
|
138
|
+
return parsed.toString().toLowerCase();
|
|
139
|
+
}
|
|
140
|
+
catch {
|
|
141
|
+
return url.toLowerCase();
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
extractSlug(normalizedUrl) {
|
|
145
|
+
return this.extractSlugWithDiagnostics(normalizedUrl).slug;
|
|
146
|
+
}
|
|
147
|
+
extractSlugWithDiagnostics(normalizedUrl) {
|
|
148
|
+
try {
|
|
149
|
+
const parsed = new URL(normalizedUrl);
|
|
150
|
+
let path = parsed.pathname;
|
|
151
|
+
// Check if the URL is outside the configured path prefix
|
|
152
|
+
if (this.config.pathPrefix) {
|
|
153
|
+
const prefixRegex = new RegExp(`^${this.escapeRegex(this.config.pathPrefix)}(/|$)`);
|
|
154
|
+
if (!prefixRegex.test(path)) {
|
|
155
|
+
return {
|
|
156
|
+
slug: undefined,
|
|
157
|
+
pathAfterPrefix: null,
|
|
158
|
+
outsidePrefix: true,
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
path = path.replace(new RegExp(`^${this.escapeRegex(this.config.pathPrefix)}`), "");
|
|
162
|
+
}
|
|
163
|
+
const slug = path.replace(/^\/+|\/+$/g, "");
|
|
164
|
+
return {
|
|
165
|
+
slug: slug || undefined,
|
|
166
|
+
pathAfterPrefix: path,
|
|
167
|
+
outsidePrefix: false,
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
catch {
|
|
171
|
+
return {
|
|
172
|
+
slug: undefined,
|
|
173
|
+
pathAfterPrefix: null,
|
|
174
|
+
outsidePrefix: false,
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
escapeRegex(str) {
|
|
179
|
+
return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
180
|
+
}
|
|
181
|
+
normalizeSlug(slug) {
|
|
182
|
+
return slug.replace(/^\/+|\/+$/g, "").toLowerCase();
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Find similar slugs using Levenshtein distance
|
|
186
|
+
*/
|
|
187
|
+
findSimilarSlugs(target, candidates, limit) {
|
|
188
|
+
const scored = candidates
|
|
189
|
+
.map((candidate) => ({
|
|
190
|
+
slug: candidate,
|
|
191
|
+
distance: this.levenshteinDistance(target, candidate),
|
|
192
|
+
}))
|
|
193
|
+
.filter((item) => item.distance <= Math.max(target.length * 0.5, 10)) // Only include reasonably similar
|
|
194
|
+
.sort((a, b) => a.distance - b.distance)
|
|
195
|
+
.slice(0, limit);
|
|
196
|
+
return scored.map((item) => item.slug);
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Calculate Levenshtein distance between two strings
|
|
200
|
+
*/
|
|
201
|
+
levenshteinDistance(a, b) {
|
|
202
|
+
if (a.length === 0)
|
|
203
|
+
return b.length;
|
|
204
|
+
if (b.length === 0)
|
|
205
|
+
return a.length;
|
|
206
|
+
const matrix = [];
|
|
207
|
+
for (let i = 0; i <= b.length; i++) {
|
|
208
|
+
matrix[i] = [i];
|
|
209
|
+
}
|
|
210
|
+
for (let j = 0; j <= a.length; j++) {
|
|
211
|
+
matrix[0][j] = j;
|
|
212
|
+
}
|
|
213
|
+
for (let i = 1; i <= b.length; i++) {
|
|
214
|
+
for (let j = 1; j <= a.length; j++) {
|
|
215
|
+
if (b.charAt(i - 1) === a.charAt(j - 1)) {
|
|
216
|
+
matrix[i][j] = matrix[i - 1][j - 1];
|
|
217
|
+
}
|
|
218
|
+
else {
|
|
219
|
+
matrix[i][j] = Math.min(matrix[i - 1][j - 1] + 1, // substitution
|
|
220
|
+
matrix[i][j - 1] + 1, // insertion
|
|
221
|
+
matrix[i - 1][j] + 1);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
return matrix[b.length][a.length];
|
|
226
|
+
}
|
|
227
|
+
}
|
package/eslint.config.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@pagebridge/core",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"private": false,
|
|
6
|
+
"type": "module",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"import": "./dist/index.js"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"dependencies": {
|
|
14
|
+
"@sanity/client": "^7.14.1",
|
|
15
|
+
"drizzle-orm": "^0.45.1",
|
|
16
|
+
"google-auth-library": "^10.5.0",
|
|
17
|
+
"googleapis": "^171.1.0",
|
|
18
|
+
"@pagebridge/db": "^0.0.1"
|
|
19
|
+
},
|
|
20
|
+
"devDependencies": {
|
|
21
|
+
"@types/node": "^22.15.3",
|
|
22
|
+
"eslint": "^9.39.1",
|
|
23
|
+
"typescript": "^5.9.3",
|
|
24
|
+
"@pagebridge/eslint-config": "^0.0.0",
|
|
25
|
+
"@pagebridge/typescript-config": "^0.0.0"
|
|
26
|
+
},
|
|
27
|
+
"scripts": {
|
|
28
|
+
"build": "tsc",
|
|
29
|
+
"dev": "tsc --watch",
|
|
30
|
+
"lint": "eslint .",
|
|
31
|
+
"check-types": "tsc --noEmit"
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
import type { DrizzleClient } from "@pagebridge/db";
|
|
2
|
+
import { searchAnalytics } from "@pagebridge/db";
|
|
3
|
+
import { and, avg, gte, lte, sql, eq } from "drizzle-orm";
|
|
4
|
+
|
|
5
|
+
export interface DecayRule {
|
|
6
|
+
type: "position_decay" | "low_ctr" | "impressions_drop";
|
|
7
|
+
threshold: number;
|
|
8
|
+
minImpressions: number;
|
|
9
|
+
comparisonWindowDays: number;
|
|
10
|
+
sustainedDays: number;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface QuietPeriodConfig {
|
|
14
|
+
enabled: boolean;
|
|
15
|
+
days: number;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface DecaySignal {
|
|
19
|
+
page: string;
|
|
20
|
+
reason: "position_decay" | "low_ctr" | "impressions_drop";
|
|
21
|
+
severity: "low" | "medium" | "high";
|
|
22
|
+
metrics: {
|
|
23
|
+
positionBefore: number;
|
|
24
|
+
positionNow: number;
|
|
25
|
+
positionDelta: number;
|
|
26
|
+
ctrBefore: number;
|
|
27
|
+
ctrNow: number;
|
|
28
|
+
impressions: number;
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
interface PageMetrics {
|
|
33
|
+
page: string;
|
|
34
|
+
position: number;
|
|
35
|
+
ctr: number;
|
|
36
|
+
impressions: number;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export const defaultRules: DecayRule[] = [
|
|
40
|
+
{
|
|
41
|
+
type: "position_decay",
|
|
42
|
+
threshold: 3,
|
|
43
|
+
minImpressions: 100,
|
|
44
|
+
comparisonWindowDays: 28,
|
|
45
|
+
sustainedDays: 14,
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
type: "low_ctr",
|
|
49
|
+
threshold: 0.01,
|
|
50
|
+
minImpressions: 1000,
|
|
51
|
+
comparisonWindowDays: 28,
|
|
52
|
+
sustainedDays: 7,
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
type: "impressions_drop",
|
|
56
|
+
threshold: 0.5,
|
|
57
|
+
minImpressions: 500,
|
|
58
|
+
comparisonWindowDays: 28,
|
|
59
|
+
sustainedDays: 14,
|
|
60
|
+
},
|
|
61
|
+
];
|
|
62
|
+
|
|
63
|
+
export class DecayDetector {
|
|
64
|
+
constructor(
|
|
65
|
+
private db: DrizzleClient,
|
|
66
|
+
private rules: DecayRule[] = defaultRules,
|
|
67
|
+
) {}
|
|
68
|
+
|
|
69
|
+
async detectDecay(
|
|
70
|
+
siteId: string,
|
|
71
|
+
publishedDates: Map<string, Date>,
|
|
72
|
+
quietPeriod: QuietPeriodConfig = { enabled: true, days: 45 },
|
|
73
|
+
): Promise<DecaySignal[]> {
|
|
74
|
+
const signals: DecaySignal[] = [];
|
|
75
|
+
const now = new Date();
|
|
76
|
+
|
|
77
|
+
for (const rule of this.rules) {
|
|
78
|
+
const currentPeriodEnd = now;
|
|
79
|
+
const currentPeriodStart = daysAgo(rule.sustainedDays);
|
|
80
|
+
const previousPeriodEnd = daysAgo(rule.comparisonWindowDays);
|
|
81
|
+
const previousPeriodStart = daysAgo(
|
|
82
|
+
rule.comparisonWindowDays + rule.sustainedDays,
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
const [currentMetrics, previousMetrics] = await Promise.all([
|
|
86
|
+
this.getAverageMetrics(siteId, currentPeriodStart, currentPeriodEnd),
|
|
87
|
+
this.getAverageMetrics(siteId, previousPeriodStart, previousPeriodEnd),
|
|
88
|
+
]);
|
|
89
|
+
|
|
90
|
+
for (const current of currentMetrics) {
|
|
91
|
+
if (quietPeriod.enabled) {
|
|
92
|
+
const publishDate = publishedDates.get(current.page);
|
|
93
|
+
if (publishDate && daysSince(publishDate) < quietPeriod.days) {
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (current.impressions < rule.minImpressions) continue;
|
|
99
|
+
|
|
100
|
+
const previous = previousMetrics.find((p) => p.page === current.page);
|
|
101
|
+
if (!previous) continue;
|
|
102
|
+
|
|
103
|
+
const signal = this.evaluateRule(rule, current, previous);
|
|
104
|
+
if (signal) signals.push(signal);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return this.deduplicateSignals(signals);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
private async getAverageMetrics(
|
|
112
|
+
siteId: string,
|
|
113
|
+
startDate: Date,
|
|
114
|
+
endDate: Date,
|
|
115
|
+
): Promise<PageMetrics[]> {
|
|
116
|
+
const results = await this.db
|
|
117
|
+
.select({
|
|
118
|
+
page: searchAnalytics.page,
|
|
119
|
+
avgPosition: avg(searchAnalytics.position),
|
|
120
|
+
avgCtr: avg(searchAnalytics.ctr),
|
|
121
|
+
totalImpressions: sql<number>`sum(${searchAnalytics.impressions})`,
|
|
122
|
+
})
|
|
123
|
+
.from(searchAnalytics)
|
|
124
|
+
.where(
|
|
125
|
+
and(
|
|
126
|
+
eq(searchAnalytics.siteId, siteId),
|
|
127
|
+
gte(searchAnalytics.date, formatDate(startDate)),
|
|
128
|
+
lte(searchAnalytics.date, formatDate(endDate)),
|
|
129
|
+
),
|
|
130
|
+
)
|
|
131
|
+
.groupBy(searchAnalytics.page);
|
|
132
|
+
|
|
133
|
+
return results.map((r) => ({
|
|
134
|
+
page: r.page,
|
|
135
|
+
position: Number(r.avgPosition) || 0,
|
|
136
|
+
ctr: Number(r.avgCtr) || 0,
|
|
137
|
+
impressions: Number(r.totalImpressions) || 0,
|
|
138
|
+
}));
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
private evaluateRule(
|
|
142
|
+
rule: DecayRule,
|
|
143
|
+
current: PageMetrics,
|
|
144
|
+
previous: PageMetrics,
|
|
145
|
+
): DecaySignal | undefined {
|
|
146
|
+
switch (rule.type) {
|
|
147
|
+
case "position_decay": {
|
|
148
|
+
const delta = current.position - previous.position;
|
|
149
|
+
if (delta >= rule.threshold) {
|
|
150
|
+
return {
|
|
151
|
+
page: current.page,
|
|
152
|
+
reason: "position_decay",
|
|
153
|
+
severity: this.calculateSeverity(delta, [3, 5, 8]),
|
|
154
|
+
metrics: {
|
|
155
|
+
positionBefore: previous.position,
|
|
156
|
+
positionNow: current.position,
|
|
157
|
+
positionDelta: delta,
|
|
158
|
+
ctrBefore: previous.ctr,
|
|
159
|
+
ctrNow: current.ctr,
|
|
160
|
+
impressions: current.impressions,
|
|
161
|
+
},
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
break;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
case "low_ctr": {
|
|
168
|
+
if (current.ctr < rule.threshold && current.position <= 10) {
|
|
169
|
+
return {
|
|
170
|
+
page: current.page,
|
|
171
|
+
reason: "low_ctr",
|
|
172
|
+
severity: this.calculateSeverity(
|
|
173
|
+
rule.threshold - current.ctr,
|
|
174
|
+
[0.005, 0.01, 0.02],
|
|
175
|
+
),
|
|
176
|
+
metrics: {
|
|
177
|
+
positionBefore: previous.position,
|
|
178
|
+
positionNow: current.position,
|
|
179
|
+
positionDelta: current.position - previous.position,
|
|
180
|
+
ctrBefore: previous.ctr,
|
|
181
|
+
ctrNow: current.ctr,
|
|
182
|
+
impressions: current.impressions,
|
|
183
|
+
},
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
break;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
case "impressions_drop": {
|
|
190
|
+
const dropRatio = 1 - current.impressions / previous.impressions;
|
|
191
|
+
if (dropRatio >= rule.threshold) {
|
|
192
|
+
return {
|
|
193
|
+
page: current.page,
|
|
194
|
+
reason: "impressions_drop",
|
|
195
|
+
severity: this.calculateSeverity(dropRatio, [0.3, 0.5, 0.7]),
|
|
196
|
+
metrics: {
|
|
197
|
+
positionBefore: previous.position,
|
|
198
|
+
positionNow: current.position,
|
|
199
|
+
positionDelta: current.position - previous.position,
|
|
200
|
+
ctrBefore: previous.ctr,
|
|
201
|
+
ctrNow: current.ctr,
|
|
202
|
+
impressions: current.impressions,
|
|
203
|
+
},
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
break;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
return undefined;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
private calculateSeverity(
|
|
214
|
+
value: number,
|
|
215
|
+
thresholds: [number, number, number],
|
|
216
|
+
): "low" | "medium" | "high" {
|
|
217
|
+
if (value >= thresholds[2]) return "high";
|
|
218
|
+
if (value >= thresholds[1]) return "medium";
|
|
219
|
+
return "low";
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
private deduplicateSignals(signals: DecaySignal[]): DecaySignal[] {
|
|
223
|
+
const byPage = new Map<string, DecaySignal>();
|
|
224
|
+
const severityOrder = { high: 3, medium: 2, low: 1 };
|
|
225
|
+
|
|
226
|
+
for (const signal of signals) {
|
|
227
|
+
const existing = byPage.get(signal.page);
|
|
228
|
+
if (
|
|
229
|
+
!existing ||
|
|
230
|
+
severityOrder[signal.severity] > severityOrder[existing.severity]
|
|
231
|
+
) {
|
|
232
|
+
byPage.set(signal.page, signal);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
return Array.from(byPage.values());
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
function daysAgo(days: number): Date {
|
|
241
|
+
const date = new Date();
|
|
242
|
+
date.setDate(date.getDate() - days);
|
|
243
|
+
return date;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
function daysSince(date: Date): number {
|
|
247
|
+
const now = new Date();
|
|
248
|
+
const diff = now.getTime() - date.getTime();
|
|
249
|
+
return Math.floor(diff / (1000 * 60 * 60 * 24));
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
function formatDate(date: Date): string {
|
|
253
|
+
return date.toISOString().split("T")[0]!;
|
|
254
|
+
}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { google, type searchconsole_v1 } from "googleapis";
|
|
2
|
+
import type { JWT } from "google-auth-library";
|
|
3
|
+
|
|
4
|
+
export interface GSCClientOptions {
|
|
5
|
+
credentials: {
|
|
6
|
+
client_email: string;
|
|
7
|
+
private_key: string;
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface SearchAnalyticsRow {
|
|
12
|
+
page: string;
|
|
13
|
+
query?: string;
|
|
14
|
+
date?: string;
|
|
15
|
+
clicks: number;
|
|
16
|
+
impressions: number;
|
|
17
|
+
ctr: number;
|
|
18
|
+
position: number;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface FetchOptions {
|
|
22
|
+
siteUrl: string;
|
|
23
|
+
startDate: Date;
|
|
24
|
+
endDate: Date;
|
|
25
|
+
dimensions?: ("page" | "query" | "date")[];
|
|
26
|
+
rowLimit?: number;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export type IndexVerdict = "PASS" | "FAIL" | "NEUTRAL" | "VERDICT_UNSPECIFIED";
|
|
30
|
+
|
|
31
|
+
export interface IndexStatusResult {
|
|
32
|
+
verdict: IndexVerdict;
|
|
33
|
+
coverageState: string | null;
|
|
34
|
+
indexingState: string | null;
|
|
35
|
+
pageFetchState: string | null;
|
|
36
|
+
lastCrawlTime: Date | null;
|
|
37
|
+
robotsTxtState: string | null;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export class GSCClient {
|
|
41
|
+
private auth: JWT;
|
|
42
|
+
private searchConsole: searchconsole_v1.Searchconsole;
|
|
43
|
+
|
|
44
|
+
constructor(options: GSCClientOptions) {
|
|
45
|
+
this.auth = new google.auth.JWT({
|
|
46
|
+
email: options.credentials.client_email,
|
|
47
|
+
key: options.credentials.private_key,
|
|
48
|
+
scopes: ["https://www.googleapis.com/auth/webmasters.readonly"],
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
this.searchConsole = google.searchconsole({
|
|
52
|
+
version: "v1",
|
|
53
|
+
auth: this.auth,
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
async fetchSearchAnalytics(options: FetchOptions): Promise<SearchAnalyticsRow[]> {
|
|
58
|
+
const { siteUrl, startDate, endDate, dimensions = ["page", "date"], rowLimit = 25000 } = options;
|
|
59
|
+
|
|
60
|
+
const rows: SearchAnalyticsRow[] = [];
|
|
61
|
+
let startRow = 0;
|
|
62
|
+
|
|
63
|
+
while (true) {
|
|
64
|
+
const response = await this.searchConsole.searchanalytics.query({
|
|
65
|
+
siteUrl,
|
|
66
|
+
requestBody: {
|
|
67
|
+
startDate: formatDate(startDate),
|
|
68
|
+
endDate: formatDate(endDate),
|
|
69
|
+
dimensions,
|
|
70
|
+
rowLimit,
|
|
71
|
+
startRow,
|
|
72
|
+
},
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
const responseRows = response.data.rows ?? [];
|
|
76
|
+
if (responseRows.length === 0) break;
|
|
77
|
+
|
|
78
|
+
for (const row of responseRows) {
|
|
79
|
+
const keys = row.keys ?? [];
|
|
80
|
+
rows.push({
|
|
81
|
+
page: dimensions.includes("page") ? keys[dimensions.indexOf("page")]! : "",
|
|
82
|
+
query: dimensions.includes("query") ? keys[dimensions.indexOf("query")] : undefined,
|
|
83
|
+
date: dimensions.includes("date") ? keys[dimensions.indexOf("date")] : undefined,
|
|
84
|
+
clicks: row.clicks ?? 0,
|
|
85
|
+
impressions: row.impressions ?? 0,
|
|
86
|
+
ctr: row.ctr ?? 0,
|
|
87
|
+
position: row.position ?? 0,
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (responseRows.length < rowLimit) break;
|
|
92
|
+
startRow += rowLimit;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return rows;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async listSites(): Promise<string[]> {
|
|
99
|
+
const response = await this.searchConsole.sites.list();
|
|
100
|
+
return (response.data.siteEntry ?? []).map((site) => site.siteUrl!).filter(Boolean);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
async inspectUrl(siteUrl: string, inspectionUrl: string): Promise<IndexStatusResult> {
|
|
104
|
+
const response = await this.searchConsole.urlInspection.index.inspect({
|
|
105
|
+
requestBody: {
|
|
106
|
+
inspectionUrl,
|
|
107
|
+
siteUrl,
|
|
108
|
+
},
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
const result = response.data.inspectionResult?.indexStatusResult;
|
|
112
|
+
|
|
113
|
+
return {
|
|
114
|
+
verdict: (result?.verdict as IndexVerdict) ?? "VERDICT_UNSPECIFIED",
|
|
115
|
+
coverageState: result?.coverageState ?? null,
|
|
116
|
+
indexingState: result?.indexingState ?? null,
|
|
117
|
+
pageFetchState: result?.pageFetchState ?? null,
|
|
118
|
+
lastCrawlTime: result?.lastCrawlTime ? new Date(result.lastCrawlTime) : null,
|
|
119
|
+
robotsTxtState: result?.robotsTxtState ?? null,
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function formatDate(date: Date): string {
|
|
125
|
+
return date.toISOString().split("T")[0]!;
|
|
126
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export {
|
|
2
|
+
GSCClient,
|
|
3
|
+
type GSCClientOptions,
|
|
4
|
+
type IndexStatusResult,
|
|
5
|
+
type IndexVerdict,
|
|
6
|
+
} from "./gsc-client.js";
|
|
7
|
+
export {
|
|
8
|
+
SyncEngine,
|
|
9
|
+
type SyncOptions,
|
|
10
|
+
type SyncResult,
|
|
11
|
+
type IndexStatusSyncResult,
|
|
12
|
+
} from "./sync-engine.js";
|
|
13
|
+
export {
|
|
14
|
+
DecayDetector,
|
|
15
|
+
defaultRules,
|
|
16
|
+
type DecayRule,
|
|
17
|
+
type DecaySignal,
|
|
18
|
+
type QuietPeriodConfig,
|
|
19
|
+
} from "./decay-detector.js";
|
|
20
|
+
export {
|
|
21
|
+
URLMatcher,
|
|
22
|
+
type MatchResult,
|
|
23
|
+
type URLMatcherConfig,
|
|
24
|
+
type UnmatchReason,
|
|
25
|
+
type MatchDiagnostics,
|
|
26
|
+
} from "./url-matcher.js";
|
|
27
|
+
export {
|
|
28
|
+
TaskGenerator,
|
|
29
|
+
type TaskGeneratorOptions,
|
|
30
|
+
type QueryContext,
|
|
31
|
+
} from "./task-generator.js";
|