diff --git a/src/__tests__/e2e.test.js b/src/__tests__/e2e.test.js index 3595cf5..cba370d 100644 --- a/src/__tests__/e2e.test.js +++ b/src/__tests__/e2e.test.js @@ -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(); + + 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' }; diff --git a/src/useTrackingImpl.js b/src/useTrackingImpl.js index 5e9432c..94f2a0e 100644 --- a/src/useTrackingImpl.js +++ b/src/useTrackingImpl.js @@ -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( @@ -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') {