Repository URL to install this package:
|
Version:
1.0.0-alpha.7 ▾
|
/**
* Helper function to pluck out a dataset JSON key
*
* @param {Object} obj An object with JSON strings as values
* @param {string} keyName The key to pluck from the object
* @return {Object} An object with keyName as key and parsed JSON as value
*/
const pluckJsonKey = (obj, keyName) => (obj[keyName] ? { [keyName]: JSON.parse(obj[keyName]) } : {});
/**
* Helper function to get the tracking attribute values from a DOM element
* and return them mapped to a trackingData object
*
* @param {HTMLElement} el An HTML element to inspect, for example
* @return {Object}
*/
const getTrackingDataObject = el => {
const { dataset } = el;
const href = el.getAttribute('href') || el.getAttribute('formAction');
const defaultServices = { amplitude: true, ga: true };
const trackingDataObject = {
href,
isSameTabLink: Boolean(href && el.getAttribute('target') !== '_blank'),
trackingData: {
services: (dataset.trackingServices && JSON.parse(dataset.trackingServices)) || defaultServices,
...pluckJsonKey(dataset, 'track'),
...pluckJsonKey(dataset, 'page'),
...pluckJsonKey(dataset, 'identify'),
},
};
return trackingDataObject;
};
export { getTrackingDataObject };