In this demo, analytics events involving <rh-cta>
elements are parsed by a
document-level analytics event listener. Unlike <pfe-cta>
, which implemented
support for analytics internally, <rh-cta>
users must implement their own
analytics code, taking this demo as an example.
function deepClosest(event, selector) {
for (const node of event.composedPath().reverse()) {
if (node.matches?.(selector)) {
return node;
}
}
return event.target.closest(selector);
}
document.addEventListener('click', function(event) {
const cta = deepClosest(event, 'rh-cta');
if (cta) {
const { href, text, title } = cta.cta;
const color = cta.colorPalette;
const type = cta.variant;
console.log('CTA ANALYTICS EVENT', { href, text, title, color, type });
}
});