Skip to content

armgit / umami

Public Imported from GitHub
0

Umami is a privacy-first web and product analytics platform. Traffic, campaigns, behavior, conversions, and revenue in one place. No cookies, no surveillance, self-hosted or in the cloud.

Added before-send attribute to tracker.

Mike Cao64505bb4177c
diff --git a/src/tracker/index.js b/src/tracker/index.js
index d155c2fd6..905ba4344 100644
--- a/src/tracker/index.js
+++ b/src/tracker/index.js
@@ -20,6 +20,7 @@
const attr = currentScript.getAttribute.bind(currentScript);
const website = attr(_data + 'website-id');
const hostUrl = attr(_data + 'host-url');
+ const beforeSend = attr(_data + 'before-send');
const tag = attr(_data + 'tag') || undefined;
const autoTrack = attr(_data + 'auto-track') !== _false;
const dnt = attr(_data + 'do-not-track') === _true;
@@ -138,6 +139,15 @@
const send = async (payload, type = 'event') => {
if (trackingDisabled()) return;
+
+ const callback = window[beforeSend];
+
+ if (typeof callback === 'function') {
+ payload = callback(type, payload);
+ }
+
+ if (!payload) return;
+
try {
const res = await fetch(endpoint, {
method: 'POST',
@@ -168,10 +178,10 @@
}
};
- const track = (obj, data) => {
- if (typeof obj === 'string') return send({ ...getPayload(), name: obj, data });
- if (typeof obj === 'object') return send(obj);
- if (typeof obj === 'function') return send(obj(getPayload()));
+ const track = (name, data) => {
+ if (typeof name === 'string') return send({ ...getPayload(), name, data });
+ if (typeof name === 'object') return send({ ...name });
+ if (typeof name === 'function') return send(name(getPayload()));
return send(getPayload());
};