diff --git a/src/pages/api/websites/[websiteId]/metrics.ts b/src/pages/api/websites/[websiteId]/metrics.ts index 02e3a0095a..f9893e7900 100644 --- a/src/pages/api/websites/[websiteId]/metrics.ts +++ b/src/pages/api/websites/[websiteId]/metrics.ts @@ -87,7 +87,7 @@ export default async ( } if (SESSION_COLUMNS.includes(type)) { - const data = await getSessionMetrics(websiteId, column, filters, limit, offset); + const data = await getSessionMetrics(websiteId, type, filters, limit, offset); if (type === 'language') { const combined = {}; diff --git a/src/queries/analytics/sessions/getSessionMetrics.ts b/src/queries/analytics/sessions/getSessionMetrics.ts index 77ccb4105f..e28f1fb2e7 100644 --- a/src/queries/analytics/sessions/getSessionMetrics.ts +++ b/src/queries/analytics/sessions/getSessionMetrics.ts @@ -1,17 +1,11 @@ import prisma from 'lib/prisma'; import clickhouse from 'lib/clickhouse'; import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db'; -import { EVENT_TYPE, SESSION_COLUMNS } from 'lib/constants'; +import { EVENT_TYPE, FILTER_COLUMNS, SESSION_COLUMNS } from 'lib/constants'; import { QueryFilters } from 'lib/types'; export async function getSessionMetrics( - ...args: [ - websiteId: string, - column: string, - filters: QueryFilters, - limit?: number, - offset?: number, - ] + ...args: [websiteId: string, type: string, filters: QueryFilters, limit?: number, offset?: number] ) { return runQuery({ [PRISMA]: () => relationalQuery(...args), @@ -21,11 +15,12 @@ export async function getSessionMetrics( async function relationalQuery( websiteId: string, - column: string, + type: string, filters: QueryFilters, limit: number = 500, offset: number = 0, ) { + const column = FILTER_COLUMNS[type] || type; const { parseFilters, rawQuery } = prisma; const { filterQuery, joinSession, params } = await parseFilters( websiteId, @@ -34,7 +29,7 @@ async function relationalQuery( eventType: EVENT_TYPE.pageView, }, { - joinSession: SESSION_COLUMNS.includes(column), + joinSession: SESSION_COLUMNS.includes(type), }, ); const includeCountry = column === 'city' || column === 'subdivision1'; @@ -63,11 +58,12 @@ async function relationalQuery( async function clickhouseQuery( websiteId: string, - column: string, + type: string, filters: QueryFilters, limit: number = 500, offset: number = 0, ): Promise<{ x: string; y: number }[]> { + const column = FILTER_COLUMNS[type] || type; const { parseFilters, rawQuery } = clickhouse; const { filterQuery, params } = await parseFilters(websiteId, { ...filters,