Skip to content

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

fix: fixes random issues

Carl-Gerhard Lindesvärd9e392e37b085
diff --git a/packages/db/src/clickhouse/client.ts b/packages/db/src/clickhouse/client.ts
index 2c2bc5db2..463bc31bf 100644
--- a/packages/db/src/clickhouse/client.ts
+++ b/packages/db/src/clickhouse/client.ts
@@ -117,7 +117,9 @@ export const CLICKHOUSE_OPTIONS: NodeClickHouseClientConfigOptions = {
request_timeout: 300_000,
keep_alive: {
enabled: true,
- idle_socket_ttl: 60_000,
+ // Must be lower than server-side keep_alive_timeout (default 10s) so we
+ // never reuse a socket the server has already closed.
+ idle_socket_ttl: 7000,
},
compression: {
request: true,
@@ -166,7 +168,7 @@ export async function withRetry<T>(
const delay = baseDelay * 2 ** attempt;
logger.warn(
{ err: error },
- `Attempt ${attempt + 1}/${maxRetries} failed, retrying in ${delay}ms`,
+ `Attempt ${attempt + 1}/${maxRetries} failed, retrying in ${delay}ms`
);
await new Promise((resolve) => setTimeout(resolve, delay));
continue;
@@ -250,7 +252,7 @@ export async function chQueryWithMeta<T extends Record<string, any>>(
elapsed: Date.now() - start,
clickhouseSettings,
},
- 'query info',
+ 'query info'
);
return response;
diff --git a/packages/db/src/services/chart-sql.test.ts b/packages/db/src/services/chart-sql.test.ts
new file mode 100644
index 000000000..26541bc17
--- /dev/null
+++ b/packages/db/src/services/chart-sql.test.ts
@@ -0,0 +1,313 @@
+/**
+ * SQL-syntax tests for chart / overview query builders.
+ *
+ * Strategy: build the SQL string, then run `EXPLAIN <sql>` against the local
+ * ClickHouse instance. EXPLAIN parses the query, resolves columns, and builds
+ * the query plan without executing it — so we catch UNKNOWN_IDENTIFIER,
+ * AMBIGUOUS_IDENTIFIER and bad JOIN ON expressions without needing seeded
+ * data. WITH FILL TO < FROM is a runtime check, so it's covered by a plain
+ * string assertion instead.
+ *
+ * Requires `pnpm dock:up` (or any locally reachable CH at
+ * http://localhost:8123/openpanel). All tests auto-skip if CH is unreachable.
+ */
+import type { IChartBreakdown, IChartEvent } from '@openpanel/validation';
+import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest';
+import { ch } from '../clickhouse/client';
+import {
+ getAggregateChartSql as _getAggregateChartSql,
+ getChartSql as _getChartSql,
+} from './chart.service';
+import { OverviewService } from './overview.service';
+
+// IGetChartDataInput has display-only fields (metric, chartType, previous,
+// etc.) that the SQL builders ignore. Tests only care about SQL output, so
+// loosen the input type here.
+const getChartSql: (input: any) => Promise<string> = _getChartSql as any;
+const getAggregateChartSql: (input: any) => Promise<string> =
+ _getAggregateChartSql as any;
+
+const PROJECT_ID = 'test-sql-validation';
+const START = '2026-04-14 00:00:00';
+const END = '2026-05-15 00:00:00';
+
+const event = (overrides: Partial<IChartEvent> = {}): IChartEvent => ({
+ id: 'A',
+ name: 'screen_view',
+ segment: 'event',
+ filters: [],
+ ...overrides,
+});
+
+const breakdown = (name: string): IChartBreakdown => ({ id: name, name });
+
+let chReachable = false;
+
+async function explain(sql: string): Promise<void> {
+ // EXPLAIN runs the parser + analyzer and builds the query plan, which
+ // catches UNKNOWN_IDENTIFIER and AMBIGUOUS_IDENTIFIER. It does not execute.
+ await ch.command({ query: `EXPLAIN ${sql}` });
+}
+
+beforeAll(async () => {
+ // The chart service is chatty; mute log spam during the test run.
+ vi.spyOn(console, 'log').mockImplementation(() => {});
+ try {
+ await ch.command({ query: 'SELECT 1' });
+ chReachable = true;
+ } catch {
+ // CH not running locally — skip every test.
+ chReachable = false;
+ }
+});
+
+afterAll(() => {
+ vi.restoreAllMocks();
+});
+
+const itCH = (name: string, fn: () => Promise<void>) =>
+ it(name, async () => {
+ if (!chReachable) {
+ console.warn(
+ '[chart-sql] skipping: ClickHouse not reachable at CLICKHOUSE_URL',
+ );
+ return;
+ }
+ await fn();
+ });
+
+describe('chart.service / getChartSql', () => {
+ itCH(
+ 'qualifies properties[...] with `e.` when group join is present (fixes AMBIGUOUS_IDENTIFIER)',
+ async () => {
+ const sql = await getChartSql({
+ event: event({
+ segment: 'session',
+ filters: [
+ { name: 'group.plan', operator: 'is', value: ['pro'] },
+ {
+ name: 'properties.__query.utm_source',
+ operator: 'is',
+ value: ['awn'],
+ },
+ ],
+ }),
+ breakdowns: [
+ breakdown('country'),
+ breakdown('properties.__query.utm_source'),
+ ],
+ interval: 'day',
+ startDate: START,
+ endDate: END,
+ projectId: PROJECT_ID,
+ timezone: 'UTC',
+ });
+
+ // Every map access on the events table must be aliased — the `_g` join
+ // also exposes a `properties` column, so the bare form is ambiguous.
+ expect(sql).not.toMatch(/(?<![._\w])properties\[/);
+ expect(sql).toContain("e.properties['__query.utm_source']");
+
+ await explain(sql);
+ },
+ );
+
+ itCH(
+ 'works without group join (qualified form is still valid when alone)',
+ async () => {
+ const sql = await getChartSql({
+ event: event({
+ filters: [
+ {
+ name: 'properties.__query.utm_source',
+ operator: 'is',
+ value: ['awn'],
+ },
+ ],
+ }),
+ breakdowns: [breakdown('properties.__query.utm_source')],
+ interval: 'day',
+ startDate: START,
+ endDate: END,
+ projectId: PROJECT_ID,
+ timezone: 'UTC',
+ });
+ await explain(sql);
+ },
+ );
+
+ itCH(
+ 'drops the all-cohorts breakdown when the project has 0 cohorts',
+ async () => {
+ const sql = await getChartSql({
+ event: event({ segment: 'user' }),
+ breakdowns: [breakdown('cohort')],
+ interval: 'day',
+ startDate: START,
+ endDate: END,
+ // Use a project that almost certainly has no cohorts in PG.
+ projectId: PROJECT_ID,
+ timezone: 'UTC',
+ });
+
+ // The all-cohorts JOIN expanded to `_uc._uc_label_X = 'Unknown'`, a
+ // constant predicate with no join key — CH rejects it. The fix is to
+ // remove the breakdown entirely when there are no cohorts.
+ expect(sql).not.toMatch(/_uc_label_\d+\s*=\s*'Unknown'/);
+ expect(sql).not.toContain('_all_cohorts');
+
+ await explain(sql);
+ },
+ );
+
+ itCH('skips WITH FILL when endDate < startDate', async () => {
+ const sql = await getChartSql({
+ event: event(),
+ breakdowns: [],
+ interval: 'day',
+ startDate: END, // inverted
+ endDate: START,
+ projectId: PROJECT_ID,
+ timezone: 'UTC',
+ });
+
+ expect(sql).not.toContain('WITH FILL');
+ // Plain query without FILL should still parse.
+ await explain(sql);
+ });
+
+ itCH('skips WITH FILL when endDate equals startDate inverted (week)', async () => {
+ const sql = await getChartSql({
+ event: event(),
+ breakdowns: [],
+ interval: 'week',
+ startDate: END,
+ endDate: START,
+ projectId: PROJECT_ID,
+ timezone: 'UTC',
+ });
+ expect(sql).not.toContain('WITH FILL');
+ });
+
+ itCH('emits WITH FILL when the range is valid', async () => {
+ const sql = await getChartSql({
+ event: event(),
+ breakdowns: [],
+ interval: 'day',
+ startDate: START,
+ endDate: END,
+ projectId: PROJECT_ID,
+ timezone: 'UTC',
+ });
+ expect(sql).toContain('WITH FILL');
+ await explain(sql);
+ });
+
+ itCH('property metric (property_sum) with group join is unambiguous', async () => {
+ const sql = await getChartSql({
+ event: event({
+ segment: 'property_sum',
+ property: 'properties.revenue_amount',
+ filters: [{ name: 'group.plan', operator: 'is', value: ['pro'] }],
+ }),
+ breakdowns: [],
+ interval: 'day',
+ startDate: START,
+ endDate: END,
+ projectId: PROJECT_ID,
+ timezone: 'UTC',
+ });
+ expect(sql).toContain("e.properties['revenue_amount']");
+ await explain(sql);
+ });
+
+ itCH('one_event_per_user segment still parses', async () => {
+ const sql = await getChartSql({
+ event: event({ segment: 'one_event_per_user' }),
+ breakdowns: [],
+ interval: 'day',
+ startDate: START,
+ endDate: END,
+ projectId: PROJECT_ID,
+ timezone: 'UTC',
+ });
+ await explain(sql);
+ });
+});
+
+describe('chart.service / getAggregateChartSql', () => {
+ itCH('drops all-cohorts breakdown on empty cohort project', async () => {
+ const sql = await getAggregateChartSql({
+ event: event({ segment: 'user' }),
+ breakdowns: [breakdown('cohort')],
+ startDate: START,
+ endDate: END,
+ projectId: PROJECT_ID,
+ timezone: 'UTC',
+ });
+ expect(sql).not.toMatch(/_uc_label_\d+\s*=\s*'Unknown'/);
+ await explain(sql);
+ });
+
+ itCH('properties + group breakdown is unambiguous', async () => {
+ const sql = await getAggregateChartSql({
+ event: event({
+ filters: [{ name: 'group.plan', operator: 'is', value: ['pro'] }],
+ }),
+ breakdowns: [breakdown('properties.__query.utm_source')],
+ startDate: START,
+ endDate: END,
+ projectId: PROJECT_ID,
+ timezone: 'UTC',
+ });
+ expect(sql).toContain("e.properties['__query.utm_source']");
+ await explain(sql);
+ });
+});
+
+describe('overview.service / getRawWhereClause (UTM remapping)', () => {
+ const svc = new OverviewService(ch);
+
+ it('rewrites utm_* to properties[__query.utm_*] for the events table', () => {
+ const where = svc.getRawWhereClause('events', [
+ { name: 'utm_source', operator: 'is', value: ['awn'] },
+ ]);
+ expect(where).toContain("properties['__query.utm_source']");
+ expect(where).not.toMatch(/(?<![._\w])utm_source\s*=/);
+ });
+
+ it('keeps utm_* as a top-level column for the sessions table', () => {
+ const where = svc.getRawWhereClause('sessions', [
+ { name: 'utm_source', operator: 'is', value: ['awn'] },
+ ]);
+ expect(where).toMatch(/(?<![._\w])utm_source\s*=/);
+ expect(where).not.toContain("properties['__query.utm_source']");
+ });
+
+ it('drops non-whitelisted filters', () => {
+ const where = svc.getRawWhereClause('events', [
+ { name: 'malicious_column', operator: 'is', value: ['x'] },
+ ]);
+ expect(where).toBe('');
+ });
+
+ itCH('events utm_source filter parses against real events table', async () => {
+ const where = svc.getRawWhereClause('events', [
+ { name: 'utm_source', operator: 'is', value: ['awn'] },
+ ]);
+ expect(where).toBeTruthy();
+ await explain(
+ `SELECT count() FROM events WHERE project_id = '${PROJECT_ID}' AND ${where}`,
+ );
+ });
+
+ itCH('sessions utm_source filter parses against real sessions table', async () => {
+ const where = svc.getRawWhereClause('sessions', [
+ { name: 'utm_source', operator: 'is', value: ['awn'] },
+ ]);
+ expect(where).toBeTruthy();
+ await explain(
+ `SELECT count() FROM sessions WHERE project_id = '${PROJECT_ID}' AND ${where}`,
+ );
+ });
+});
diff --git a/packages/db/src/services/chart.service.ts b/packages/db/src/services/chart.service.ts
index ee7a86f19..98a2df9ff 100644
--- a/packages/db/src/services/chart.service.ts
+++ b/packages/db/src/services/chart.service.ts
@@ -220,6 +220,13 @@ export function getSelectPropertyKey(
projectId?: string,
cohortId?: string,
cohortName?: string,
+ /**
+ * When set, the events table's `properties` map is qualified with this
+ * alias (e.g. `e.properties[...]`). Required in any query where another
+ * joined table also exposes a `properties` column (such as the groups
+ * `_g` join), otherwise ClickHouse rejects with "ambiguous identifier".
+ */
+ eventsAlias?: string,
) {
const extractedCohortId = cohortId || extractCohortId(property);
@@ -252,18 +259,24 @@ export function getSelectPropertyKey(
return property;
}
+ // Only the events table's bare `properties` map needs aliasing —
+ // `profile.properties` already routes through the profile join alias.
+ const aliasPrefix = match === 'properties' && eventsAlias
+ ? `${eventsAlias}.`
+ : '';
+
if (property.includes('*')) {
- return `arrayMap(x -> trim(x), mapValues(mapExtractKeyLike(${match}, ${sqlstring.escape(
+ return `arrayMap(x -> trim(x), mapValues(mapExtractKeyLike(${aliasPrefix}${match}, ${sqlstring.escape(
transformPropertyKey(property)
)})))`;
}
- return `${match}['${property.replace(new RegExp(`^${match}.`), '')}']`;
+ return `${aliasPrefix}${match}['${property.replace(new RegExp(`^${match}.`), '')}']`;
}
export async function getChartSql({
event,
- breakdowns,
+ breakdowns: initialBreakdowns,
interval,
startDate,
endDate,
@@ -284,12 +297,22 @@ export async function getChartSql({
with: addCte,
} = createSqlBuilder();
- const hasAllCohortsBreakdown = breakdowns.some((b) =>
+ let breakdowns = initialBreakdowns;
+ const requestedAllCohortsBreakdown = breakdowns.some((b) =>
isAllCohortsBreakdown(b.name),
);
- const allCohorts = hasAllCohortsBreakdown
+ const allCohorts = requestedAllCohortsBreakdown
? await fetchProjectCohorts(projectId)
: [];
+ // Drop the all-cohorts breakdown when the project has no cohorts: the label
+ // expression collapses to the literal 'Unknown', making the _uc JOIN ON it
+ // a constant comparison with no join key (ClickHouse rejects with
+ // "Cannot determine join keys").
+ if (requestedAllCohortsBreakdown && allCohorts.length === 0) {
+ breakdowns = breakdowns.filter((b) => !isAllCohortsBreakdown(b.name));
+ }
+ const hasAllCohortsBreakdown =
+ requestedAllCohortsBreakdown && allCohorts.length > 0;
const cohortIds = collectCohortIds(event.filters, breakdowns);
const cohortMetadata = await fetchCohortsMetadata(cohortIds);
@@ -311,7 +334,7 @@ export async function getChartSql({
`LEFT ANY JOIN ${getCohortCteName(cohortId)} AS ${getCohortAlias(cohortId)} ON ${getCohortAlias(cohortId)}.profile_id = e.profile_id`;
}
- sb.where = getEventFiltersWhereClause(event.filters, projectId);
+ sb.where = getEventFiltersWhereClause(event.filters, projectId, 'e');
sb.where.projectId = `project_id = ${sqlstring.escape(projectId)}`;
if (event.name !== '*') {
@@ -459,29 +482,43 @@ export async function getChartSql({
}
sb.select.count = 'count(*) as count';
+ // ClickHouse rejects WITH FILL when TO < FROM, so only emit a fill clause
+ // for a valid range. The SELECT bucket truncation is always safe.
+ const hasValidFillRange =
+ !!startDate && !!endDate && new Date(endDate) >= new Date(startDate);
switch (interval) {
case 'minute': {
- sb.fill = `FROM toStartOfMinute(toDateTime('${startDate}')) TO toStartOfMinute(toDateTime('${endDate}')) STEP toIntervalMinute(1)`;
+ if (hasValidFillRange) {
+ sb.fill = `FROM toStartOfMinute(toDateTime('${startDate}')) TO toStartOfMinute(toDateTime('${endDate}')) STEP toIntervalMinute(1)`;
+ }
sb.select.date = 'toStartOfMinute(created_at) as date';
break;
}
case 'hour': {
- sb.fill = `FROM toStartOfHour(toDateTime('${startDate}')) TO toStartOfHour(toDateTime('${endDate}')) STEP toIntervalHour(1)`;
+ if (hasValidFillRange) {
+ sb.fill = `FROM toStartOfHour(toDateTime('${startDate}')) TO toStartOfHour(toDateTime('${endDate}')) STEP toIntervalHour(1)`;
+ }
sb.select.date = 'toStartOfHour(created_at) as date';
break;
}
case 'day': {
- sb.fill = `FROM toStartOfDay(toDateTime('${startDate}')) TO toStartOfDay(toDateTime('${endDate}')) STEP toIntervalDay(1)`;
+ if (hasValidFillRange) {
+ sb.fill = `FROM toStartOfDay(toDateTime('${startDate}')) TO toStartOfDay(toDateTime('${endDate}')) STEP toIntervalDay(1)`;
+ }
sb.select.date = 'toStartOfDay(created_at) as date';
break;
}
case 'week': {
- sb.fill = `FROM toStartOfWeek(toDateTime('${startDate}'), 1, '${timezone}') TO toStartOfWeek(toDateTime('${endDate}'), 1, '${timezone}') STEP toIntervalWeek(1)`;
+ if (hasValidFillRange) {
+ sb.fill = `FROM toStartOfWeek(toDateTime('${startDate}'), 1, '${timezone}') TO toStartOfWeek(toDateTime('${endDate}'), 1, '${timezone}') STEP toIntervalWeek(1)`;
+ }
sb.select.date = `toStartOfWeek(created_at, 1, '${timezone}') as date`;
break;
}
case 'month': {
- sb.fill = `FROM toStartOfMonth(toDateTime('${startDate}'), '${timezone}') TO toStartOfMonth(toDateTime('${endDate}'), '${timezone}') STEP toIntervalMonth(1)`;
+ if (hasValidFillRange) {
+ sb.fill = `FROM toStartOfMonth(toDateTime('${startDate}'), '${timezone}') TO toStartOfMonth(toDateTime('${endDate}'), '${timezone}') STEP toIntervalMonth(1)`;
+ }
sb.select.date = `toStartOfMonth(created_at, '${timezone}') as date`;
break;
}
@@ -509,7 +546,7 @@ export async function getChartSql({
? cohortMetadata.get(breakdownCohortId)?.name
: undefined;
sb.select[key] =
- `${getSelectPropertyKey(breakdown.name, projectId, breakdownCohortId ?? undefined, breakdownCohortName)} as ${key}`;
+ `${getSelectPropertyKey(breakdown.name, projectId, breakdownCohortId ?? undefined, breakdownCohortName, 'e')} as ${key}`;
}
sb.groupBy[key] = `${key}`;
});
@@ -539,7 +576,13 @@ export async function getChartSql({
}[event.segment as string];
if (mathFunction && event.property) {
- const propertyKey = getSelectPropertyKey(event.property);
+ const propertyKey = getSelectPropertyKey(
+ event.property,
+ undefined,
+ undefined,
+ undefined,
+ 'e',
+ );
if (isNumericColumn(event.property)) {
sb.select.count = `${mathFunction}(${propertyKey}) as count`;
@@ -559,6 +602,11 @@ export async function getChartSql({
ORDER BY profile_id, created_at DESC
) as subQuery`;
sb.joins = {};
+ // Filters were already applied inside the subquery, and the outer query
+ // selects from `subQuery` — the `e` alias used in sb.where is no longer
+ // in scope, so re-emitting WHERE would produce
+ // "Unknown identifier `e.name`". Clear it.
+ sb.where = {};
const sql = `${getWith()}${getSelect()} ${getFrom()} ${getJoins()} ${getWhere()} ${getGroupBy()} ${getOrderBy()} ${getFill()}`;
console.log('-- Report --');
@@ -597,6 +645,7 @@ export async function getChartSql({
projectId,
bId ?? undefined,
bName,
+ 'e',
);
return `${propertyKey} as _uc_label_${index + 1}`;
});
@@ -625,6 +674,7 @@ export async function getChartSql({
projectId,
bId ?? undefined,
bName,
+ 'e',
);
return `_uc._uc_label_${index + 1} = ${propertyKey}`;
})
@@ -653,7 +703,7 @@ export async function getChartSql({
export async function getAggregateChartSql({
event,
- breakdowns,
+ breakdowns: initialBreakdowns,
startDate,
endDate,
projectId,
@@ -663,12 +713,19 @@ export async function getAggregateChartSql({
}) {
const { sb, join, getJoins, with: addCte, getSql } = createSqlBuilder();
- const hasAllCohortsBreakdown = breakdowns.some((b) =>
+ let breakdowns = initialBreakdowns;
+ const requestedAllCohortsBreakdown = breakdowns.some((b) =>
isAllCohortsBreakdown(b.name),
);
- const allCohorts = hasAllCohortsBreakdown
+ const allCohorts = requestedAllCohortsBreakdown
? await fetchProjectCohorts(projectId)
: [];
+ // See getChartSql for rationale.
+ if (requestedAllCohortsBreakdown && allCohorts.length === 0) {
+ breakdowns = breakdowns.filter((b) => !isAllCohortsBreakdown(b.name));
+ }
+ const hasAllCohortsBreakdown =
+ requestedAllCohortsBreakdown && allCohorts.length > 0;
const cohortIds = collectCohortIds(event.filters, breakdowns);
const cohortMetadata = await fetchCohortsMetadata(cohortIds);
@@ -690,7 +747,7 @@ export async function getAggregateChartSql({
`LEFT ANY JOIN ${getCohortCteName(cohortId)} AS ${getCohortAlias(cohortId)} ON ${getCohortAlias(cohortId)}.profile_id = e.profile_id`;
}
- sb.where = getEventFiltersWhereClause(event.filters, projectId);
+ sb.where = getEventFiltersWhereClause(event.filters, projectId, 'e');
sb.where.projectId = `project_id = ${sqlstring.escape(projectId)}`;
if (event.name !== '*') {
@@ -849,7 +906,7 @@ export async function getAggregateChartSql({
? cohortMetadata.get(breakdownCohortId)?.name
: undefined;
sb.select[key] =
- `${getSelectPropertyKey(breakdown.name, projectId, breakdownCohortId ?? undefined, breakdownCohortName)} as ${key}`;
+ `${getSelectPropertyKey(breakdown.name, projectId, breakdownCohortId ?? undefined, breakdownCohortName, 'e')} as ${key}`;
}
sb.groupBy[key] = `${key}`;
});
@@ -886,7 +943,13 @@ export async function getAggregateChartSql({
}[event.segment as string];
if (mathFunction && event.property) {
- const propertyKey = getSelectPropertyKey(event.property, projectId);
+ const propertyKey = getSelectPropertyKey(
+ event.property,
+ projectId,
+ undefined,
+ undefined,
+ 'e',
+ );
if (isNumericColumn(event.property)) {
sb.select.count = `${mathFunction}(${propertyKey}) as count`;
@@ -936,7 +999,14 @@ function isNumericColumn(columnName: string): boolean {
export function getEventFiltersWhereClause(
filters: IChartEventFilter[],
- projectId?: string
+ projectId?: string,
+ /**
+ * See `getSelectPropertyKey`. When the surrounding query joins another
+ * table that has a `properties` column (e.g. the `_g` groups join), the
+ * events table must be aliased and passed here so we can emit
+ * `e.properties[...]` instead of the ambiguous `properties[...]`.
+ */
+ eventsAlias?: string,
) {
const where: Record<string, string> = {};
filters.forEach((filter, index) => {
@@ -1035,9 +1105,15 @@ export function getEventFiltersWhereClause(
name.startsWith('properties.') ||
name.startsWith('profile.properties.')
) {
- const propertyKey = getSelectPropertyKey(name);
+ const propertyKey = getSelectPropertyKey(
+ name,
+ undefined,
+ undefined,
+ undefined,
+ eventsAlias,
+ );
const isWildcard = propertyKey.includes('%');
- const whereFrom = getSelectPropertyKey(name);
+ const whereFrom = propertyKey;
switch (operator) {
case 'is': {
diff --git a/packages/db/src/services/overview.service.ts b/packages/db/src/services/overview.service.ts
index 83b0c17a9..78e40826b 100644
--- a/packages/db/src/services/overview.service.ts
+++ b/packages/db/src/services/overview.service.ts
@@ -55,6 +55,16 @@ const WHITELISTED_FILTERS = [
'utm_content',
];
+// Columns that exist on the sessions table but not on events — on events
+// they're stored inside the properties map under __query.utm_*.
+const UTM_COLUMNS = [
+ 'utm_source',
+ 'utm_medium',
+ 'utm_campaign',
+ 'utm_term',
+ 'utm_content',
+];
+
// Types
type MetricsRow = {
bounce_rate: number;
@@ -643,6 +653,13 @@ export class OverviewService {
}
return [item];
}
+ // events table has no top-level utm_* columns — those live in the
+ // properties map under the __query.utm_* keys. Route them through
+ // getEventFiltersWhereClause's properties.* path so we emit
+ // `properties['__query.utm_source']` instead of the bare column.
+ if (UTM_COLUMNS.includes(item.name)) {
+ return [{ ...item, name: `properties.__query.${item.name}` }];
+ }
return [item];
})
);
diff --git a/packages/trpc/src/routers/chart.ts b/packages/trpc/src/routers/chart.ts
index 9fe876a0b..0b1553f77 100644
--- a/packages/trpc/src/routers/chart.ts
+++ b/packages/trpc/src/routers/chart.ts
@@ -51,6 +51,17 @@ import {
publicProcedure,
} from '../trpc';
+// utm_* are surfaced as filterable "columns" in the UI but only exist on the
+// sessions table — on events they live inside the properties map under the
+// __query.utm_* keys.
+const UTM_COLUMNS = [
+ 'utm_source',
+ 'utm_medium',
+ 'utm_campaign',
+ 'utm_term',
+ 'utm_content',
+];
+
function utc(date: string | Date) {
if (typeof date === 'string') {
return date.replace('T', ' ').slice(0, 19);
@@ -385,9 +396,15 @@ export const chartRouter = createTRPCRouter({
const res = await query.execute();
values.push(...res.map((r) => String(r.values)).filter(Boolean));
} else {
+ // utm_* don't exist as columns on the events table — they live in
+ // the properties map. Route them through the properties code path
+ // so the SELECT becomes `properties['__query.utm_source']`.
+ const resolvedProperty = UTM_COLUMNS.includes(property)
+ ? `properties.__query.${property}`
+ : property;
const query = clix(ch)
.select<{ values: string[] }>([
- `distinct ${getSelectPropertyKey(property)} as values`,
+ `distinct ${getSelectPropertyKey(resolvedProperty)} as values`,
])
.from(TABLE_NAMES.events)
.where('project_id', '=', projectId)