Umami is a privacy-first web and product analytics platform. Traffic, campaigns, behavior, conversions, and revenue in one place. No cookies, no surveillance, self-hosted or in the cloud.
TypeScript analyticsaudience-segmentationchartscohort-analysisgoogle-analyticsproduct-analyticsstatisticsuser-journeyweb-analytics
feat: API for event data now can return associated event name
Aitor Alonso92fc4e87c788
diff --git a/.gitignore b/.gitignoreindex 7066fb28f..99087ab50 100644--- a/.gitignore+++ b/.gitignore@@ -25,6 +25,7 @@ node_modules*.iml*.log.vscode+.tool-versions# debugnpm-debug.log*diff --git a/pages/api/event-data/fields.ts b/pages/api/event-data/fields.tsindex 18b74bc32..ca0f23d4f 100644--- a/pages/api/event-data/fields.ts+++ b/pages/api/event-data/fields.ts@@ -21,13 +21,19 @@ export default async (await useAuth(req, res);if (req.method === 'GET') {- const { websiteId, startAt, endAt, field } = req.query;+ const { websiteId, startAt, endAt, field, withEventNames } = req.query;if (!(await canViewWebsite(req.auth, websiteId))) {return unauthorized(res);}- const data = await getEventDataFields(websiteId, new Date(+startAt), new Date(+endAt), field);+ const data = await getEventDataFields(+ websiteId,+ new Date(+startAt),+ new Date(+endAt),+ field,+ withEventNames,+ );return ok(res, data);}diff --git a/queries/analytics/eventData/getEventDataFields.ts b/queries/analytics/eventData/getEventDataFields.tsindex 6306d239e..1df6db124 100644--- a/queries/analytics/eventData/getEventDataFields.ts+++ b/queries/analytics/eventData/getEventDataFields.ts@@ -6,7 +6,13 @@ import { loadWebsite } from 'lib/query';import { DEFAULT_CREATED_AT } from 'lib/constants';export async function getEventDataFields(- ...args: [websiteId: string, startDate: Date, endDate: Date, field?: string]+ ...args: [+ websiteId: string,+ startDate: Date,+ endDate: Date,+ field?: string,+ withEventNames?: boolean,+ ]): Promise<WebsiteEventDataFields[]> {return runQuery({[PRISMA]: () => relationalQuery(...args),@@ -14,7 +20,13 @@ export async function getEventDataFields(});}-async function relationalQuery(websiteId: string, startDate: Date, endDate: Date, field: string) {+async function relationalQuery(+ websiteId: string,+ startDate: Date,+ endDate: Date,+ field: string,+ withEventNames: boolean,+) {const { toUuid, rawQuery } = prisma;const website = await loadWebsite(websiteId);const resetDate = new Date(website?.resetAt || DEFAULT_CREATED_AT);@@ -31,12 +43,30 @@ async function relationalQuery(websiteId: string, startDate: Date, endDate: Dateand created_at between $4 and $5group by event_key, string_valueorder by 3 desc, 2 desc, 1 asc- limit 100`,[websiteId, field, resetDate, startDate, endDate] as any,);}+ if (withEventNames) {+ return rawQuery(+ `select+ ed.event_key as field,+ ed.data_type as type,+ count(ed.*) as total,+ e.event_name as event+ from event_data as ed+ join website_event as e on e.event_id = ed.website_event_id+ where ed.website_id = $1${toUuid()}+ and ed.created_at >= $2+ and ed.created_at between $3 and $4+ group by e.event_name, ed.event_key, ed.data_type+ order by 3 desc, 2 asc, 1 asc+ `,+ [websiteId, resetDate, startDate, endDate] as any,+ );+ }+return rawQuery(`selectevent_key as field,@@ -48,13 +78,18 @@ async function relationalQuery(websiteId: string, startDate: Date, endDate: Dateand created_at between $3 and $4group by event_key, data_typeorder by 3 desc, 2 asc, 1 asc- limit 100`,[websiteId, resetDate, startDate, endDate] as any,);}-async function clickhouseQuery(websiteId: string, startDate: Date, endDate: Date, field: string) {+async function clickhouseQuery(+ websiteId: string,+ startDate: Date,+ endDate: Date,+ field: string,+ withEventNames: boolean,+) {const { rawQuery, getDateFormat, getBetweenDates } = clickhouse;const website = await loadWebsite(websiteId);const resetDate = new Date(website?.resetAt || DEFAULT_CREATED_AT);@@ -72,12 +107,30 @@ async function clickhouseQuery(websiteId: string, startDate: Date, endDate: Dateand ${getBetweenDates('created_at', startDate, endDate)}group by event_key, string_valueorder by 3 desc, 2 desc, 1 asc- limit 100`,{ websiteId, field },);}+ if (withEventNames) {+ return rawQuery(+ `select+ ed.event_key as field,+ ed.data_type as type,+ count(ed.*) as total,+ e.event_name as event+ from event_data as ed+ join website_event as e on e.event_id = ed.website_event_id+ where website_id = {websiteId:UUID}+ and created_at >= ${getDateFormat(resetDate)}+ and ${getBetweenDates('created_at', startDate, endDate)}+ group by e.event_name, ed.event_key, ed.data_type+ order by 3 desc, 2 asc, 1 asc+ `,+ [websiteId, resetDate, startDate, endDate] as any,+ );+ }+return rawQuery(`selectevent_key as field,@@ -89,7 +142,6 @@ async function clickhouseQuery(websiteId: string, startDate: Date, endDate: Dateand ${getBetweenDates('created_at', startDate, endDate)}group by event_key, data_typeorder by 3 desc, 2 asc, 1 asc- limit 100`,{ websiteId },);