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

21 lines
644 B
JavaScript
Raw Normal View History

import moment from 'moment-timezone';
2020-07-28 08:52:14 +02:00
import { getPageviewData } from 'lib/db';
import { useAuth } from 'lib/middleware';
2020-07-26 09:12:42 +02:00
export default async (req, res) => {
2020-07-28 08:52:14 +02:00
await useAuth(req, res);
2020-07-26 09:12:42 +02:00
const { id, start_at, end_at, unit, tz } = req.query;
2020-07-26 09:12:42 +02:00
if (!moment.tz.zone(tz) || !['hour', 'day'].includes(unit)) {
return res.status(400).end();
}
2020-07-28 08:52:14 +02:00
const [pageviews, uniques] = await Promise.all([
getPageviewData(+id, new Date(+start_at), new Date(+end_at), tz, unit, '*'),
getPageviewData(+id, new Date(+start_at), new Date(+end_at), tz, unit, 'distinct session_id'),
2020-07-28 08:52:14 +02:00
]);
return res.status(200).json({ pageviews, uniques });
2020-07-26 09:12:42 +02:00
};