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

134 lines
2.7 KiB
TypeScript
Raw Normal View History

2023-04-02 00:44:30 +02:00
import { NextApiResponse } from 'next';
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
import { WebsiteMetric, NextApiRequestQueryBody } from 'lib/types';
import { canViewWebsite } from 'lib/auth';
2022-10-12 22:11:44 +02:00
import { useAuth, useCors } from 'lib/middleware';
2023-04-02 00:44:30 +02:00
import { SESSION_COLUMNS, EVENT_COLUMNS, FILTER_COLUMNS } from 'lib/constants';
import { getPageviewMetrics, getSessionMetrics } from 'queries';
2022-11-18 07:46:05 +01:00
export interface WebsiteMetricsRequestQuery {
2022-11-15 22:21:14 +01:00
id: string;
type: string;
2022-12-27 02:36:48 +01:00
startAt: number;
endAt: number;
2022-11-15 22:21:14 +01:00
url: string;
referrer: string;
2023-03-31 14:55:28 +02:00
title: string;
2023-04-02 00:44:30 +02:00
query: string;
event: string;
2022-11-15 22:21:14 +01:00
os: string;
browser: string;
device: string;
country: string;
subdivision1: string;
subdivision2: string;
city: string;
2022-11-15 22:21:14 +01:00
}
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 {
id: websiteId,
type,
2022-12-27 02:36:48 +01:00
startAt,
endAt,
url,
referrer,
2023-03-31 14:55:28 +02:00
title,
2023-04-02 00:44:30 +02:00
query,
event,
os,
browser,
device,
country,
subdivision1,
subdivision2,
city,
} = req.query;
2022-10-12 22:11:44 +02:00
if (req.method === 'GET') {
if (!(await canViewWebsite(req.auth, websiteId))) {
return unauthorized(res);
}
2022-12-27 02:36:48 +01:00
const startDate = new Date(+startAt);
const endDate = new Date(+endAt);
2020-07-28 08:52:14 +02:00
2023-04-02 00:44:30 +02:00
if (SESSION_COLUMNS.includes(type)) {
const column = FILTER_COLUMNS[type] || type;
const filters = {
os,
browser,
device,
country,
subdivision1,
subdivision2,
city,
};
filters[type] = undefined;
2022-09-12 18:55:34 +02:00
let data = await getSessionMetrics(websiteId, {
startDate,
endDate,
2023-04-02 00:44:30 +02:00
column,
filters,
2022-04-10 12:51:43 +02:00
});
2021-12-02 04:25:00 +01:00
if (type === 'language') {
2023-02-08 01:29:25 +01:00
const combined = {};
2021-12-02 04:25:00 +01:00
2023-02-08 01:29:25 +01:00
for (const { x, y } of data) {
const key = String(x).toLowerCase().split('-')[0];
2022-04-10 12:51:43 +02:00
2023-04-02 00:44:30 +02:00
if (combined[key] === undefined) {
combined[key] = { x: key, y };
2022-04-10 12:51:43 +02:00
} else {
2023-02-08 01:29:25 +01:00
combined[key].y += y;
2022-04-10 12:51:43 +02:00
}
2021-12-02 04:25:00 +01:00
}
data = Object.values(combined);
}
return ok(res, data);
}
2023-04-02 00:44:30 +02:00
if (EVENT_COLUMNS.includes(type)) {
const column = FILTER_COLUMNS[type] || type;
const filters = {
2023-04-02 00:44:30 +02:00
url,
referrer,
title,
query,
event,
os,
browser,
device,
country,
subdivision1,
subdivision2,
city,
};
2023-04-02 00:44:30 +02:00
filters[type] = 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,
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
};