Skip to content

Commit 9790da7

Browse files
author
Gijs Nieuwenhuis
committed
Added 'useTrackingProps'-hook
1 parent 1641c34 commit 9790da7

File tree

4 files changed

+110
-0
lines changed

4 files changed

+110
-0
lines changed

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ A library containing javascript utilities that we until now often copy between p
2727
- [Hooks](#hooks)
2828
- [`useStateWithRef`](#usestatewithref)
2929
- [`useScrollToTopOnDependencyChange`](#usescrolltotopondependencychange)
30+
- [`useTrackingProps`](#usetrackingprops)
3031
- [Storage](#storage)
3132
- [`localStorage`](#localstorage)
3233
- [`sessionStorage`](#sessionstorage)
@@ -380,6 +381,32 @@ const location = useLocation(); // react-router-dom
380381
useScrollToTopOnDependencyChange(location.pathname, location.search);
381382
```
382383
384+
#### `useTrackingProps`
385+
386+
Applies uniform setup for tracking events, using attributes on DOM elements. In Google Tag Manager these can be registered and used to, in turn, push events to Google Analytics or other (tracking) platforms.
387+
388+
Usage:
389+
390+
```jsx
391+
import useTrackingProps from '@freshheads/javascript-essentials/build/react/hooks/useTrackingProps';
392+
393+
type Props = {
394+
subscribeToNewsletter: boolean,
395+
};
396+
397+
const PreorderSubmitButton: React.FC<Props> = (subscribeToNewsletter) => {
398+
const trackingProps = useTrackingProps('preorder', 'submit', {
399+
subscribeToNewsletter,
400+
});
401+
402+
return (
403+
<button {...trackingProps} type="submit">
404+
Submit
405+
</button>
406+
);
407+
};
408+
```
409+
383410
## Routing
384411
385412
### `createPathFromRoute`
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`useTrackingProps() When applied on a JSX element ..with context supplied should render the tracking props on it correctly 1`] = `
4+
<DocumentFragment>
5+
<button
6+
data-tracking-event-action="submit"
7+
data-tracking-event-context="{\\"subscribeToNewsletter\\":true}"
8+
data-tracking-event-name="preorder"
9+
/>
10+
</DocumentFragment>
11+
`;
12+
13+
exports[`useTrackingProps() When applied on a JSX element ..without context supplied should render the tracking props on it correctly 1`] = `
14+
<DocumentFragment>
15+
<button
16+
data-tracking-event-action="submit"
17+
data-tracking-event-name="preorder"
18+
/>
19+
</DocumentFragment>
20+
`;
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { render } from '@testing-library/react';
2+
import useTrackingProps from '../useTrackingProps';
3+
4+
import React from 'react';
5+
6+
describe('useTrackingProps()', () => {
7+
describe('When applied on a JSX element', () => {
8+
describe('..with context supplied', () => {
9+
it('should render the tracking props on it correctly', () => {
10+
const trackingProps = useTrackingProps('preorder', 'submit', {
11+
subscribeToNewsletter: true,
12+
});
13+
14+
const { asFragment } = render(<button {...trackingProps} />);
15+
16+
expect(asFragment()).toMatchSnapshot();
17+
});
18+
});
19+
20+
describe('..without context supplied', () => {
21+
it('should render the tracking props on it correctly', () => {
22+
const trackingProps = useTrackingProps('preorder', 'submit');
23+
24+
const { asFragment } = render(<button {...trackingProps} />);
25+
26+
expect(asFragment()).toMatchSnapshot();
27+
});
28+
});
29+
});
30+
});
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { Serializable } from '../../types/utility';
2+
3+
export type TrackingProps = {
4+
'data-tracking-event-name': string;
5+
'data-tracking-event-action': string;
6+
'data-tracking-event-context'?: Serializable;
7+
};
8+
9+
/**
10+
* Applies uniform setup for tracking events, using attributes on DOM elements.
11+
* In Google Tag Manager these can be registered and used to, in turn, push
12+
* events to Google Analytics or other (tracking) platforms.
13+
*
14+
* @param {string} eventName i.e. "preorder"
15+
* @param {string} eventAction i.e. "submit"
16+
* @param {Serializable=} context i.e. "{ subscribeToNewsletter: false }"
17+
*/
18+
export default function useTrackingProps(
19+
eventName: string,
20+
eventAction: string,
21+
context?: Serializable
22+
): TrackingProps {
23+
let props: TrackingProps = {
24+
'data-tracking-event-name': eventName,
25+
'data-tracking-event-action': eventAction,
26+
};
27+
28+
if (context) {
29+
props['data-tracking-event-context'] = JSON.stringify(context);
30+
}
31+
32+
return props;
33+
}

0 commit comments

Comments
 (0)