Updated event handling.

This commit is contained in:
Mike Cao 2020-07-19 02:23:15 -07:00
parent 9f3acd1583
commit 9cb08956c0
7 changed files with 19 additions and 41 deletions

View File

@ -5,6 +5,7 @@ const prisma = new PrismaClient();
export async function runQuery(query) { export async function runQuery(query) {
return query return query
.catch(e => { .catch(e => {
console.error(e);
throw e; throw e;
}) })
.finally(async () => { .finally(async () => {
@ -69,9 +70,9 @@ export async function savePageView(website_id, session_id, url, referrer) {
); );
} }
export async function saveEvent(website_id, session_id, url, eventType, eventValue) { export async function saveEvent(website_id, session_id, url, event_type, event_value) {
return runQuery( return runQuery(
prisma.pageview.create({ prisma.event.create({
data: { data: {
website: { website: {
connect: { connect: {
@ -84,8 +85,8 @@ export async function saveEvent(website_id, session_id, url, eventType, eventVal
}, },
}, },
url, url,
eventType, event_type,
eventValue, event_value,
}, },
}), }),
); );

View File

@ -91,8 +91,8 @@ export function parseCollectRequest(req) {
const { const {
url, url,
referrer, referrer,
eventType, event_type,
eventValue, event_value,
session: { website_id, session_id, time, hash: validationHash }, session: { website_id, session_id, time, hash: validationHash },
} = payload; } = payload;
@ -109,8 +109,8 @@ export function parseCollectRequest(req) {
session_id, session_id,
url, url,
referrer, referrer,
eventType, event_type,
eventValue, event_value,
}; };
} }
} }

View File

@ -8,14 +8,14 @@ export default async (req, res) => {
const values = parseCollectRequest(req); const values = parseCollectRequest(req);
if (values.success) { if (values.success) {
const { type, website_id, session_id, url, referrer, eventType, eventValue } = values; const { type, website_id, session_id, url, referrer, event_type, event_value } = values;
if (type === 'pageview') { if (type === 'pageview') {
await savePageView(website_id, session_id, url, referrer).catch(() => { await savePageView(website_id, session_id, url, referrer).catch(() => {
values.success = 0; values.success = 0;
}); });
} else if (type === 'event') { } else if (type === 'event') {
await saveEvent(website_id, session_id, url, eventType, eventValue).catch(() => { await saveEvent(website_id, session_id, url, event_type, event_value).catch(() => {
values.success = 0; values.success = 0;
}); });
} }

View File

@ -15,7 +15,7 @@ export default function Home() {
<a>123</a> <a>123</a>
</Link> </Link>
<br /> <br />
<button id="primary-button" className="otherClass umami--click" type="button"> <button id="primary-button" className="otherClass umami--click--primary-button" type="button">
Button Button
</button> </button>
</Layout> </Layout>

File diff suppressed because one or more lines are too long

View File

@ -60,29 +60,12 @@ if (script) {
return success; return success;
}); });
const trackEvent = (url, eventType, eventValue) => const trackEvent = (url, event_type, event_value) =>
post(`${hostUrl}/api/collect`, { post(`${hostUrl}/api/collect`, {
type: 'event', type: 'event',
payload: { url, eventType, eventValue, session: getSession() }, payload: { url, event_type, event_value, session: getSession() },
}).then(({ success }) => {
if (!success) {
store.removeItem(sessionKey);
}
return success;
}); });
const elementToString = e => {
return JSON.stringify(
e.getAttributeNames().reduce(
(obj, val) => {
obj[val] = e.getAttribute(val);
return obj;
},
{ tag: e.tagName.toLowerCase() },
),
);
};
const execute = (url, referrer) => { const execute = (url, referrer) => {
const data = getSessionData(url); const data = getSessionData(url);
@ -120,19 +103,13 @@ if (script) {
document.querySelectorAll("[class*='umami--']").forEach(e => { document.querySelectorAll("[class*='umami--']").forEach(e => {
e.className.split(' ').forEach(c => { e.className.split(' ').forEach(c => {
console.log('class', c);
if (/^umami--/.test(c)) { if (/^umami--/.test(c)) {
const [, event] = c.split('--'); const [, event, value] = c.split('--');
console.log('event', event); if (event && value) {
if (event) { e.addEventListener(event, () => trackEvent(currentUrl, event, value), true);
e.addEventListener(event, () => {
trackEvent(currentUrl, event, elementToString(e));
console.log('exec event', event, elementToString(e));
});
} }
} }
}); });
console.log('match', e);
}); });
/* Start */ /* Start */

View File

@ -34,7 +34,7 @@ create table event (
created_at timestamp with time zone default current_timestamp, created_at timestamp with time zone default current_timestamp,
url varchar(500) not null, url varchar(500) not null,
event_type varchar(50) not null, event_type varchar(50) not null,
event_value varchar(255) not null event_value varchar(50) not null
); );
create index on session(created_at); create index on session(created_at);