umami/pages/api/collect.js

32 lines
818 B
JavaScript
Raw Normal View History

2020-07-19 10:57:01 +02:00
import { savePageView, saveEvent } from 'lib/db';
import { allowPost } from 'lib/middleware';
import checkSession from 'lib/session';
2020-07-23 05:45:09 +02:00
import { createToken } from 'lib/crypto';
2020-07-17 10:03:38 +02:00
export default async (req, res) => {
await allowPost(req, res);
const session = await checkSession(req);
2020-07-17 10:03:38 +02:00
2020-07-24 04:56:55 +02:00
const token = await createToken(session);
2020-07-23 05:45:09 +02:00
const { website_id, session_id } = session;
const { type, payload } = req.body;
2020-07-24 04:56:55 +02:00
let ok = false;
2020-07-17 10:03:38 +02:00
if (type === 'pageview') {
const { url, referrer } = payload;
2020-07-24 04:56:55 +02:00
await savePageView(website_id, session_id, url, referrer);
ok = true;
} else if (type === 'event') {
const { url, event_type, event_value } = payload;
2020-07-17 10:03:38 +02:00
2020-07-24 04:56:55 +02:00
await saveEvent(website_id, session_id, url, event_type, event_value);
ok = true;
}
2020-07-23 05:45:09 +02:00
res.status(200).json({ ok, session: token });
2020-07-17 10:03:38 +02:00
};