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

135 lines
2.9 KiB
TypeScript
Raw Normal View History

2023-04-01 22:44:30 +00:00
import { NextApiResponse } from 'next';
2023-08-01 06:08:28 +00:00
import { badRequest, methodNotAllowed, ok, unauthorized } from 'next-basics';
import { WebsiteMetric, NextApiRequestQueryBody } from 'lib/types';
import { canViewWebsite } from 'lib/auth';
2023-08-20 05:23:15 +00:00
import { useAuth, useCors, useValidate } from 'lib/middleware';
2023-04-01 22:44:30 +00:00
import { SESSION_COLUMNS, EVENT_COLUMNS, FILTER_COLUMNS } from 'lib/constants';
import { getPageviewMetrics, getSessionMetrics } from 'queries';
2023-07-26 06:59:08 +00:00
import { parseDateRangeQuery } from 'lib/query';
2023-08-20 05:23:15 +00:00
import * as yup from 'yup';
2022-11-18 06:46:05 +00:00
export interface WebsiteMetricsRequestQuery {
2022-11-15 21:21:14 +00:00
id: string;
type: string;
2022-12-27 01:36:48 +00:00
startAt: number;
endAt: number;
url?: string;
2023-08-29 03:09:54 +00:00
referrer?: string;
title?: string;
query?: string;
os?: string;
browser?: string;
device?: string;
country?: string;
region?: string;
city?: string;
language?: string;
event?: string;
2022-11-15 21:21:14 +00:00
}
2023-08-20 05:23:15 +00:00
const schema = {
GET: yup.object().shape({
id: yup.string().uuid().required(),
2023-08-29 03:09:54 +00:00
type: yup.string().required(),
startAt: yup.number().required(),
endAt: yup.number().required(),
url: yup.string(),
2023-09-25 20:19:56 +00:00
referrer: yup.string(),
title: yup.string(),
query: yup.string(),
os: yup.string(),
browser: yup.string(),
device: yup.string(),
country: yup.string(),
region: yup.string(),
city: yup.string(),
language: yup.string(),
event: yup.string(),
2023-08-20 05:23:15 +00:00
}),
};
2022-11-15 21:21:14 +00:00
export default async (
2022-11-18 06:46:05 +00:00
req: NextApiRequestQueryBody<WebsiteMetricsRequestQuery>,
2022-11-15 21:21:14 +00:00
res: NextApiResponse<WebsiteMetric[]>,
) => {
2022-10-12 20:11:44 +00:00
await useCors(req, res);
await useAuth(req, res);
2023-09-30 03:24:48 +00:00
await useValidate(schema, req, res);
2023-08-20 05:23:15 +00:00
const {
id: websiteId,
type,
url,
referrer,
2023-03-31 12:55:28 +00:00
title,
2023-04-01 22:44:30 +00:00
query,
os,
browser,
device,
country,
2023-04-14 05:28:29 +00:00
region,
city,
2023-08-04 07:51:52 +00:00
language,
2023-08-29 03:09:54 +00:00
event,
} = req.query;
2022-10-12 20:11:44 +00:00
if (req.method === 'GET') {
if (!(await canViewWebsite(req.auth, websiteId))) {
return unauthorized(res);
}
2023-07-26 06:59:08 +00:00
const { startDate, endDate } = await parseDateRangeQuery(req);
2020-07-28 06:52:14 +00:00
2023-08-04 07:51:52 +00:00
const filters = {
2023-08-04 20:18:30 +00:00
startDate,
endDate,
url,
2023-08-04 07:51:52 +00:00
referrer,
title,
query,
os,
browser,
device,
country,
region,
city,
language,
2023-08-29 03:09:54 +00:00
event,
2023-08-04 07:51:52 +00:00
};
const column = FILTER_COLUMNS[type] || type;
2023-04-01 22:44:30 +00:00
2023-08-04 07:51:52 +00:00
if (SESSION_COLUMNS.includes(type)) {
2023-08-04 20:18:30 +00:00
const data = await getSessionMetrics(websiteId, column, filters);
2021-12-02 03:25:00 +00:00
if (type === 'language') {
2023-02-08 00:29:25 +00:00
const combined = {};
2021-12-02 03:25:00 +00:00
2023-02-08 00:29:25 +00:00
for (const { x, y } of data) {
const key = String(x).toLowerCase().split('-')[0];
2022-04-10 10:51:43 +00:00
2023-04-01 22:44:30 +00:00
if (combined[key] === undefined) {
combined[key] = { x: key, y };
2022-04-10 10:51:43 +00:00
} else {
2023-02-08 00:29:25 +00:00
combined[key].y += y;
2022-04-10 10:51:43 +00:00
}
2021-12-02 03:25:00 +00:00
}
2023-08-02 21:21:13 +00:00
return ok(res, Object.values(combined));
2021-12-02 03:25:00 +00:00
}
return ok(res, data);
}
2023-04-01 22:44:30 +00:00
if (EVENT_COLUMNS.includes(type)) {
2023-08-04 20:18:30 +00:00
const data = await getPageviewMetrics(websiteId, column, filters);
2020-09-11 20:49:43 +00:00
return ok(res, data);
}
2023-08-01 06:08:28 +00:00
return badRequest(res);
2020-09-11 20:49:43 +00:00
}
return methodNotAllowed(res);
2020-07-28 06:52:14 +00:00
};