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
Update queries to calculate correct metrics.
Mike Caoa24bee815c6c
diff --git a/lib/queries.js b/lib/queries.jsindex 9fd611253..c16f1ac0b 100644--- a/lib/queries.js+++ b/lib/queries.js@@ -333,12 +333,32 @@ export function getPageviews();}-export function getRankings(website_id, start_at, end_at, type, table, domain) {- const filter = domain ? `and ${type} not like '%${domain}%'` : '';+export function getSessionMetrics(website_id, start_at, end_at, field) {+ return rawQuery(+ `+ select ${field} x, count(*) y+ from session+ where session_id in (+ select session_id+ from pageview+ where website_id=$1+ and created_at between $2 and $3+ )+ group by 1+ order by 2 desc+ `,+ website_id,+ start_at,+ end_at,+ );+}++export function getPageviewMetrics(website_id, start_at, end_at, field, table, domain) {+ const filter = domain ? `and ${field} not like '%${domain}%'` : '';return rawQuery(`- select distinct ${type} x, count(*) y+ select ${field} x, count(*) yfrom ${table}where website_id=$1and created_at between $2 and $3diff --git a/pages/api/website/[id]/rankings.js b/pages/api/website/[id]/rankings.jsindex 90a0cd9f0..36669c4eb 100644--- a/pages/api/website/[id]/rankings.js+++ b/pages/api/website/[id]/rankings.js@@ -1,4 +1,4 @@-import { getRankings } from 'lib/queries';+import { getPageviewMetrics, getSessionMetrics } from 'lib/queries';import { ok, badRequest, methodNotAllowed, unauthorized } from 'lib/response';import { DOMAIN_REGEX } from 'lib/constants';import { allowQuery } from 'lib/auth';@@ -33,30 +33,32 @@ export default async (req, res) => {const { id, type, start_at, end_at, domain } = req.query;+ if (domain && !DOMAIN_REGEX.test(domain)) {+ return badRequest(res);+ }+const websiteId = +id;const startDate = new Date(+start_at);const endDate = new Date(+end_at);- if (- type !== 'event' &&- !sessionColumns.includes(type) &&- !pageviewColumns.includes(type) &&- domain &&- DOMAIN_REGEX.test(domain)- ) {- return badRequest(res);+ if (sessionColumns.includes(type)) {+ const data = await getSessionMetrics(websiteId, startDate, endDate, type);++ return ok(res, data);}- const rankings = await getRankings(- websiteId,- startDate,- endDate,- getColumn(type),- getTable(type),- domain,- );+ if (type === 'event' || pageviewColumns.includes(type)) {+ const data = await getPageviewMetrics(+ websiteId,+ startDate,+ endDate,+ getColumn(type),+ getTable(type),+ domain,+ );- return ok(res, rankings);+ return ok(res, data);+ }}return methodNotAllowed(res);