|
20 | 20 | const attr = currentScript.getAttribute.bind(currentScript); |
21 | 21 | const website = attr(_data + 'website-id'); |
22 | 22 | const hostUrl = attr(_data + 'host-url'); |
| 23 | + const beforeSend = attr(_data + 'before-send'); |
23 | 24 | const tag = attr(_data + 'tag') || undefined; |
24 | 25 | const autoTrack = attr(_data + 'auto-track') !== _false; |
25 | 26 | const dnt = attr(_data + 'do-not-track') === _true; |
|
138 | 139 |
|
139 | 140 | const send = async (payload, type = 'event') => { |
140 | 141 | 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 | + |
141 | 151 | try { |
142 | 152 | const res = await fetch(endpoint, { |
143 | 153 | method: 'POST', |
|
168 | 178 | } |
169 | 179 | }; |
170 | 180 |
|
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())); |
175 | 185 | return send(getPayload()); |
176 | 186 | }; |
177 | 187 |
|
|
0 commit comments