Repository URL to install this package:
|
Version:
0.6.0 ▾
|
import getTrackingAttributes from './getTrackingAttributes';
// set multiple element attributes at once
const setAttributes = (element, attrs) => {
Object.keys(attrs).forEach(key => {
element.setAttribute(key, attrs[key]);
});
};
describe('getTrackingAttributes', () => {
it('should return an object with all the tracking attributes mapped from a DOM element', () => {
const element = document.createElement('div');
setAttributes(element, {
href: '/dashboard',
'data-tracking': true,
// ga
'data-ga-page': 'Page',
'data-ga-category': 'Category',
'data-ga-action': 'Action',
'data-ga-label': 'Label',
// amplitude
'data-amplitude-type': 'Type',
'data-amplitude-name': 'Name',
'data-amplitude-properties': JSON.stringify({ 'Some-Props': 'Its a string' }),
});
const trackingObjeckt = getTrackingAttributes(element);
expect(trackingObjeckt).toEqual({
href: '/dashboard',
isSameTabLink: true,
valuesToTrack: {
ga: {
eventAction: 'Action',
eventCategory: 'Category',
eventLabel: 'Label',
eventPage: 'Page',
},
amplitude: {
eventName: 'Name',
eventProperties: { 'Some-Props': 'Its a string' },
eventType: 'Type',
},
},
});
});
it('it should set isSameTabLink to false if the target of a href is _blank', () => {
const element = document.createElement('div');
setAttributes(element, {
href: '/dashboard',
target: '_blank',
'data-tracking': true,
// ga
'data-ga-page': 'Page',
'data-ga-category': 'Category',
'data-ga-action': 'Action',
'data-ga-label': 'Label',
// amplitude
'data-amplitude-type': 'Type',
'data-amplitude-name': 'Name',
'data-amplitude-properties': JSON.stringify({ 'Some-Props': 'Its a string' }),
});
const trackingObjeckt = getTrackingAttributes(element);
expect(trackingObjeckt.isSameTabLink).toEqual(false);
});
});