From f69dbaeaf654a532dad25077ee9b09356095ae4d Mon Sep 17 00:00:00 2001 From: Mike Cao Date: Wed, 13 Apr 2022 19:04:06 -0700 Subject: [PATCH] Allow filtering of events with url. --- lib/queries.js | 14 +++++++++----- pages/api/website/[id]/metrics.js | 3 ++- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/lib/queries.js b/lib/queries.js index ffbb6655..bb6b9395 100644 --- a/lib/queries.js +++ b/lib/queries.js @@ -64,7 +64,7 @@ export function getFilterQuery(table, filters = {}, params = []) { switch (key) { case 'url': - if (table === 'session' || table === 'pageview') { + if (table === 'pageview' || table === 'event') { arr.push(`and ${table}.${key}=$${params.length + 1}`); params.push(decodeURIComponent(value)); } @@ -110,11 +110,11 @@ export function getFilterQuery(table, filters = {}, params = []) { } export function parseFilters(table, filters = {}, params = []) { - const { domain, url, referrer, os, browser, device, country, event_type } = filters; + const { domain, url, event_url, referrer, os, browser, device, country, event_type } = filters; const pageviewFilters = { domain, url, referrer }; const sessionFilters = { os, browser, device, country }; - const eventFilters = { event_type }; + const eventFilters = { url: event_url, event_type }; return { pageviewFilters, @@ -502,8 +502,11 @@ export function getSessionMetrics(website_id, start_at, end_at, field, filters = export function getPageviewMetrics(website_id, start_at, end_at, field, table, filters = {}) { const params = [website_id, start_at, end_at]; - console.log({ table, filters }); - const { pageviewQuery, sessionQuery, joinSession } = parseFilters(table, filters, params); + const { pageviewQuery, sessionQuery, eventQuery, joinSession } = parseFilters( + table, + filters, + params, + ); return rawQuery( ` @@ -514,6 +517,7 @@ export function getPageviewMetrics(website_id, start_at, end_at, field, table, f and ${table}.created_at between $2 and $3 ${pageviewQuery} ${joinSession && sessionQuery} + ${eventQuery} group by 1 order by 2 desc `, diff --git a/pages/api/website/[id]/metrics.js b/pages/api/website/[id]/metrics.js index 645a8707..54c5bf3e 100644 --- a/pages/api/website/[id]/metrics.js +++ b/pages/api/website/[id]/metrics.js @@ -83,12 +83,13 @@ export default async (req, res) => { const data = await getPageviewMetrics(websiteId, startDate, endDate, column, table, { domain, - url: type !== 'url' ? url : undefined, + url: type !== 'url' && table !== 'event' ? url : undefined, referrer: type !== 'referrer' ? referrer : undefined, os: type !== 'os' ? os : undefined, browser: type !== 'browser' ? browser : undefined, device: type !== 'device' ? device : undefined, country: type !== 'country' ? country : undefined, + event_url: type !== 'url' && table === 'event' ? url : undefined, }); return ok(res, data);