Skip to content

Instantly share code, notes, and snippets.

@omrilotan
Created April 5, 2020 14:10
Show Gist options
  • Save omrilotan/c1f49eb70e6808febf1aee03e397e2ce to your computer and use it in GitHub Desktop.
Save omrilotan/c1f49eb70e6808febf1aee03e397e2ce to your computer and use it in GitHub Desktop.
/**
* Add a breadcrumb to "interactionTrail" array
* @param {Event} event
*/
function collectBreadcrumb({ type, target }) {
const { tagName, attributes = [] } = target;
const breadcrumb = {
type,
// Turn the event target into a descriptive object
target: Object.assign(
{ tagName },
...[...attributes].map(
({name, value}) => ({[name]: value})
),
)
)
};
// For form submission - collect form information
/form/i.test(tagName) && Object.assign(
breadcrumb,
{
fields: [...target].filter(
// Don't want sensitive information in our logs
({name}) => name && /password/i.test(name)
).map(
// Turn fields into key-value objects
({name, value}) => ({[name]: value})
)
}
);
// I'm using unshift so that the most recent interaction is at the top
(window.interactionTrail = window.interactionTrail || []).unshift(breadcrumb);
}
['click', 'submit'].forEach(
type => window.addEventListener(type, collectBreadcrumb)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment