Skip to content
This repository was archived by the owner on May 6, 2024. It is now read-only.

Commit 9514e30

Browse files
authored
feat(usepageviewtrigger): add support for custom parameters in page view event (#209)
1 parent 4105329 commit 9514e30

File tree

3 files changed

+46
-14
lines changed

3 files changed

+46
-14
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,9 @@ The `pageView` event will be dispatched with:
359359
interface Event {
360360
app: string;
361361
view: string;
362+
customParameters?: {
363+
[key: string]: any
364+
};
362365
event: 'page-view'; // Added internally by the hook
363366
timestamp: number; // Added internally by the library when the dispatch function is called
364367
}

src/hooks/usePageViewTrigger/usePageViewTrigger.spec.tsx

+33-11
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,28 @@
1414
*/
1515

1616
import * as React from 'react';
17-
import { render, fireEvent, screen } from '@testing-library/react';
17+
import { render } from '@testing-library/react';
1818

1919
import { TrackingRoot } from '../../components/TrackingRoot';
2020
import { Events } from '../../types';
2121

2222
import { usePageViewTrigger } from './usePageViewTrigger';
2323

24-
const DispatchButton = () => {
25-
const dispatch = usePageViewTrigger();
24+
const DispatchPage = ({ mockDispatchData = {} }) => {
25+
const dispatchPageView = usePageViewTrigger();
2626

27-
return (
28-
<button data-testid="dispatch-btn" onClick={() => dispatch()}>
29-
Dispatch button
30-
</button>
31-
);
27+
React.useEffect(() => {
28+
dispatchPageView(mockDispatchData);
29+
// eslint-disable-next-line react-hooks/exhaustive-deps
30+
}, []);
31+
32+
return <div>Dispatch page</div>;
3233
};
3334

3435
describe('usePageViewTrigger', () => {
3536
it('should provide a dispatch function that contains the pageView event', () => {
3637
const dispatch = jest.fn();
3738
const app = 'test-app-hook';
38-
const btn = 'dispatch-btn';
3939

4040
const expected = {
4141
app,
@@ -46,11 +46,33 @@ describe('usePageViewTrigger', () => {
4646

4747
render(
4848
<TrackingRoot name={app} onDispatch={dispatch}>
49-
<DispatchButton />
49+
<DispatchPage />
5050
</TrackingRoot>,
5151
);
5252

53-
fireEvent.click(screen.getByTestId(btn));
53+
expect(dispatch).toHaveBeenCalledWith(expected);
54+
});
55+
56+
it('should provide a dispatch function that contains the pageView event with optional custom parameters', () => {
57+
const dispatch = jest.fn();
58+
const app = 'test-app-hook';
59+
const customParameters = {
60+
isConsentUpdate: true,
61+
};
62+
63+
const expected = {
64+
app,
65+
event: Events.pageView,
66+
elementTree: [],
67+
timestamp: expect.any(Number),
68+
customParameters,
69+
};
70+
71+
render(
72+
<TrackingRoot name={app} onDispatch={dispatch}>
73+
<DispatchPage mockDispatchData={{ customParameters }} />
74+
</TrackingRoot>,
75+
);
5476

5577
expect(dispatch).toHaveBeenCalledWith(expected);
5678
});

src/hooks/usePageViewTrigger/usePageViewTrigger.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,19 @@
1616
import * as React from 'react';
1717

1818
import { useBaseTrigger } from '../useBaseTrigger';
19-
import { Events } from '../../types';
19+
import { Events, Dispatch, DispatchFn } from '../../types';
2020

21-
export const usePageViewTrigger = (): (() => void) => {
21+
export const usePageViewTrigger = (): DispatchFn => {
2222
const dispatch = useBaseTrigger(Events.pageView);
2323

24-
const handleTrigger = React.useCallback(() => dispatch({}), [dispatch]);
24+
const handleTrigger = React.useCallback(
25+
(dispatchParams?: Dispatch) => {
26+
const { customParameters } = dispatchParams || {};
27+
28+
return dispatch({ customParameters });
29+
},
30+
[dispatch],
31+
);
2532

2633
return handleTrigger;
2734
};

0 commit comments

Comments
 (0)