umami/pages/api/website/[id]/rankings.js

29 lines
860 B
JavaScript
Raw Normal View History

2020-08-27 12:42:24 +02:00
import { getRankings, getEventRankings } from 'lib/queries';
import { ok, badRequest } from 'lib/response';
2020-08-01 04:05:14 +02:00
2020-08-07 04:37:19 +02:00
const sessionColumns = ['browser', 'os', 'device', 'country'];
2020-08-01 04:05:14 +02:00
const pageviewColumns = ['url', 'referrer'];
export default async (req, res) => {
const { id, type, start_at, end_at } = req.query;
2020-08-25 08:49:14 +02:00
const websiteId = +id;
const startDate = new Date(+start_at);
const endDate = new Date(+end_at);
2020-08-01 04:05:14 +02:00
2020-08-25 08:49:14 +02:00
if (type !== 'event' && !sessionColumns.includes(type) && !pageviewColumns.includes(type)) {
return badRequest(res);
2020-08-01 04:05:14 +02:00
}
2020-08-25 08:49:14 +02:00
if (type === 'event') {
2020-08-27 12:42:24 +02:00
const events = await getEventRankings(websiteId, startDate, endDate);
2020-08-25 08:49:14 +02:00
return ok(res, events);
}
2020-08-01 04:05:14 +02:00
const table = sessionColumns.includes(type) ? 'session' : 'pageview';
2020-08-25 08:49:14 +02:00
const rankings = await getRankings(websiteId, startDate, endDate, type, table);
2020-08-01 04:05:14 +02:00
return ok(res, rankings);
2020-08-01 04:05:14 +02:00
};