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