Skip to content

Commit

Permalink
Add mergeOptions config option (#187)
Browse files Browse the repository at this point in the history
  • Loading branch information
BenLorantfy authored Oct 17, 2022
1 parent 67cde3c commit 6380e1f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
24 changes: 24 additions & 0 deletions src/__tests__/e2e.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,30 @@ describe('e2e', () => {
expect(dispatchTrackingEvent).toHaveBeenCalledWith({ test: true });
});

it('allows tracking errors', () => {
@track(null, {
mergeOptions: {
isMergeableObject: obj => !(obj instanceof Error),
},
})
class TestPage extends React.Component {
componentDidMount() {
this.props.tracking.trackEvent({ test: new Error('my crazy error') });
}

render() {
return 'hi';
}
}

mount(<TestPage />);

expect(dispatchTrackingEvent).toHaveBeenCalledTimes(1);
expect(dispatchTrackingEvent).toHaveBeenCalledWith({
test: new Error('my crazy error'),
});
});

it('defaults to dispatchTrackingEvent when no dispatch function passed in to options', () => {
const testPageData = { page: 'TestPage' };

Expand Down
18 changes: 15 additions & 3 deletions src/useTrackingImpl.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,23 @@ export default function useTrackingImpl(trackingData, options) {
return () =>
contextGetTrackingData === getOwnTrackingData
? getOwnTrackingData()
: merge(contextGetTrackingData(), getOwnTrackingData());
: merge(
contextGetTrackingData(),
getOwnTrackingData(),
(latestOptions.current || {}).mergeOptions
);
}, [getOwnTrackingData, tracking]);

const getTrackingDispatcher = useCallback(() => {
const contextDispatch = (tracking && tracking.dispatch) || dispatch;
return data => contextDispatch(merge(getOwnTrackingData(), data || {}));
return data =>
contextDispatch(
merge(
getOwnTrackingData(),
data || {},
(latestOptions.current || {}).mergeOptions
)
);
}, [getOwnTrackingData, tracking, dispatch]);

const trackEvent = useCallback(
Expand Down Expand Up @@ -73,7 +84,8 @@ export default function useTrackingImpl(trackingData, options) {
trackEvent(
merge(
contextProcess(getOwnTrackingData()) || {},
dispatchOnMount(getTrackingData()) || {}
dispatchOnMount(getTrackingData()) || {},
(latestOptions.current || {}).mergeOptions
)
);
} else if (typeof contextProcess === 'function') {
Expand Down

0 comments on commit 6380e1f

Please sign in to comment.