From 373e46ad702dae29a16525b1b2b96384806c6a6c Mon Sep 17 00:00:00 2001 From: Meerfolk Date: Wed, 29 Sep 2021 11:38:52 +0300 Subject: [PATCH] Add event_type filter for events endpoint --- lib/queries.js | 9 ++++++++- pages/api/website/[id]/events.js | 7 +++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/lib/queries.js b/lib/queries.js index 4e9ce892..ff9d557f 100644 --- a/lib/queries.js +++ b/lib/queries.js @@ -465,15 +465,21 @@ export function getEventMetrics( filters = {}, ) { const params = [website_id, start_at, end_at]; - const { url } = filters; + const { url, event_type } = filters; let urlFilter = ''; + let eventTypeFilter = ''; if (url) { urlFilter = `and url=$${params.length + 1}`; params.push(decodeURIComponent(url)); } + if (event_type) { + eventTypeFilter = `and event_type=$${params.length + 1}`; + params.push(event_type); + } + return rawQuery( ` select @@ -484,6 +490,7 @@ export function getEventMetrics( where website_id=$1 and created_at between $2 and $3 ${urlFilter} + ${eventTypeFilter} group by 1, 2 order by 2 `, diff --git a/pages/api/website/[id]/events.js b/pages/api/website/[id]/events.js index 7d98717a..7f348ee1 100644 --- a/pages/api/website/[id]/events.js +++ b/pages/api/website/[id]/events.js @@ -11,7 +11,7 @@ export default async (req, res) => { return unauthorized(res); } - const { id, start_at, end_at, unit, tz, url } = req.query; + const { id, start_at, end_at, unit, tz, url, event_type } = req.query; if (!moment.tz.zone(tz) || !unitTypes.includes(unit)) { return badRequest(res); @@ -21,7 +21,10 @@ export default async (req, res) => { const startDate = new Date(+start_at); const endDate = new Date(+end_at); - const events = await getEventMetrics(websiteId, startDate, endDate, tz, unit, { url }); + const events = await getEventMetrics(websiteId, startDate, endDate, tz, unit, { + url, + event_type, + }); return ok(res, events); }