update send

This commit is contained in:
Francis Cao 2023-03-15 16:06:51 -07:00
parent 7596f42545
commit 275e7d3df6

View File

@ -34,9 +34,7 @@ export default async (req: NextApiRequestCollect, res: NextApiResponse) => {
const { type, payload } = getJsonBody(req); const { type, payload } = getJsonBody(req);
const { referrer, eventName, pageTitle } = payload; const { url, referrer, eventName, pageTitle } = payload;
let { url } = payload;
const ignoreIps = process.env.IGNORE_IP; const ignoreIps = process.env.IGNORE_IP;
const ignoreHostnames = process.env.IGNORE_HOSTNAME; const ignoreHostnames = process.env.IGNORE_HOSTNAME;
@ -83,17 +81,41 @@ export default async (req: NextApiRequestCollect, res: NextApiResponse) => {
const session = req.session; const session = req.session;
let urlPath = url.split('?')[0];
const urlQuery = url.split('?')[1];
let referrerPath;
let referrerQuery;
let referrerDomain;
if (referrer.substring(0, 4) === 'http') {
const newRef = new URL(referrer);
referrerPath = newRef.pathname;
referrerDomain = newRef.hostname;
referrerQuery = newRef.search.substring(1);
} else {
referrerPath = referrer.split('?')[0];
referrerQuery = referrer.split('?')[1];
}
if (process.env.REMOVE_TRAILING_SLASH) { if (process.env.REMOVE_TRAILING_SLASH) {
url = url.replace(/\/$/, ''); urlPath = urlPath.replace(/\/$/, '');
} }
if (type === 'pageview') { if (type === 'pageview') {
await savePageView({ ...session, url, referrer, pageTitle }); await savePageView({
...session,
urlPath,
urlQuery,
referrerPath,
referrerQuery,
referrerDomain,
pageTitle,
});
} else if (type === 'event') { } else if (type === 'event') {
await saveEvent({ await saveEvent({
...session, ...session,
url, urlPath,
referrer, urlQuery,
pageTitle, pageTitle,
eventName, eventName,
}); });