Skip to content

Commit 64505bb

Browse files
committed
Added before-send attribute to tracker.
1 parent 1a794a8 commit 64505bb

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

src/tracker/index.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
const attr = currentScript.getAttribute.bind(currentScript);
2121
const website = attr(_data + 'website-id');
2222
const hostUrl = attr(_data + 'host-url');
23+
const beforeSend = attr(_data + 'before-send');
2324
const tag = attr(_data + 'tag') || undefined;
2425
const autoTrack = attr(_data + 'auto-track') !== _false;
2526
const dnt = attr(_data + 'do-not-track') === _true;
@@ -138,6 +139,15 @@
138139

139140
const send = async (payload, type = 'event') => {
140141
if (trackingDisabled()) return;
142+
143+
const callback = window[beforeSend];
144+
145+
if (typeof callback === 'function') {
146+
payload = callback(type, payload);
147+
}
148+
149+
if (!payload) return;
150+
141151
try {
142152
const res = await fetch(endpoint, {
143153
method: 'POST',
@@ -168,10 +178,10 @@
168178
}
169179
};
170180

171-
const track = (obj, data) => {
172-
if (typeof obj === 'string') return send({ ...getPayload(), name: obj, data });
173-
if (typeof obj === 'object') return send(obj);
174-
if (typeof obj === 'function') return send(obj(getPayload()));
181+
const track = (name, data) => {
182+
if (typeof name === 'string') return send({ ...getPayload(), name, data });
183+
if (typeof name === 'object') return send({ ...name });
184+
if (typeof name === 'function') return send(name(getPayload()));
175185
return send(getPayload());
176186
};
177187

0 commit comments

Comments
 (0)