Allow setting custom data on autotracked pageviews via data attributes on script element

This commit is contained in:
Ewen Le Bihan 2023-12-17 00:11:39 +01:00
parent 3656234b5a
commit 1fd997c374

View file

@ -39,6 +39,15 @@
} }
}; };
const getPageviewEventData = () => Object.fromEntries(
[...currentScript.attributes]
.filter(attribute => eventRegex.match(attribute.name))
.map(attribute => {
const match = attribute.name.match(eventRegex);
return [match[1], attribute.value]
})
)
const getPayload = () => ({ const getPayload = () => ({
website, website,
hostname, hostname,
@ -207,7 +216,7 @@
} else if (typeof obj === 'function') { } else if (typeof obj === 'function') {
return send(obj(getPayload())); return send(obj(getPayload()));
} }
return send(getPayload()); return send({ ...getPayload(), data: getPageviewEventData() });
}; };
const identify = data => send({ ...getPayload(), data }, 'identify'); const identify = data => send({ ...getPayload(), data }, 'identify');