From 9cb08956c063efd30c12150d377a94b09ed9dc51 Mon Sep 17 00:00:00 2001 From: Mike Cao Date: Sun, 19 Jul 2020 02:23:15 -0700 Subject: [PATCH] Updated event handling. --- lib/db.js | 9 +++++---- lib/utils.js | 8 ++++---- pages/api/collect.js | 4 ++-- pages/index.js | 2 +- public/umami.js | 2 +- scripts/umami/index.js | 33 +++++---------------------------- sql/schema.postgresql.sql | 2 +- 7 files changed, 19 insertions(+), 41 deletions(-) diff --git a/lib/db.js b/lib/db.js index 8725f88c..57518b18 100644 --- a/lib/db.js +++ b/lib/db.js @@ -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, }, }), ); diff --git a/lib/utils.js b/lib/utils.js index c2730f70..52f76160 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -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, }; } } diff --git a/pages/api/collect.js b/pages/api/collect.js index 724191f8..b1573673 100644 --- a/pages/api/collect.js +++ b/pages/api/collect.js @@ -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; }); } diff --git a/pages/index.js b/pages/index.js index 0bd3d0ac..e8681e22 100644 --- a/pages/index.js +++ b/pages/index.js @@ -15,7 +15,7 @@ export default function Home() { 123
- diff --git a/public/umami.js b/public/umami.js index b4ab7334..3d201df3 100644 --- a/public/umami.js +++ b/public/umami.js @@ -1 +1 @@ -!function(){"use strict";function e(e){var t=this.constructor;return this.then((function(n){return t.resolve(e()).then((function(){return n}))}),(function(n){return t.resolve(e()).then((function(){return t.reject(n)}))}))}var t=setTimeout;function n(e){return Boolean(e&&void 0!==e.length)}function r(){}function o(e){if(!(this instanceof o))throw new TypeError("Promises must be constructed via new");if("function"!=typeof e)throw new TypeError("not a function");this._state=0,this._handled=!1,this._value=void 0,this._deferreds=[],f(e,this)}function i(e,t){for(;3===e._state;)e=e._value;0!==e._state?(e._handled=!0,o._immediateFn((function(){var n=1===e._state?t.onFulfilled:t.onRejected;if(null!==n){var r;try{r=n(e._value)}catch(e){return void s(t.promise,e)}u(t.promise,r)}else(1===e._state?u:s)(t.promise,e._value)}))):e._deferreds.push(t)}function u(e,t){try{if(t===e)throw new TypeError("A promise cannot be resolved with itself.");if(t&&("object"==typeof t||"function"==typeof t)){var n=t.then;if(t instanceof o)return e._state=3,e._value=t,void c(e);if("function"==typeof n)return void f((r=n,i=t,function(){r.apply(i,arguments)}),e)}e._state=1,e._value=t,c(e)}catch(t){s(e,t)}var r,i}function s(e,t){e._state=2,e._value=t,c(e)}function c(e){2===e._state&&0===e._deferreds.length&&o._immediateFn((function(){e._handled||o._unhandledRejectionFn(e._value)}));for(var t=0,n=e._deferreds.length;t + 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 */ diff --git a/sql/schema.postgresql.sql b/sql/schema.postgresql.sql index da65c4b3..8076c1fa 100644 --- a/sql/schema.postgresql.sql +++ b/sql/schema.postgresql.sql @@ -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);