Skip to content

OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.

gsc: date-only bounds for Search Console queries

Carl-Gerhard Lindesvärdcfdb7c3c54cd
diff --git a/packages/core/src/modules/assistant/src/tools/seo.ts b/packages/core/src/modules/assistant/src/tools/seo.ts
index f87de60fa..934aff770 100644
--- a/packages/core/src/modules/assistant/src/tools/seo.ts
+++ b/packages/core/src/modules/assistant/src/tools/seo.ts
@@ -12,6 +12,23 @@ import {
import { getTopPagesCore } from '../../../overview/pages.service';
import { chatTool, resolveDateRange, truncateRows } from './helpers';
+/** `YYYY-MM-DD`. */
+const DATE_ONLY_LENGTH = 10;
+
+/**
+ * The GSC tables store `date` as a ClickHouse `Date` and Google's
+ * searchAnalytics API takes `YYYY-MM-DD`, so the datetime bounds
+ * `resolveDateRange` returns — right for the event and session tables — are
+ * refused here with "Cannot convert string ... to type Date"
+ * (main a0e122e8).
+ */
+function toGscRange(range: { startDate: string; endDate: string }) {
+ return {
+ startDate: range.startDate.slice(0, DATE_ONLY_LENGTH),
+ endDate: range.endDate.slice(0, DATE_ONLY_LENGTH),
+ };
+}
+
export const gscGetOverview = (deps: ServiceDeps) =>
chatTool(
{
@@ -32,8 +49,7 @@ export const gscGetOverview = (deps: ServiceDeps) =>
});
return gscGetOverviewCore(deps, {
projectId: context.projectId,
- startDate: range.startDate,
- endDate: range.endDate,
+ ...toGscRange(range),
interval,
});
}
@@ -59,8 +75,7 @@ export const gscGetTopQueries = (deps: ServiceDeps) =>
});
const rows = await gscGetTopQueriesCore(deps, {
projectId: context.projectId,
- startDate: range.startDate,
- endDate: range.endDate,
+ ...toGscRange(range),
limit: limit ?? 50,
});
return truncateRows(rows, 100);
@@ -87,8 +102,7 @@ export const gscGetTopPages = (deps: ServiceDeps) =>
});
const rows = await gscGetTopPagesCore(deps, {
projectId: context.projectId,
- startDate: range.startDate,
- endDate: range.endDate,
+ ...toGscRange(range),
limit: limit ?? 50,
});
return truncateRows(rows, 100);
@@ -115,8 +129,7 @@ export const gscGetQueryDetails = (deps: ServiceDeps) =>
});
return gscGetQueryDetailsCore(deps, {
projectId: context.projectId,
- startDate: range.startDate,
- endDate: range.endDate,
+ ...toGscRange(range),
query,
});
}
@@ -142,8 +155,7 @@ export const gscGetPageDetails = (deps: ServiceDeps) =>
});
return gscGetPageDetailsCore(deps, {
projectId: context.projectId,
- startDate: range.startDate,
- endDate: range.endDate,
+ ...toGscRange(range),
page,
});
}
@@ -169,8 +181,7 @@ export const gscGetQueryOpportunities = (deps: ServiceDeps) =>
});
return gscGetQueryOpportunitiesCore(deps, {
projectId: context.projectId,
- startDate: range.startDate,
- endDate: range.endDate,
+ ...toGscRange(range),
minImpressions,
});
}
@@ -195,8 +206,7 @@ export const gscGetCannibalization = (deps: ServiceDeps) =>
});
return gscGetCannibalizationCore(deps, {
projectId: context.projectId,
- startDate: range.startDate,
- endDate: range.endDate,
+ ...toGscRange(range),
});
}
);
@@ -223,8 +233,7 @@ export const correlateSeoWithTraffic = (deps: ServiceDeps) =>
const [gscPages, opPages] = await Promise.all([
gscGetTopPagesCore(deps, {
projectId: context.projectId,
- startDate: range.startDate,
- endDate: range.endDate,
+ ...toGscRange(range),
limit: 200,
}),
getTopPagesCore(deps, {
diff --git a/packages/core/src/modules/export/export.routes.ts b/packages/core/src/modules/export/export.routes.ts
index 52674a303..c9347c5c3 100644
--- a/packages/core/src/modules/export/export.routes.ts
+++ b/packages/core/src/modules/export/export.routes.ts
@@ -85,6 +85,7 @@ import { getChartStartEndDate } from '../report/src/chart-dates';
import { querySessionsCore } from '../session/session.service';
import {
resolveExportProjectId,
+ resolveGscInsightsDateRange,
resolveInsightsDateRange,
resolveInsightsProjectId,
} from './export.service';
@@ -1406,7 +1407,7 @@ export const insightsRoutes = defineRoutes((app) =>
'/insights/:projectId/gsc/overview',
async ({ params, query, client, ctx }) => {
const projectId = await resolveInsightsProjectId(ctx, client, params);
- const { startDate, endDate } = await resolveInsightsDateRange(
+ const { startDate, endDate } = await resolveGscInsightsDateRange(
ctx,
projectId,
query
@@ -1433,7 +1434,7 @@ export const insightsRoutes = defineRoutes((app) =>
'/insights/:projectId/gsc/pages',
async ({ params, query, client, ctx }) => {
const projectId = await resolveInsightsProjectId(ctx, client, params);
- const { startDate, endDate } = await resolveInsightsDateRange(
+ const { startDate, endDate } = await resolveGscInsightsDateRange(
ctx,
projectId,
query
@@ -1459,7 +1460,7 @@ export const insightsRoutes = defineRoutes((app) =>
'/insights/:projectId/gsc/pages/details',
async ({ params, query, client, ctx }) => {
const projectId = await resolveInsightsProjectId(ctx, client, params);
- const { startDate, endDate } = await resolveInsightsDateRange(
+ const { startDate, endDate } = await resolveGscInsightsDateRange(
ctx,
projectId,
query
@@ -1485,7 +1486,7 @@ export const insightsRoutes = defineRoutes((app) =>
'/insights/:projectId/gsc/queries',
async ({ params, query, client, ctx }) => {
const projectId = await resolveInsightsProjectId(ctx, client, params);
- const { startDate, endDate } = await resolveInsightsDateRange(
+ const { startDate, endDate } = await resolveGscInsightsDateRange(
ctx,
projectId,
query
@@ -1511,7 +1512,7 @@ export const insightsRoutes = defineRoutes((app) =>
'/insights/:projectId/gsc/queries/details',
async ({ params, query, client, ctx }) => {
const projectId = await resolveInsightsProjectId(ctx, client, params);
- const { startDate, endDate } = await resolveInsightsDateRange(
+ const { startDate, endDate } = await resolveGscInsightsDateRange(
ctx,
projectId,
query
@@ -1537,7 +1538,7 @@ export const insightsRoutes = defineRoutes((app) =>
'/insights/:projectId/gsc/queries/opportunities',
async ({ params, query, client, ctx }) => {
const projectId = await resolveInsightsProjectId(ctx, client, params);
- const { startDate, endDate } = await resolveInsightsDateRange(
+ const { startDate, endDate } = await resolveGscInsightsDateRange(
ctx,
projectId,
query
@@ -1564,7 +1565,7 @@ export const insightsRoutes = defineRoutes((app) =>
'/insights/:projectId/gsc/cannibalization',
async ({ params, query, client, ctx }) => {
const projectId = await resolveInsightsProjectId(ctx, client, params);
- const { startDate, endDate } = await resolveInsightsDateRange(
+ const { startDate, endDate } = await resolveGscInsightsDateRange(
ctx,
projectId,
query
diff --git a/packages/core/src/modules/export/export.service.ts b/packages/core/src/modules/export/export.service.ts
index f0a15d2f3..70f02b1cc 100644
--- a/packages/core/src/modules/export/export.service.ts
+++ b/packages/core/src/modules/export/export.service.ts
@@ -108,6 +108,33 @@ export async function resolveInsightsDateRange(
);
}
+/** `YYYY-MM-DD`. */
+const DATE_ONLY_LENGTH = 10;
+
+/**
+ * The GSC tables store `date` as a ClickHouse `Date` and Google's
+ * searchAnalytics API takes `YYYY-MM-DD`, so the datetime bounds the event and
+ * session tables need are wrong for both: ClickHouse refuses
+ * `'2026-08-07 00:00:00'` with "Cannot convert string ... to type Date", which
+ * failed every /insights/:projectId/gsc/* route for a named `range`
+ * (main a0e122e8).
+ */
+export async function resolveGscInsightsDateRange(
+ deps: ServiceDeps,
+ projectId: string,
+ data: { startDate?: string; endDate?: string; range?: IChartRange }
+): Promise<{ startDate: string; endDate: string }> {
+ const { startDate, endDate } = await resolveInsightsDateRange(
+ deps,
+ projectId,
+ data
+ );
+ return {
+ startDate: startDate.slice(0, DATE_ONLY_LENGTH),
+ endDate: endDate.slice(0, DATE_ONLY_LENGTH),
+ };
+}
+
// --- service ------------------------------------------------------------
export function createExportService(