Refactored session and collect process.
This commit is contained in:
1 parent
e58aa9e90f
commit
132bbcbe0d
7 files changed
+192
-252
No files matched your search
@@ -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,
|
||||
|
||||
@@ -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(':');
|
||||
}
|
||||
}
|
||||
}
|
||||
+14
-56
@@ -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;
|
||||
}
|
||||
+19
-14
@@ -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);
|
||||
};
|
||||
+1
-1
@@ -1 +1 @@
|
||||
!function(){"use strict";function e(e){var n=this.constructor;return this.then((function(t){return n.resolve(e()).then((function(){return t}))}),(function(t){return n.resolve(e()).then((function(){return n.reject(t)}))}))}var n=setTimeout;function t(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,n){for(;3===e._state;)e=e._value;0!==e._state?(e._handled=!0,o._immediateFn((function(){var t=1===e._state?n.onFulfilled:n.onRejected;if(null!==t){var r;try{r=t(e._value)}catch(e){return void s(n.promise,e)}u(n.promise,r)}else(1===e._state?u:s)(n.promise,e._value)}))):e._deferreds.push(n)}function u(e,n){try{if(n===e)throw new TypeError("A promise cannot be resolved with itself.");if(n&&("object"==typeof n||"function"==typeof n)){var t=n.then;if(n instanceof o)return e._state=3,e._value=n,void a(e);if("function"==typeof t)return void f((r=t,i=n,function(){r.apply(i,arguments)}),e)}e._state=1,e._value=n,a(e)}catch(n){s(e,n)}var r,i}function s(e,n){e._state=2,e._value=n,a(e)}function a(e){2===e._state&&0===e._deferreds.length&&o._immediateFn((function(){e._handled||o._unhandledRejectionFn(e._value)}));for(var n=0,t=e._deferreds.length;n<t;n++)i(e,e._deferreds[n]);e._deferreds=null}function c(e,n,t){this.onFulfilled="function"==typeof e?e:null,this.onRejected="function"==typeof n?n:null,this.promise=t}function f(e,n){var t=!1;try{e((function(e){t||(t=!0,u(n,e))}),(function(e){t||(t=!0,s(n,e))}))}catch(e){if(t)return;t=!0,s(n,e)}}o.prototype["catch"]=function(e){return this.then(null,e)},o.prototype.then=function(e,n){var t=new this.constructor(r);return i(this,new c(e,n,t)),t},o.prototype["finally"]=e,o.all=function(e){return new o((function(n,r){if(!t(e))return r(new TypeError("Promise.all accepts an array"));var o=Array.prototype.slice.call(e);if(0===o.length)return n([]);var i=o.length;function u(e,t){try{if(t&&("object"==typeof t||"function"==typeof t)){var s=t.then;if("function"==typeof s)return void s.call(t,(function(n){u(e,n)}),r)}o[e]=t,0==--i&&n(o)}catch(e){r(e)}}for(var s=0;s<o.length;s++)u(s,o[s])}))},o.resolve=function(e){return e&&"object"==typeof e&&e.constructor===o?e:new o((function(n){n(e)}))},o.reject=function(e){return new o((function(n,t){t(e)}))},o.race=function(e){return new o((function(n,r){if(!t(e))return r(new TypeError("Promise.race accepts an array"));for(var i=0,u=e.length;i<u;i++)o.resolve(e[i]).then(n,r)}))},o._immediateFn="function"==typeof setImmediate&&function(e){setImmediate(e)}||function(e){n(e,0)},o._unhandledRejectionFn=function(e){"undefined"!=typeof console&&console&&console.warn("Possible Unhandled Promise Rejection:",e)};var l=function(){if("undefined"!=typeof self)return self;if("undefined"!=typeof window)return window;if("undefined"!=typeof global)return global;throw new Error("unable to locate global object")}();"Promise"in l?l.Promise.prototype["finally"]||(l.Promise.prototype["finally"]=e):l["Promise"]=o,self.fetch||(self.fetch=function(e,n){return n=n||{},new Promise((function(t,r){var o=new XMLHttpRequest,i=[],u=[],s={},a=function(){return{ok:2==(o.status/100|0),statusText:o.statusText,status:o.status,url:o.responseURL,text:function(){return Promise.resolve(o.responseText)},json:function(){return Promise.resolve(JSON.parse(o.responseText))},blob:function(){return Promise.resolve(new Blob([o.response]))},clone:a,headers:{keys:function(){return i},entries:function(){return u},get:function(e){return s[e.toLowerCase()]},has:function(e){return e.toLowerCase()in s}}}};for(var c in o.open(n.method||"get",e,!0),o.onload=function(){o.getAllResponseHeaders().replace(/^(.*?):[^\S\n]*([\s\S]*?)$/gm,(function(e,n,t){i.push(n=n.toLowerCase()),u.push([n,t]),s[n]=s[n]?s[n]+","+t:t})),t(a())},o.onerror=r,o.withCredentials="include"==n.credentials,n.headers)o.setRequestHeader(c,n.headers[c]);o.send(n.body||null)}))});var p=window.screen,d=p.width,h=p.height,v=window.navigator.language,y=window.location,m=y.hostname,w=y.pathname,_=y.search,g=window.localStorage,b=window.document,j=window.history,P=b.querySelector("script[data-website-id]");if(P){var S=P.getAttribute("data-website-id");if(S){var T="umami.session",E=new URL(P.src).origin,R=d+"x"+h,L=""+w+_,x=b.referrer,F=[],O=function(e,n){return fetch(e,{method:"post",cache:"no-cache",headers:{Accept:"application/json","Content-Type":"application/json"},body:JSON.stringify(n)}).then((function(e){return e.json()}))},A=function(e){return O(E+"/api/session",e).then((function(e){var n=e.success,t=function(e,n){var t={};for(var r in e)Object.prototype.hasOwnProperty.call(e,r)&&-1===n.indexOf(r)&&(t[r]=e[r]);return t}(e,["success"]);if(n)return g.setItem(T,JSON.stringify(t)),n}))},I=function(){return JSON.parse(g.getItem(T))},C=function(e,n){return O(E+"/api/collect",{type:"pageview",payloaLine truncated
|
||||
!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 a(e);if("function"==typeof n)return void f((r=n,i=t,function(){r.apply(i,arguments)}),e)}e._state=1,e._value=t,a(e)}catch(t){s(e,t)}var r,i}function s(e,t){e._state=2,e._value=t,a(e)}function a(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<n;t++)i(e,e._deferreds[t]);e._deferreds=null}function c(e,t,n){this.onFulfilled="function"==typeof e?e:null,this.onRejected="function"==typeof t?t:null,this.promise=n}function f(e,t){var n=!1;try{e((function(e){n||(n=!0,u(t,e))}),(function(e){n||(n=!0,s(t,e))}))}catch(e){if(n)return;n=!0,s(t,e)}}o.prototype["catch"]=function(e){return this.then(null,e)},o.prototype.then=function(e,t){var n=new this.constructor(r);return i(this,new c(e,t,n)),n},o.prototype["finally"]=e,o.all=function(e){return new o((function(t,r){if(!n(e))return r(new TypeError("Promise.all accepts an array"));var o=Array.prototype.slice.call(e);if(0===o.length)return t([]);var i=o.length;function u(e,n){try{if(n&&("object"==typeof n||"function"==typeof n)){var s=n.then;if("function"==typeof s)return void s.call(n,(function(t){u(e,t)}),r)}o[e]=n,0==--i&&t(o)}catch(e){r(e)}}for(var s=0;s<o.length;s++)u(s,o[s])}))},o.resolve=function(e){return e&&"object"==typeof e&&e.constructor===o?e:new o((function(t){t(e)}))},o.reject=function(e){return new o((function(t,n){n(e)}))},o.race=function(e){return new o((function(t,r){if(!n(e))return r(new TypeError("Promise.race accepts an array"));for(var i=0,u=e.length;i<u;i++)o.resolve(e[i]).then(t,r)}))},o._immediateFn="function"==typeof setImmediate&&function(e){setImmediate(e)}||function(e){t(e,0)},o._unhandledRejectionFn=function(e){"undefined"!=typeof console&&console&&console.warn("Possible Unhandled Promise Rejection:",e)};var l=function(){if("undefined"!=typeof self)return self;if("undefined"!=typeof window)return window;if("undefined"!=typeof global)return global;throw new Error("unable to locate global object")}();"Promise"in l?l.Promise.prototype["finally"]||(l.Promise.prototype["finally"]=e):l["Promise"]=o,self.fetch||(self.fetch=function(e,t){return t=t||{},new Promise((function(n,r){var o=new XMLHttpRequest,i=[],u=[],s={},a=function(){return{ok:2==(o.status/100|0),statusText:o.statusText,status:o.status,url:o.responseURL,text:function(){return Promise.resolve(o.responseText)},json:function(){return Promise.resolve(JSON.parse(o.responseText))},blob:function(){return Promise.resolve(new Blob([o.response]))},clone:a,headers:{keys:function(){return i},entries:function(){return u},get:function(e){return s[e.toLowerCase()]},has:function(e){return e.toLowerCase()in s}}}};for(var c in o.open(t.method||"get",e,!0),o.onload=function(){o.getAllResponseHeaders().replace(/^(.*?):[^\S\n]*([\s\S]*?)$/gm,(function(e,t,n){i.push(t=t.toLowerCase()),u.push([t,n]),s[t]=s[t]?s[t]+","+n:n})),n(a())},o.onerror=r,o.withCredentials="include"==t.credentials,t.headers)o.setRequestHeader(c,t.headers[c]);o.send(t.body||null)}))}),function(e,t){var n=e.screen,r=n.width,o=n.height,i=e.navigator.language,u=e.location,s=u.hostname,a=u.pathname,c=u.search,f=e.localStorage,l=e.document,h=e.history,p=l.querySelector("script[data-website-id]"),d=p&&p.getAttribute("data-website-id"),v=new URL(p.src).origin,y=r+"x"+o,m=[],w=""+a+c,_=l.referrer,g=function(e,n){var r={session:f.getItem(t),url:w,referrer:_,website:d,hostname:s,screen:y,language:i};return n&&Object.keys(n).forEach((function(e){r[e]=n[e]})),function(e,t){return fetch(e,{method:"post",cache:"no-cache",headers:{Accept:"application/json","Content-Type":"application/json"},body:JSON.stringify(t)}).then((function(e){return e.json()}))}(v+"/api/collect",{type:e,payload:r}).then((function(e){var n=e.ok,r=e.session;return n&&r&&f.setItem(t,r)}))},b=function(){return g("pageview").then((function(){return setTimeout(T,300)}))},j=function(e,t,n){E(),_=w,w=n,b()},P=function(e,t){var n=h[e];return functionLine truncated
|
||||
+95
-118
@@ -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');
|
||||
Reference in new issue
Block a user