mirror of
https://github.com/umami-software/umami.git
synced 2025-12-08 05:12:36 +01:00
Refactored session and collect process.
This commit is contained in:
parent
e58aa9e90f
commit
132bbcbe0d
7 changed files with 192 additions and 252 deletions
|
|
@ -1,25 +1,30 @@
|
|||
import { parseCollectRequest } from 'lib/utils';
|
||||
import { parseSession } from 'lib/utils';
|
||||
import { savePageView, saveEvent } from 'lib/db';
|
||||
import { allowPost } from 'lib/middleware';
|
||||
import checkSession from 'lib/session';
|
||||
|
||||
export default async (req, res) => {
|
||||
await allowPost(req, res);
|
||||
|
||||
const values = parseCollectRequest(req);
|
||||
const session = await checkSession(req);
|
||||
|
||||
if (values.success) {
|
||||
const { type, website_id, session_id, url, referrer, event_type, event_value } = values;
|
||||
const { website_id, session_id } = parseSession(session);
|
||||
const { type, payload } = req.body;
|
||||
let ok = 1;
|
||||
|
||||
if (type === 'pageview') {
|
||||
await savePageView(website_id, session_id, url, referrer).catch(() => {
|
||||
values.success = 0;
|
||||
});
|
||||
} else if (type === 'event') {
|
||||
await saveEvent(website_id, session_id, url, event_type, event_value).catch(() => {
|
||||
values.success = 0;
|
||||
});
|
||||
}
|
||||
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;
|
||||
});
|
||||
}
|
||||
|
||||
res.status(200).json({ success: values.success });
|
||||
res.status(200).json({ ok, session });
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,51 +0,0 @@
|
|||
import { getWebsite, getSession, createSession } from 'lib/db';
|
||||
import { hash, parseSessionRequest } from 'lib/utils';
|
||||
import { allowPost } from 'lib/middleware';
|
||||
|
||||
export default async (req, res) => {
|
||||
await allowPost(req, res);
|
||||
|
||||
let result = { success: 0 };
|
||||
|
||||
const {
|
||||
website_id,
|
||||
session_id,
|
||||
hostname,
|
||||
browser,
|
||||
os,
|
||||
screen,
|
||||
language,
|
||||
country,
|
||||
} = await parseSessionRequest(req);
|
||||
|
||||
if (website_id && session_id) {
|
||||
const website = await getWebsite(website_id);
|
||||
|
||||
if (website) {
|
||||
const session = await getSession(session_id);
|
||||
const time = Date.now();
|
||||
|
||||
if (!session) {
|
||||
await createSession(website_id, session_id, {
|
||||
hostname,
|
||||
browser,
|
||||
os,
|
||||
screen,
|
||||
language,
|
||||
country,
|
||||
});
|
||||
}
|
||||
|
||||
result = {
|
||||
...result,
|
||||
success: 1,
|
||||
session_id,
|
||||
website_id,
|
||||
time,
|
||||
hash: hash(`${website_id}${session_id}${time}`),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
res.status(200).json(result);
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue