mirror of
https://github.com/kremalicious/umami.git
synced 2025-02-14 21:10:34 +01:00
commit
c9e966d5a1
@ -43,21 +43,6 @@ import { removeTrailingSlash } from '../lib/url';
|
|||||||
|
|
||||||
/* Collect metrics */
|
/* Collect metrics */
|
||||||
|
|
||||||
const post = (url, data, callback) => {
|
|
||||||
const req = new XMLHttpRequest();
|
|
||||||
req.open('POST', url, true);
|
|
||||||
req.setRequestHeader('Content-Type', 'application/json');
|
|
||||||
if (cache) req.setRequestHeader('x-umami-cache', cache);
|
|
||||||
|
|
||||||
req.onreadystatechange = () => {
|
|
||||||
if (req.readyState === 4) {
|
|
||||||
callback(req.response);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
req.send(JSON.stringify(data));
|
|
||||||
};
|
|
||||||
|
|
||||||
const getPayload = () => ({
|
const getPayload = () => ({
|
||||||
website,
|
website,
|
||||||
hostname,
|
hostname,
|
||||||
@ -73,17 +58,22 @@ import { removeTrailingSlash } from '../lib/url';
|
|||||||
return a;
|
return a;
|
||||||
};
|
};
|
||||||
|
|
||||||
const collect = (type, payload) => {
|
const collect = (type, payload, persist = false) => {
|
||||||
if (trackingDisabled()) return;
|
if (trackingDisabled()) return;
|
||||||
|
const endpoint = `${root}/api/collect`;
|
||||||
post(
|
let headers = { 'Content-Type': 'application/json' };
|
||||||
`${root}/api/collect`,
|
if (cache) headers['x-umami-cache'] = cache;
|
||||||
{
|
let options = {
|
||||||
type,
|
method: 'POST',
|
||||||
payload,
|
body: JSON.stringify({type, payload}),
|
||||||
},
|
headers
|
||||||
res => (cache = res),
|
};
|
||||||
);
|
if (!persist) {
|
||||||
|
fetch(endpoint, options).then(res => res.text()).then(resText => { cache = resText });
|
||||||
|
} else {
|
||||||
|
options['keepalive'] = true;
|
||||||
|
fetch(endpoint, options);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const trackView = (url = currentUrl, referrer = currentRef, uuid = website) => {
|
const trackView = (url = currentUrl, referrer = currentRef, uuid = website) => {
|
||||||
@ -93,7 +83,7 @@ import { removeTrailingSlash } from '../lib/url';
|
|||||||
website: uuid,
|
website: uuid,
|
||||||
url,
|
url,
|
||||||
referrer,
|
referrer,
|
||||||
}),
|
})
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -105,28 +95,21 @@ import { removeTrailingSlash } from '../lib/url';
|
|||||||
url,
|
url,
|
||||||
event_type,
|
event_type,
|
||||||
event_value,
|
event_value,
|
||||||
}),
|
})
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Handle events */
|
/* Handle events */
|
||||||
|
|
||||||
const sendEvent = (value, type) => {
|
const sendEvent = (event_value, event_type) => {
|
||||||
const payload = getPayload();
|
collect(
|
||||||
|
'event',
|
||||||
payload.event_type = type;
|
assign(getPayload(), {
|
||||||
payload.event_value = value;
|
event_type,
|
||||||
|
event_value,
|
||||||
const data = JSON.stringify({
|
}),
|
||||||
type: 'event',
|
true
|
||||||
payload,
|
);
|
||||||
});
|
|
||||||
|
|
||||||
fetch(`${root}/api/collect`, {
|
|
||||||
method: 'POST',
|
|
||||||
body: data,
|
|
||||||
keepalive: true,
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const addEvents = node => {
|
const addEvents = node => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user