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';
|
|
|
|
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';
|
2022-10-25 19:45:56 +02:00
|
|
|
import { getPageviewMetrics, getSessionMetrics, getWebsite } from 'queries';
|
2020-07-28 08:52:14 +02:00
|
|
|
|
2023-03-20 19:26:45 +01:00
|
|
|
const sessionColumns = [
|
|
|
|
'browser',
|
|
|
|
'os',
|
|
|
|
'device',
|
|
|
|
'screen',
|
|
|
|
'country',
|
|
|
|
'language',
|
|
|
|
'subdivision1',
|
|
|
|
'subdivision2',
|
|
|
|
'city',
|
|
|
|
];
|
2023-03-01 19:53:57 +01:00
|
|
|
const pageviewColumns = ['url', 'referrer', 'query', 'pageTitle'];
|
2020-10-09 00:02:48 +02:00
|
|
|
|
|
|
|
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');
|
2020-10-09 00:02:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function getColumn(type) {
|
2023-03-20 19:26:45 +01:00
|
|
|
switch (type) {
|
|
|
|
case 'url':
|
|
|
|
return 'url_path';
|
|
|
|
case 'referrer':
|
|
|
|
return 'referrer_domain';
|
|
|
|
case 'event':
|
|
|
|
return 'event_name';
|
|
|
|
case 'query':
|
|
|
|
return 'url_query';
|
2020-10-09 00:02:48 +02:00
|
|
|
}
|
2023-03-20 19:26:45 +01:00
|
|
|
|
2020-10-09 00:02:48 +02:00
|
|
|
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;
|
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-01 19:53:57 +01:00
|
|
|
pageTitle: string;
|
2022-11-15 22:21:14 +01:00
|
|
|
os: string;
|
|
|
|
browser: string;
|
|
|
|
device: string;
|
|
|
|
country: string;
|
2023-02-20 18:04:20 +01:00
|
|
|
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
|
|
|
|
2022-12-02 05:53:37 +01:00
|
|
|
const {
|
|
|
|
id: websiteId,
|
|
|
|
type,
|
2022-12-27 02:36:48 +01:00
|
|
|
startAt,
|
|
|
|
endAt,
|
2022-12-02 05:53:37 +01:00
|
|
|
url,
|
|
|
|
referrer,
|
2023-03-01 19:53:57 +01:00
|
|
|
pageTitle,
|
2022-12-02 05:53:37 +01:00
|
|
|
os,
|
|
|
|
browser,
|
|
|
|
device,
|
|
|
|
country,
|
2023-02-20 18:04:20 +01:00
|
|
|
subdivision1,
|
|
|
|
subdivision2,
|
|
|
|
city,
|
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);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2020-10-09 00:02:48 +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,
|
2023-02-20 18:04:20 +01:00
|
|
|
subdivision1,
|
|
|
|
subdivision2,
|
|
|
|
city,
|
2022-09-12 18:55:34 +02:00
|
|
|
},
|
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-02-08 01:29:25 +01:00
|
|
|
if (!combined[key]) {
|
|
|
|
combined[key] = { x, 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);
|
|
|
|
}
|
2020-07-30 06:40:26 +02:00
|
|
|
|
2020-10-09 00:02:48 +02:00
|
|
|
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);
|
2022-07-21 10:11:10 +02:00
|
|
|
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,
|
2022-07-23 08:56:29 +02:00
|
|
|
referrer: type !== 'referrer' && table !== 'event' ? referrer : FILTER_IGNORED,
|
2023-03-01 19:53:57 +01:00
|
|
|
pageTitle: type !== 'pageTitle' && table !== 'event' ? pageTitle : undefined,
|
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,
|
2023-02-20 18:04:20 +01:00
|
|
|
subdivision1: type !== 'subdivision1' ? subdivision1 : undefined,
|
|
|
|
subdivision2: type !== 'subdivision2' ? subdivision2 : undefined,
|
|
|
|
city: type !== 'city' ? city : undefined,
|
2022-12-27 08:21:54 +01:00
|
|
|
eventUrl: type !== 'url' && table === 'event' ? url : undefined,
|
2022-08-08 10:26:20 +02:00
|
|
|
query: type === 'query' && table !== 'event' ? true : undefined,
|
2022-07-21 10:11:10 +02:00
|
|
|
};
|
|
|
|
|
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,
|
2023-01-06 04:39:29 +01:00
|
|
|
type,
|
2022-09-12 18:55:34 +02:00
|
|
|
});
|
2020-09-11 22:49:43 +02:00
|
|
|
|
2020-10-09 00:02:48 +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
|
|
|
};
|