Skip to content

Commit 6c0247e

Browse files
committed
middleware: add data-pathname attribute for custom pathname handling
Modified middleware to support the `data-pathname` attribute, ensuring it can be used to customize the pathname sent with tracking events.
1 parent 75309ec commit 6c0247e

2 files changed

Lines changed: 5 additions & 3 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ These parameters can be used with the tracker snippet:
5858
| `data-proxy-url` | No | A complete proxy URL endpoint for the tracking request. Use this when you need to specify a custom tracking endpoint beyond just the domain. Cannot be used together with `data-proxy`. |
5959
| `data-host` | No | Tinybird host URL. Defaults to `https://api.tinybird.co/`, but could be `https://api.us-east.tinybird.co` or a dedicated cluster. The banner already generates the snippet with the proper host. |
6060
| `data-datasource` | No | If you iterate the landing data source, or you just want to ingest the event in a different one, you can specify the landing data source name.
61+
| `data-pathname` | No | Override the `pathname` field sent with every event. Useful for SPAs where `window.location` is polluted with campaign parameters, or when you need to send a virtual/canonical URL (e.g. a content ID instead of the full path).
6162
| `web-vitals` | No | Tracks web vitals using the [web-vitals](https://www.npmjs.com/package/web-vitals) library
6263

6364
### Implementing custom attributes

middleware/src/index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@
1111
let globalAttributes = {}
1212
let stringifyPayload = true
1313

14-
let proxy, proxyUrl, token, host, domain, tenantId
14+
let proxy, proxyUrl, token, host, domain, tenantId, customPathname
1515
if (document.currentScript) {
1616
host = document.currentScript.getAttribute('data-host')
1717
proxy = document.currentScript.getAttribute('data-proxy')
1818
proxyUrl = document.currentScript.getAttribute('data-proxy-url')
1919
token = document.currentScript.getAttribute('data-token')
2020
domain = document.currentScript.getAttribute('data-domain') || ''
2121
tenantId = document.currentScript.getAttribute('data-tenant-id') || ''
22+
customPathname = document.currentScript.getAttribute('data-pathname') || ''
2223

2324
// Check if both proxy and proxyUrl are specified
2425
if (proxy && proxyUrl) {
@@ -322,7 +323,7 @@
322323
locale,
323324
location: country,
324325
referrer: document.referrer,
325-
pathname: window.location.pathname,
326+
pathname: customPathname || window.location.pathname,
326327
href: window.location.href,
327328
})
328329
}, 300)
@@ -339,7 +340,7 @@
339340
rating: metric.rating,
340341
id: metric.id,
341342
navigationType: metric.navigationType,
342-
pathname: window.location.pathname,
343+
pathname: customPathname || window.location.pathname,
343344
href: window.location.href,
344345
'user-agent': window.navigator.userAgent,
345346
locale,

0 commit comments

Comments
 (0)