mirror of
https://github.com/umami-software/umami.git
synced 2026-02-20 12:35:38 +01:00
eventdata api
This commit is contained in:
parent
b948f0d381
commit
67394194af
9 changed files with 230 additions and 14 deletions
|
|
@ -58,13 +58,11 @@ export default async (req, res) => {
|
|||
|
||||
await useSession(req, res);
|
||||
|
||||
const {
|
||||
session: { website, session },
|
||||
} = req;
|
||||
const { website, session } = req.session;
|
||||
|
||||
const { type, payload } = getJsonBody(req);
|
||||
|
||||
let { url, referrer, eventName, eventData } = payload;
|
||||
let { url, referrer, event_name: eventName, event_data: eventData } = payload;
|
||||
|
||||
if (process.env.REMOVE_TRAILING_SLASH) {
|
||||
url = url.replace(/\/$/, '');
|
||||
|
|
@ -88,9 +86,8 @@ export default async (req, res) => {
|
|||
|
||||
const token = createToken(
|
||||
{
|
||||
websiteId: website.websiteUuid,
|
||||
sessionId: session.sessionId,
|
||||
sessionUuid: session.sessionUuid,
|
||||
website,
|
||||
session,
|
||||
},
|
||||
secret(),
|
||||
);
|
||||
|
|
|
|||
50
pages/api/websites/[id]/eventdata.js
Normal file
50
pages/api/websites/[id]/eventdata.js
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
import moment from 'moment-timezone';
|
||||
import { getEventData } from 'queries';
|
||||
import { ok, badRequest, methodNotAllowed, unauthorized } from 'next-basics';
|
||||
import { allowQuery } from 'lib/auth';
|
||||
import { useAuth, useCors } from 'lib/middleware';
|
||||
|
||||
const unitTypes = ['year', 'month', 'hour', 'day'];
|
||||
|
||||
export default async (req, res) => {
|
||||
await useCors(req, res);
|
||||
await useAuth(req, res);
|
||||
|
||||
if (req.method === 'POST') {
|
||||
if (!(await allowQuery(req))) {
|
||||
return unauthorized(res);
|
||||
}
|
||||
|
||||
const {
|
||||
website_id: websiteId,
|
||||
start_at,
|
||||
end_at,
|
||||
unit,
|
||||
timezone,
|
||||
event_name: eventName,
|
||||
columns,
|
||||
filters,
|
||||
} = req.body;
|
||||
|
||||
if (!moment.tz.zone(timezone) || !unitTypes.includes(unit)) {
|
||||
return badRequest(res);
|
||||
}
|
||||
|
||||
const startDate = new Date(+start_at);
|
||||
const endDate = new Date(+end_at);
|
||||
|
||||
const events = await getEventData(websiteId, {
|
||||
startDate,
|
||||
endDate,
|
||||
timezone,
|
||||
unit,
|
||||
eventName,
|
||||
columns,
|
||||
filters,
|
||||
});
|
||||
|
||||
return ok(res, events);
|
||||
}
|
||||
|
||||
return methodNotAllowed(res);
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue