@yourbright/emdash-analytics-plugin 0.1.9 → 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 +55 -7
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -1048,15 +1048,12 @@ function buildOverviewData(summary, freshness, allPages, topOpportunities, topUn
1048
1048
  }
1049
1049
  async function getContentContext(ctx, collection, id, slug) {
1050
1050
  const config = await requireConfig(ctx);
1051
- const page = await findContentPage(ctx, collection, id, slug);
1052
- if (!page) {
1053
- throw new PluginRouteError("NOT_FOUND", "Analytics data not found for content", 404);
1054
- }
1051
+ const freshness = await getFreshness(ctx);
1052
+ const page = await findContentPage(ctx, collection, id, slug) || createFallbackContentPage(config.siteOrigin, collection, id, slug, freshness);
1055
1053
  const contentRef = resolveStoredContentRef(page, collection, id, slug);
1056
- const queries = await getFreshQueriesForPage(ctx, config, contentRef.urlPath);
1054
+ const queries = page.lastSyncedAt ? await getFreshQueriesForPage(ctx, config, contentRef.urlPath) : [];
1057
1055
  const windows = buildWindows();
1058
1056
  const score = scorePage(page, queries);
1059
- const freshness = await getFreshness(ctx);
1060
1057
  return {
1061
1058
  content: {
1062
1059
  collection: "posts",
@@ -1202,6 +1199,43 @@ async function findContentPage(ctx, collection, id, slug) {
1202
1199
  }
1203
1200
  return null;
1204
1201
  }
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
+ }
1205
1239
  function resolveStoredContentRef(page, collection, id, slug) {
1206
1240
  return {
1207
1241
  collection: "posts",
@@ -1704,7 +1738,21 @@ function createPlugin() {
1704
1738
  if (!id && !slug) {
1705
1739
  throw new PluginRouteError2("BAD_REQUEST", "id or slug is required", 400);
1706
1740
  }
1707
- 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
+ }
1708
1756
  }
1709
1757
  }
1710
1758
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yourbright/emdash-analytics-plugin",
3
- "version": "0.1.9",
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",