umami/pages/api/websites/[id]/metrics.ts

155 lines
3.5 KiB
TypeScript
Raw Normal View History

import { WebsiteMetric, NextApiRequestQueryBody } from 'lib/types';
import { canViewWebsite } from 'lib/auth';
import { FILTER_IGNORED } from 'lib/constants';
2022-10-12 22:11:44 +02:00
import { useAuth, useCors } from 'lib/middleware';
2022-11-15 22:21:14 +01:00
import { NextApiResponse } from 'next';
2022-10-12 04:37:38 +02:00
import { badRequest, methodNotAllowed, ok, unauthorized } from 'next-basics';
import { getPageviewMetrics, getSessionMetrics, getWebsite } from 'queries';
2020-07-28 08:52:14 +02:00
2022-07-16 07:29:55 +02:00
const sessionColumns = ['browser', 'os', 'device', 'screen', 'country', 'language'];
2022-08-08 10:26:20 +02:00
const pageviewColumns = ['url', 'referrer', 'query'];
function getTable(type) {
if (type === 'event') {
return 'event';
}
if (sessionColumns.includes(type)) {
return 'session';
}
2022-08-08 10:26:20 +02:00
if (pageviewColumns.includes(type)) {
return 'pageview';
}
throw new Error('Invalid type');
}
function getColumn(type) {
if (type === 'event') {
2022-08-08 10:26:20 +02:00
return 'event_name';
}
if (type === 'query') {
return 'url';
}
return type;
}
2022-11-18 07:46:05 +01:00
export interface WebsiteMetricsRequestQuery {
2022-11-15 22:21:14 +01:00
id: string;
type: string;
start_at: number;
end_at: number;
url: string;
referrer: string;
os: string;
browser: string;
device: string;
country: string;
}
export default async (
2022-11-18 07:46:05 +01:00
req: NextApiRequestQueryBody<WebsiteMetricsRequestQuery>,
2022-11-15 22:21:14 +01:00
res: NextApiResponse<WebsiteMetric[]>,
) => {
2022-10-12 22:11:44 +02:00
await useCors(req, res);
await useAuth(req, res);
2022-04-04 09:33:20 +02:00
const {
user: { id: userId },
} = req.auth;
const {
id: websiteId,
type,
start_at,
end_at,
url,
referrer,
os,
browser,
device,
country,
} = req.query;
2022-10-12 22:11:44 +02:00
if (req.method === 'GET') {
if (!(await canViewWebsite(userId, websiteId))) {
return unauthorized(res);
}
2020-09-11 22:49:43 +02:00
const startDate = new Date(+start_at);
const endDate = new Date(+end_at);
2020-07-28 08:52:14 +02:00
if (sessionColumns.includes(type)) {
2022-09-12 18:55:34 +02:00
let data = await getSessionMetrics(websiteId, {
startDate,
endDate,
field: type,
filters: {
os,
browser,
device,
country,
},
2022-04-10 12:51:43 +02:00
});
2021-12-02 04:25:00 +01:00
if (type === 'language') {
let combined = {};
for (let { x, y } of data) {
x = String(x).toLowerCase().split('-')[0];
2022-04-10 12:51:43 +02:00
if (!combined[x]) {
combined[x] = { x, y };
} else {
combined[x].y += y;
}
2021-12-02 04:25:00 +01:00
}
data = Object.values(combined);
}
return ok(res, data);
}
2021-02-27 04:50:44 +01:00
if (pageviewColumns.includes(type) || type === 'event') {
let domain;
2022-08-08 10:26:20 +02:00
2021-02-27 04:50:44 +01:00
if (type === 'referrer') {
2022-11-01 07:42:37 +01:00
const website = await getWebsite({ id: websiteId });
2021-02-27 04:50:44 +01:00
if (!website) {
return badRequest(res);
}
domain = website.domain;
}
2022-04-11 09:19:29 +02:00
const column = getColumn(type);
const table = getTable(type);
const filters = {
2022-04-11 09:19:29 +02:00
domain,
2022-04-14 04:04:06 +02:00
url: type !== 'url' && table !== 'event' ? url : undefined,
referrer: type !== 'referrer' && table !== 'event' ? referrer : FILTER_IGNORED,
2022-04-11 09:19:29 +02:00
os: type !== 'os' ? os : undefined,
browser: type !== 'browser' ? browser : undefined,
device: type !== 'device' ? device : undefined,
country: type !== 'country' ? country : undefined,
2022-04-14 04:04:06 +02:00
event_url: type !== 'url' && table === 'event' ? url : undefined,
2022-08-08 10:26:20 +02:00
query: type === 'query' && table !== 'event' ? true : undefined,
};
2022-10-11 02:01:48 +02:00
const data = await getPageviewMetrics(websiteId, {
2022-09-12 18:55:34 +02:00
startDate,
endDate,
column,
table,
filters,
});
2020-09-11 22:49:43 +02:00
return ok(res, data);
}
2020-09-11 22:49:43 +02:00
}
return methodNotAllowed(res);
2020-07-28 08:52:14 +02:00
};