@yourbright/emdash-analytics-plugin 0.1.8 → 0.1.10

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 (2) hide show
  1. package/dist/index.js +57 -58
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -297,27 +297,6 @@ async function getManagedContentMap(siteOrigin) {
297
297
  void siteOrigin;
298
298
  return managed;
299
299
  }
300
- async function resolveManagedContent(collection, id, slug, siteOrigin) {
301
- if (collection !== "posts") return null;
302
- const ref = id || slug;
303
- if (!ref) return null;
304
- const result = await getEmDashEntry("posts", ref);
305
- const entry = result.entry;
306
- if (!entry) return null;
307
- const entryId = typeof entry.id === "string" ? entry.id : ref;
308
- const entrySlug = typeof entry.slug === "string" ? entry.slug : null;
309
- const data = entry.data ?? {};
310
- const urlPath = `/blog/${entrySlug || entryId}/`;
311
- return {
312
- collection: "posts",
313
- id: entryId,
314
- slug: entrySlug,
315
- urlPath,
316
- title: typeof data.title === "string" ? data.title : entrySlug || entryId,
317
- excerpt: typeof data.excerpt === "string" ? data.excerpt : void 0,
318
- seoDescription: typeof data.seo_description === "string" ? data.seo_description : void 0
319
- };
320
- }
321
300
  function pageStorageId(urlPath) {
322
301
  return stableStorageId(urlPath);
323
302
  }
@@ -1069,23 +1048,12 @@ function buildOverviewData(summary, freshness, allPages, topOpportunities, topUn
1069
1048
  }
1070
1049
  async function getContentContext(ctx, collection, id, slug) {
1071
1050
  const config = await requireConfig(ctx);
1072
- const page = await findContentPage(ctx, collection, id, slug);
1073
- if (!page) {
1074
- throw new PluginRouteError("NOT_FOUND", "Analytics data not found for content", 404);
1075
- }
1076
- const contentRef = await resolveContentRef(config.siteOrigin, page, collection, id, slug) || {
1077
- collection: "posts",
1078
- id: page.contentId || id || pageStorageId(page.urlPath),
1079
- slug: page.contentSlug || slug || null,
1080
- urlPath: page.urlPath,
1081
- title: page.title,
1082
- excerpt: void 0,
1083
- seoDescription: void 0
1084
- };
1085
- const queries = await getFreshQueriesForPage(ctx, config, contentRef.urlPath);
1051
+ const freshness = await getFreshness(ctx);
1052
+ const page = await findContentPage(ctx, collection, id, slug) || createFallbackContentPage(config.siteOrigin, collection, id, slug, freshness);
1053
+ const contentRef = resolveStoredContentRef(page, collection, id, slug);
1054
+ const queries = page.lastSyncedAt ? await getFreshQueriesForPage(ctx, config, contentRef.urlPath) : [];
1086
1055
  const windows = buildWindows();
1087
1056
  const score = scorePage(page, queries);
1088
- const freshness = await getFreshness(ctx);
1089
1057
  return {
1090
1058
  content: {
1091
1059
  collection: "posts",
@@ -1231,27 +1199,44 @@ async function findContentPage(ctx, collection, id, slug) {
1231
1199
  }
1232
1200
  return null;
1233
1201
  }
1234
- async function resolveContentRef(siteOrigin, page, collection, id, slug) {
1235
- if (collection !== "posts") {
1236
- return {
1237
- collection: "posts",
1238
- id: page.contentId || id || pageStorageId(page.urlPath),
1239
- slug: page.contentSlug || slug || null,
1240
- urlPath: page.urlPath,
1241
- title: page.title,
1242
- excerpt: void 0,
1243
- seoDescription: void 0
1244
- };
1245
- }
1246
- const resolved = await resolveManagedContent(
1247
- collection,
1248
- page.contentId || id,
1249
- page.contentSlug || slug || void 0,
1250
- siteOrigin
1251
- );
1252
- if (resolved) {
1253
- return resolved;
1254
- }
1202
+ function createFallbackContentPage(siteOrigin, collection, id, slug, freshness) {
1203
+ const host = new URL(siteOrigin).hostname;
1204
+ const resolvedSlug = slug || null;
1205
+ const resolvedId = id || null;
1206
+ const urlPath = collection === "posts" ? `/blog/${resolvedSlug || resolvedId || "unknown"}/` : "/";
1207
+ const title = resolvedSlug || resolvedId || urlPath;
1208
+ return {
1209
+ urlPath,
1210
+ host,
1211
+ pageKind: classifyPageKind(urlPath),
1212
+ managed: true,
1213
+ title,
1214
+ contentCollection: collection,
1215
+ contentId: resolvedId,
1216
+ contentSlug: resolvedSlug,
1217
+ gscClicks28d: 0,
1218
+ gscImpressions28d: 0,
1219
+ gscCtr28d: 0,
1220
+ gscPosition28d: 0,
1221
+ gscClicksPrev28d: 0,
1222
+ gscImpressionsPrev28d: 0,
1223
+ gaViews28d: 0,
1224
+ gaUsers28d: 0,
1225
+ gaSessions28d: 0,
1226
+ gaEngagementRate28d: 0,
1227
+ gaBounceRate28d: 0,
1228
+ gaAvgSessionDuration28d: 0,
1229
+ gaViewsPrev28d: 0,
1230
+ gaUsersPrev28d: 0,
1231
+ gaSessionsPrev28d: 0,
1232
+ opportunityScore: 0,
1233
+ opportunityTags: [],
1234
+ lastSyncedAt: "",
1235
+ lastGscDate: freshness?.lastGscDate || null,
1236
+ lastGaDate: freshness?.lastGaDate || null
1237
+ };
1238
+ }
1239
+ function resolveStoredContentRef(page, collection, id, slug) {
1255
1240
  return {
1256
1241
  collection: "posts",
1257
1242
  id: page.contentId || id || pageStorageId(page.urlPath),
@@ -1753,7 +1738,21 @@ function createPlugin() {
1753
1738
  if (!id && !slug) {
1754
1739
  throw new PluginRouteError2("BAD_REQUEST", "id or slug is required", 400);
1755
1740
  }
1756
- return getContentContext(ctx, collection, id, slug);
1741
+ try {
1742
+ return getContentContext(ctx, collection, id, slug);
1743
+ } catch (error) {
1744
+ console.error("[analytics-plugin] content-context failed", {
1745
+ collection,
1746
+ id,
1747
+ slug,
1748
+ error: error instanceof Error ? {
1749
+ name: error.name,
1750
+ message: error.message,
1751
+ stack: error.stack
1752
+ } : String(error)
1753
+ });
1754
+ throw error;
1755
+ }
1757
1756
  }
1758
1757
  }
1759
1758
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yourbright/emdash-analytics-plugin",
3
- "version": "0.1.8",
3
+ "version": "0.1.10",
4
4
  "description": "Google Search Console and GA4 analytics plugin for EmDash",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",