2023-04-01 22:44:30 +00:00
|
|
|
import { NextApiResponse } from 'next';
|
|
|
|
|
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
|
2022-12-09 07:43:43 +00:00
|
|
|
import { WebsiteMetric, NextApiRequestQueryBody } from 'lib/types';
|
2022-12-02 04:53:37 +00:00
|
|
|
import { canViewWebsite } from 'lib/auth';
|
2022-10-12 20:11:44 +00:00
|
|
|
import { useAuth, useCors } 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';
|
2020-10-08 22:02:48 +00:00
|
|
|
|
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;
|
2022-11-15 21:21:14 +00:00
|
|
|
url: string;
|
|
|
|
|
referrer: string;
|
2023-03-31 12:55:28 +00:00
|
|
|
title: string;
|
2023-04-01 22:44:30 +00:00
|
|
|
query: string;
|
|
|
|
|
event: string;
|
2022-11-15 21:21:14 +00:00
|
|
|
os: string;
|
|
|
|
|
browser: string;
|
|
|
|
|
device: string;
|
|
|
|
|
country: string;
|
2023-04-14 05:28:29 +00:00
|
|
|
region: string;
|
2023-02-20 17:04:20 +00:00
|
|
|
city: string;
|
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);
|
2022-04-04 07:33:20 +00:00
|
|
|
|
2022-12-02 04:53:37 +00:00
|
|
|
const {
|
|
|
|
|
id: websiteId,
|
|
|
|
|
type,
|
2022-12-27 01:36:48 +00:00
|
|
|
startAt,
|
|
|
|
|
endAt,
|
2022-12-02 04:53:37 +00:00
|
|
|
url,
|
|
|
|
|
referrer,
|
2023-03-31 12:55:28 +00:00
|
|
|
title,
|
2023-04-01 22:44:30 +00:00
|
|
|
query,
|
|
|
|
|
event,
|
2022-12-02 04:53:37 +00:00
|
|
|
os,
|
|
|
|
|
browser,
|
|
|
|
|
device,
|
|
|
|
|
country,
|
2023-04-14 05:28:29 +00:00
|
|
|
region,
|
2023-02-20 17:04:20 +00:00
|
|
|
city,
|
2022-12-02 04:53:37 +00:00
|
|
|
} = req.query;
|
|
|
|
|
|
2022-10-12 20:11:44 +00:00
|
|
|
if (req.method === 'GET') {
|
2022-12-27 23:18:58 +00:00
|
|
|
if (!(await canViewWebsite(req.auth, websiteId))) {
|
2020-09-18 05:52:20 +00:00
|
|
|
return unauthorized(res);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-27 01:36:48 +00:00
|
|
|
const startDate = new Date(+startAt);
|
|
|
|
|
const endDate = new Date(+endAt);
|
2020-07-28 06:52:14 +00:00
|
|
|
|
2023-04-01 22:44:30 +00:00
|
|
|
if (SESSION_COLUMNS.includes(type)) {
|
|
|
|
|
const column = FILTER_COLUMNS[type] || type;
|
|
|
|
|
const filters = {
|
|
|
|
|
os,
|
|
|
|
|
browser,
|
|
|
|
|
device,
|
|
|
|
|
country,
|
2023-04-14 05:28:29 +00:00
|
|
|
region,
|
2023-04-01 22:44:30 +00:00
|
|
|
city,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
filters[type] = undefined;
|
|
|
|
|
|
2022-09-12 16:55:34 +00:00
|
|
|
let data = await getSessionMetrics(websiteId, {
|
|
|
|
|
startDate,
|
|
|
|
|
endDate,
|
2023-04-01 22:44:30 +00:00
|
|
|
column,
|
|
|
|
|
filters,
|
2022-04-10 10:51:43 +00:00
|
|
|
});
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data = Object.values(combined);
|
|
|
|
|
}
|
2020-07-30 04:40:26 +00:00
|
|
|
|
2020-10-08 22:02:48 +00:00
|
|
|
return ok(res, data);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-01 22:44:30 +00:00
|
|
|
if (EVENT_COLUMNS.includes(type)) {
|
|
|
|
|
const column = FILTER_COLUMNS[type] || type;
|
2022-07-21 08:11:10 +00:00
|
|
|
const filters = {
|
2023-04-01 22:44:30 +00:00
|
|
|
url,
|
|
|
|
|
referrer,
|
|
|
|
|
title,
|
|
|
|
|
query,
|
|
|
|
|
event,
|
|
|
|
|
os,
|
|
|
|
|
browser,
|
|
|
|
|
device,
|
|
|
|
|
country,
|
2023-04-14 05:28:29 +00:00
|
|
|
region,
|
2023-04-01 22:44:30 +00:00
|
|
|
city,
|
2022-07-21 08:11:10 +00:00
|
|
|
};
|
|
|
|
|
|
2023-04-01 22:44:30 +00:00
|
|
|
filters[type] = undefined;
|
|
|
|
|
|
2022-10-11 00:01:48 +00:00
|
|
|
const data = await getPageviewMetrics(websiteId, {
|
2022-09-12 16:55:34 +00:00
|
|
|
startDate,
|
|
|
|
|
endDate,
|
|
|
|
|
column,
|
|
|
|
|
filters,
|
|
|
|
|
});
|
2020-09-11 20:49:43 +00:00
|
|
|
|
2020-10-08 22:02:48 +00:00
|
|
|
return ok(res, data);
|
|
|
|
|
}
|
2020-09-11 20:49:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return methodNotAllowed(res);
|
2020-07-28 06:52:14 +00:00
|
|
|
};
|