mirror of
https://github.com/kremalicious/umami.git
synced 2024-11-22 09:57:00 +01:00
Refactored session and collect process.
This commit is contained in:
parent
e58aa9e90f
commit
132bbcbe0d
26
lib/db.js
26
lib/db.js
@ -13,37 +13,39 @@ export async function runQuery(query) {
|
||||
});
|
||||
}
|
||||
|
||||
export async function getWebsite(website_id) {
|
||||
export async function getWebsite(website_uuid) {
|
||||
return runQuery(
|
||||
prisma.website.findOne({
|
||||
where: {
|
||||
website_uuid: website_id,
|
||||
website_uuid,
|
||||
},
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
export async function createSession(website_id, session_id, data) {
|
||||
await runQuery(
|
||||
export async function createSession(website_id, data) {
|
||||
return runQuery(
|
||||
prisma.session.create({
|
||||
data: {
|
||||
session_uuid: session_id,
|
||||
website: {
|
||||
connect: {
|
||||
website_uuid: website_id,
|
||||
website_id,
|
||||
},
|
||||
},
|
||||
...data,
|
||||
},
|
||||
select: {
|
||||
session_id: true,
|
||||
},
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
export async function getSession(session_id) {
|
||||
export async function getSession(session_uuid) {
|
||||
return runQuery(
|
||||
prisma.session.findOne({
|
||||
where: {
|
||||
session_uuid: session_id,
|
||||
session_uuid,
|
||||
},
|
||||
}),
|
||||
);
|
||||
@ -55,12 +57,12 @@ export async function savePageView(website_id, session_id, url, referrer) {
|
||||
data: {
|
||||
website: {
|
||||
connect: {
|
||||
website_uuid: website_id,
|
||||
website_id,
|
||||
},
|
||||
},
|
||||
session: {
|
||||
connect: {
|
||||
session_uuid: session_id,
|
||||
session_id,
|
||||
},
|
||||
},
|
||||
url,
|
||||
@ -76,12 +78,12 @@ export async function saveEvent(website_id, session_id, url, event_type, event_v
|
||||
data: {
|
||||
website: {
|
||||
connect: {
|
||||
website_uuid: website_id,
|
||||
website_id,
|
||||
},
|
||||
},
|
||||
session: {
|
||||
connect: {
|
||||
session_uuid: session_id,
|
||||
session_id,
|
||||
},
|
||||
},
|
||||
url,
|
||||
|
49
lib/session.js
Normal file
49
lib/session.js
Normal 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(':');
|
||||
}
|
||||
}
|
||||
}
|
70
lib/utils.js
70
lib/utils.js
@ -12,8 +12,8 @@ export function md5(s) {
|
||||
return crypto.createHash('md5').update(s).digest('hex');
|
||||
}
|
||||
|
||||
export function hash(s) {
|
||||
return uuid(s, md5(process.env.HASH_SALT));
|
||||
export function hash(...args) {
|
||||
return uuid(args.join(''), md5(process.env.HASH_SALT));
|
||||
}
|
||||
|
||||
export function validHash(s) {
|
||||
@ -60,61 +60,19 @@ export async function getCountry(req, ip) {
|
||||
return result.country.iso_code;
|
||||
}
|
||||
|
||||
export async function parseSessionRequest(req) {
|
||||
if (req.method === 'POST') {
|
||||
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 {
|
||||
website_id,
|
||||
session_id,
|
||||
hostname,
|
||||
browser,
|
||||
os,
|
||||
screen,
|
||||
language,
|
||||
country,
|
||||
};
|
||||
}
|
||||
|
||||
return {};
|
||||
export function parseSession(session) {
|
||||
const [website_id, website_uuid, session_id, session_uuid, sig] = (session || '').split(':');
|
||||
return {
|
||||
website_id: parseInt(website_id),
|
||||
website_uuid,
|
||||
session_id: parseInt(session_id),
|
||||
session_uuid,
|
||||
sig,
|
||||
};
|
||||
}
|
||||
|
||||
export function parseCollectRequest(req) {
|
||||
if (req.method === 'POST') {
|
||||
const { type, payload } = req.body;
|
||||
export function isValidSession(session) {
|
||||
const { website_id, website_uuid, session_id, session_uuid, sig } = parseSession(session);
|
||||
|
||||
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 };
|
||||
return hash(website_id, website_uuid, session_id, session_uuid) === sig;
|
||||
}
|
||||
|
@ -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);
|
||||
};
|
File diff suppressed because one or more lines are too long
@ -1,134 +1,111 @@
|
||||
import 'promise-polyfill/src/polyfill';
|
||||
import 'unfetch/polyfill';
|
||||
|
||||
const {
|
||||
screen: { width, height },
|
||||
navigator: { language },
|
||||
location: { hostname, pathname, search },
|
||||
localStorage: store,
|
||||
document,
|
||||
history,
|
||||
} = window;
|
||||
((window, sessionKey) => {
|
||||
const {
|
||||
screen: { width, height },
|
||||
navigator: { language },
|
||||
location: { hostname, pathname, search },
|
||||
localStorage: store,
|
||||
document,
|
||||
history,
|
||||
} = window;
|
||||
|
||||
/* Load script */
|
||||
const script = document.querySelector('script[data-website-id]');
|
||||
const website = script && script.getAttribute('data-website-id');
|
||||
const hostUrl = new URL(script.src).origin;
|
||||
const screen = `${width}x${height}`;
|
||||
const listeners = [];
|
||||
|
||||
const script = document.querySelector('script[data-website-id]');
|
||||
let currentUrl = `${pathname}${search}`;
|
||||
let currentRef = document.referrer;
|
||||
|
||||
if (script) {
|
||||
const website_id = script.getAttribute('data-website-id');
|
||||
/* Helper methods */
|
||||
|
||||
if (website_id) {
|
||||
const sessionKey = 'umami.session';
|
||||
const hostUrl = new URL(script.src).origin;
|
||||
const screen = `${width}x${height}`;
|
||||
let currentUrl = `${pathname}${search}`;
|
||||
let currenrRef = document.referrer;
|
||||
const listeners = [];
|
||||
const post = (url, params) =>
|
||||
fetch(url, {
|
||||
method: 'post',
|
||||
cache: 'no-cache',
|
||||
headers: {
|
||||
Accept: 'application/json',
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify(params),
|
||||
}).then(res => res.json());
|
||||
|
||||
/* Helper methods */
|
||||
|
||||
const post = (url, params) =>
|
||||
fetch(url, {
|
||||
method: 'post',
|
||||
cache: 'no-cache',
|
||||
headers: {
|
||||
Accept: 'application/json',
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify(params),
|
||||
}).then(res => res.json());
|
||||
|
||||
const createSession = data =>
|
||||
post(`${hostUrl}/api/session`, data).then(({ success, ...session }) => {
|
||||
if (success) {
|
||||
store.setItem(sessionKey, JSON.stringify(session));
|
||||
return success;
|
||||
}
|
||||
});
|
||||
|
||||
const getSession = () => JSON.parse(store.getItem(sessionKey));
|
||||
|
||||
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)),
|
||||
);
|
||||
}
|
||||
const collect = (type, params) => {
|
||||
const payload = {
|
||||
session: store.getItem(sessionKey),
|
||||
url: currentUrl,
|
||||
referrer: currentRef,
|
||||
website,
|
||||
hostname,
|
||||
screen,
|
||||
language,
|
||||
};
|
||||
|
||||
/* Handle push state */
|
||||
|
||||
const handlePush = (state, title, url) => {
|
||||
removeEvents();
|
||||
currenrRef = currentUrl;
|
||||
currentUrl = url;
|
||||
execute(currentUrl, currenrRef);
|
||||
setTimeout(loadEvents, 300);
|
||||
};
|
||||
|
||||
const hook = (type, cb) => {
|
||||
const orig = history[type];
|
||||
return (state, title, url) => {
|
||||
const args = [state, title, url];
|
||||
cb.apply(null, args);
|
||||
return orig.apply(history, args);
|
||||
};
|
||||
};
|
||||
|
||||
history.pushState = hook('pushState', handlePush);
|
||||
history.replaceState = hook('replaceState', handlePush);
|
||||
|
||||
/* Handle events */
|
||||
|
||||
const removeEvents = () => {
|
||||
listeners.forEach(([element, type, listener]) => {
|
||||
element.removeEventListener(type, listener, true);
|
||||
if (params) {
|
||||
Object.keys(params).forEach(key => {
|
||||
payload[key] = params[key];
|
||||
});
|
||||
listeners.length = 0;
|
||||
};
|
||||
}
|
||||
|
||||
const loadEvents = () => {
|
||||
document.querySelectorAll("[class*='umami--']").forEach(element => {
|
||||
element.className.split(' ').forEach(className => {
|
||||
if (/^umami--/.test(className)) {
|
||||
const [, type, value] = className.split('--');
|
||||
if (type && value) {
|
||||
const listener = () => trackEvent(currentUrl, type, value);
|
||||
listeners.push([element, type, listener]);
|
||||
element.addEventListener(type, listener, true);
|
||||
}
|
||||
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) => {
|
||||
removeEvents();
|
||||
currentRef = currentUrl;
|
||||
currentUrl = url;
|
||||
pageView();
|
||||
};
|
||||
|
||||
const hook = (type, cb) => {
|
||||
const orig = history[type];
|
||||
return (state, title, url) => {
|
||||
const args = [state, title, url];
|
||||
cb.apply(null, args);
|
||||
return orig.apply(history, args);
|
||||
};
|
||||
};
|
||||
|
||||
history.pushState = hook('pushState', handlePush);
|
||||
history.replaceState = hook('replaceState', handlePush);
|
||||
|
||||
/* Handle events */
|
||||
|
||||
const removeEvents = () => {
|
||||
listeners.forEach(([element, type, listener]) => {
|
||||
element.removeEventListener(type, listener, true);
|
||||
});
|
||||
listeners.length = 0;
|
||||
};
|
||||
|
||||
const loadEvents = () => {
|
||||
document.querySelectorAll("[class*='umami--']").forEach(element => {
|
||||
element.className.split(' ').forEach(className => {
|
||||
if (/^umami--/.test(className)) {
|
||||
const [, type, value] = className.split('--');
|
||||
if (type && value) {
|
||||
const listener = () => pageEvent(type, value);
|
||||
listeners.push([element, type, listener]);
|
||||
element.addEventListener(type, listener, true);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
});
|
||||
};
|
||||
|
||||
/* Start */
|
||||
/* Start */
|
||||
|
||||
execute(currentUrl, currenrRef);
|
||||
loadEvents();
|
||||
}
|
||||
}
|
||||
pageView();
|
||||
})(window, 'umami.session');
|
||||
|
Loading…
Reference in New Issue
Block a user