Refactored session and collect process.

This commit is contained in:
Mike Cao 2020-07-20 01:54:21 -07:00
parent e58aa9e90f
commit 132bbcbe0d
7 changed files with 192 additions and 252 deletions

View File

@ -13,37 +13,39 @@ export async function runQuery(query) {
}); });
} }
export async function getWebsite(website_id) { export async function getWebsite(website_uuid) {
return runQuery( return runQuery(
prisma.website.findOne({ prisma.website.findOne({
where: { where: {
website_uuid: website_id, website_uuid,
}, },
}), }),
); );
} }
export async function createSession(website_id, session_id, data) { export async function createSession(website_id, data) {
await runQuery( return runQuery(
prisma.session.create({ prisma.session.create({
data: { data: {
session_uuid: session_id,
website: { website: {
connect: { connect: {
website_uuid: website_id, website_id,
}, },
}, },
...data, ...data,
}, },
select: {
session_id: true,
},
}), }),
); );
} }
export async function getSession(session_id) { export async function getSession(session_uuid) {
return runQuery( return runQuery(
prisma.session.findOne({ prisma.session.findOne({
where: { where: {
session_uuid: session_id, session_uuid,
}, },
}), }),
); );
@ -55,12 +57,12 @@ export async function savePageView(website_id, session_id, url, referrer) {
data: { data: {
website: { website: {
connect: { connect: {
website_uuid: website_id, website_id,
}, },
}, },
session: { session: {
connect: { connect: {
session_uuid: session_id, session_id,
}, },
}, },
url, url,
@ -76,12 +78,12 @@ export async function saveEvent(website_id, session_id, url, event_type, event_v
data: { data: {
website: { website: {
connect: { connect: {
website_uuid: website_id, website_id,
}, },
}, },
session: { session: {
connect: { connect: {
session_uuid: session_id, session_id,
}, },
}, },
url, url,

49
lib/session.js Normal file
View File

@ -0,0 +1,49 @@
import { getWebsite, getSession, createSession } from 'lib/db';
import { getCountry, getDevice, getIpAddress, hash, isValidSession } from 'lib/utils';
export default async function checkSession(req) {
const { payload } = req.body;
const { session } = payload;
if (isValidSession(session)) {
return session;
}
const ip = getIpAddress(req);
const { userAgent, browser, os } = getDevice(req);
const country = await getCountry(req, ip);
const { website: website_uuid, hostname, screen, language } = payload;
if (website_uuid) {
const website = await getWebsite(website_uuid);
if (website) {
const { website_id } = website;
const session_uuid = hash(`${website_id}${hostname}${ip}${userAgent}${os}`);
let session = await getSession(session_uuid);
if (!session) {
session = await createSession(website_id, {
session_uuid,
hostname,
browser,
os,
screen,
language,
country,
});
}
const { session_id } = session;
return [
website_id,
website_uuid,
session_id,
session_uuid,
hash(website_id, website_uuid, session_id, session_uuid),
].join(':');
}
}
}

View File

@ -12,8 +12,8 @@ export function md5(s) {
return crypto.createHash('md5').update(s).digest('hex'); return crypto.createHash('md5').update(s).digest('hex');
} }
export function hash(s) { export function hash(...args) {
return uuid(s, md5(process.env.HASH_SALT)); return uuid(args.join(''), md5(process.env.HASH_SALT));
} }
export function validHash(s) { export function validHash(s) {
@ -60,61 +60,19 @@ export async function getCountry(req, ip) {
return result.country.iso_code; return result.country.iso_code;
} }
export async function parseSessionRequest(req) { export function parseSession(session) {
if (req.method === 'POST') { const [website_id, website_uuid, session_id, session_uuid, sig] = (session || '').split(':');
const ip = getIpAddress(req);
const { website_id, hostname, screen, language } = req.body;
const { userAgent, browser, os } = getDevice(req);
const country = await getCountry(req, ip);
const session_id = hash(`${website_id}${hostname}${ip}${userAgent}${os}`);
return { return {
website_id, website_id: parseInt(website_id),
session_id, website_uuid,
hostname, session_id: parseInt(session_id),
browser, session_uuid,
os, sig,
screen,
language,
country,
}; };
} }
return {}; export function isValidSession(session) {
} const { website_id, website_uuid, session_id, session_uuid, sig } = parseSession(session);
export function parseCollectRequest(req) { return hash(website_id, website_uuid, session_id, session_uuid) === sig;
if (req.method === 'POST') {
const { type, payload } = req.body;
if (payload.session) {
const {
url,
referrer,
event_type,
event_value,
session: { website_id, session_id, time, hash: validationHash },
} = payload;
if (
validHash(website_id) &&
validHash(session_id) &&
validHash(validationHash) &&
hash(`${website_id}${session_id}${time}`) === validationHash
) {
return {
success: 1,
type,
website_id,
session_id,
url,
referrer,
event_type,
event_value,
};
}
}
}
return { success: 0 };
} }

View File

@ -1,25 +1,30 @@
import { parseCollectRequest } from 'lib/utils'; import { parseSession } from 'lib/utils';
import { savePageView, saveEvent } from 'lib/db'; import { savePageView, saveEvent } from 'lib/db';
import { allowPost } from 'lib/middleware'; import { allowPost } from 'lib/middleware';
import checkSession from 'lib/session';
export default async (req, res) => { export default async (req, res) => {
await allowPost(req, res); await allowPost(req, res);
const values = parseCollectRequest(req); const session = await checkSession(req);
if (values.success) { const { website_id, session_id } = parseSession(session);
const { type, website_id, session_id, url, referrer, event_type, event_value } = values; const { type, payload } = req.body;
let ok = 1;
if (type === 'pageview') { if (type === 'pageview') {
await savePageView(website_id, session_id, url, referrer).catch(() => { const { url, referrer } = payload;
values.success = 0; await savePageView(website_id, session_id, url, referrer).catch(e => {
ok = 0;
throw e;
}); });
} else if (type === 'event') { } else if (type === 'event') {
const { url, event_type, event_value } = payload;
await saveEvent(website_id, session_id, url, event_type, event_value).catch(() => { await saveEvent(website_id, session_id, url, event_type, event_value).catch(() => {
values.success = 0; ok = 0;
throw e;
}); });
} }
}
res.status(200).json({ success: values.success }); res.status(200).json({ ok, session });
}; };

View File

@ -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);
};

File diff suppressed because one or more lines are too long

View File

@ -1,6 +1,7 @@
import 'promise-polyfill/src/polyfill'; import 'promise-polyfill/src/polyfill';
import 'unfetch/polyfill'; import 'unfetch/polyfill';
((window, sessionKey) => {
const { const {
screen: { width, height }, screen: { width, height },
navigator: { language }, navigator: { language },
@ -10,21 +11,15 @@ const {
history, history,
} = window; } = window;
/* Load script */
const script = document.querySelector('script[data-website-id]'); const script = document.querySelector('script[data-website-id]');
const website = script && script.getAttribute('data-website-id');
if (script) {
const website_id = script.getAttribute('data-website-id');
if (website_id) {
const sessionKey = 'umami.session';
const hostUrl = new URL(script.src).origin; const hostUrl = new URL(script.src).origin;
const screen = `${width}x${height}`; const screen = `${width}x${height}`;
let currentUrl = `${pathname}${search}`;
let currenrRef = document.referrer;
const listeners = []; const listeners = [];
let currentUrl = `${pathname}${search}`;
let currentRef = document.referrer;
/* Helper methods */ /* Helper methods */
const post = (url, params) => const post = (url, params) =>
@ -38,56 +33,40 @@ if (script) {
body: JSON.stringify(params), body: JSON.stringify(params),
}).then(res => res.json()); }).then(res => res.json());
const createSession = data => const collect = (type, params) => {
post(`${hostUrl}/api/session`, data).then(({ success, ...session }) => { const payload = {
if (success) { session: store.getItem(sessionKey),
store.setItem(sessionKey, JSON.stringify(session)); url: currentUrl,
return success; referrer: currentRef,
} website,
}); hostname,
screen,
const getSession = () => JSON.parse(store.getItem(sessionKey)); language,
const getSessionData = url => ({ website_id, hostname, url, screen, language });
const pageView = (url, referrer) =>
post(`${hostUrl}/api/collect`, {
type: 'pageview',
payload: { url, referrer, session: getSession() },
}).then(({ success }) => {
if (!success) {
store.removeItem(sessionKey);
}
return success;
});
const trackEvent = (url, event_type, event_value) =>
post(`${hostUrl}/api/collect`, {
type: 'event',
payload: { url, event_type, event_value, session: getSession() },
});
const execute = (url, referrer) => {
const data = getSessionData(url);
if (!store.getItem(sessionKey)) {
createSession(data).then(success => success && pageView(url, referrer));
} else {
pageView(url, referrer).then(
success =>
!success && createSession(data).then(success => success && pageView(url, referrer)),
);
}
}; };
/* Handle push state */ if (params) {
Object.keys(params).forEach(key => {
payload[key] = params[key];
});
}
return post(`${hostUrl}/api/collect`, {
type,
payload,
}).then(({ ok, session }) => ok && session && store.setItem(sessionKey, session));
};
const pageView = () => collect('pageview').then(() => setTimeout(loadEvents, 300));
const pageEvent = (event_type, event_value) => collect('event', { event_type, event_value });
/* Handle history */
const handlePush = (state, title, url) => { const handlePush = (state, title, url) => {
removeEvents(); removeEvents();
currenrRef = currentUrl; currentRef = currentUrl;
currentUrl = url; currentUrl = url;
execute(currentUrl, currenrRef); pageView();
setTimeout(loadEvents, 300);
}; };
const hook = (type, cb) => { const hook = (type, cb) => {
@ -117,7 +96,7 @@ if (script) {
if (/^umami--/.test(className)) { if (/^umami--/.test(className)) {
const [, type, value] = className.split('--'); const [, type, value] = className.split('--');
if (type && value) { if (type && value) {
const listener = () => trackEvent(currentUrl, type, value); const listener = () => pageEvent(type, value);
listeners.push([element, type, listener]); listeners.push([element, type, listener]);
element.addEventListener(type, listener, true); element.addEventListener(type, listener, true);
} }
@ -128,7 +107,5 @@ if (script) {
/* Start */ /* Start */
execute(currentUrl, currenrRef); pageView();
loadEvents(); })(window, 'umami.session');
}
}