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) {
return query
.catch(e => {
console.error(e);
throw e;
})
.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(
prisma.pageview.create({
prisma.event.create({
data: {
website: {
connect: {
@ -84,8 +85,8 @@ export async function saveEvent(website_id, session_id, url, eventType, eventVal
},
},
url,
eventType,
eventValue,
event_type,
event_value,
},
}),
);

View File

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

View File

@ -8,14 +8,14 @@ export default async (req, res) => {
const values = parseCollectRequest(req);
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') {
await savePageView(website_id, session_id, url, referrer).catch(() => {
values.success = 0;
});
} 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;
});
}

View File

@ -15,7 +15,7 @@ export default function Home() {
<a>123</a>
</Link>
<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>
</Layout>

File diff suppressed because one or more lines are too long

View File

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

View File

@ -34,7 +34,7 @@ create table event (
created_at timestamp with time zone default current_timestamp,
url varchar(500) 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);