2020-07-20 10:54:21 +02:00
|
|
|
import { parseSession } from 'lib/utils';
|
2020-07-19 10:57:01 +02:00
|
|
|
import { savePageView, saveEvent } from 'lib/db';
|
2020-07-18 19:36:46 +02:00
|
|
|
import { allowPost } from 'lib/middleware';
|
2020-07-20 10:54:21 +02:00
|
|
|
import checkSession from 'lib/session';
|
2020-07-17 10:03:38 +02:00
|
|
|
|
|
|
|
export default async (req, res) => {
|
2020-07-18 19:36:46 +02:00
|
|
|
await allowPost(req, res);
|
|
|
|
|
2020-07-20 10:54:21 +02:00
|
|
|
const session = await checkSession(req);
|
2020-07-17 10:03:38 +02:00
|
|
|
|
2020-07-20 10:54:21 +02:00
|
|
|
const { website_id, session_id } = parseSession(session);
|
|
|
|
const { type, payload } = req.body;
|
|
|
|
let ok = 1;
|
2020-07-17 10:03:38 +02:00
|
|
|
|
2020-07-20 10:54:21 +02:00
|
|
|
if (type === 'pageview') {
|
|
|
|
const { url, referrer } = payload;
|
|
|
|
await savePageView(website_id, session_id, url, referrer).catch(e => {
|
|
|
|
ok = 0;
|
|
|
|
throw e;
|
|
|
|
});
|
|
|
|
} else if (type === 'event') {
|
|
|
|
const { url, event_type, event_value } = payload;
|
|
|
|
await saveEvent(website_id, session_id, url, event_type, event_value).catch(() => {
|
|
|
|
ok = 0;
|
|
|
|
throw e;
|
|
|
|
});
|
2020-07-17 10:03:38 +02:00
|
|
|
}
|
|
|
|
|
2020-07-20 10:54:21 +02:00
|
|
|
res.status(200).json({ ok, session });
|
2020-07-17 10:03:38 +02:00
|
|
|
};
|