const TrackableFeature = (props) => {
const [trackedPaths, setTrackedPaths] = useState([]);
return (
<>
<Provider
value="Provider"
onTrack={(trackedProperties, action) => {
setTrackedPaths((currentTrackedPaths) => [
{ path: trackedProperties.path, action },
...currentTrackedPaths,
]);
}}
>
{props.children}
</Provider>
<hr />
{trackedPaths && (
<section>
Tracked Properties & Actions:
<pre>{JSON.stringify(trackedPaths, null, 2)}</pre>
</section>
)}
</>
);
};
const MyCustomModal = (props) => {
const [track] = useAnalytics(props);
const { show, onHide } = props;
useEffect(() => {
if (show) {
track('MyCustomModal', { type: 'MY_CUSTOM_MODAL_SHOW', payload: 'show' });
}
}, [show]);
const handleHide = () => {
track('MyCustomModal', { type: 'MY_CUSTOM_MODAL_HIDE', payload: 'hide' });
onHide();
};
return (
<Modal show={show} onHide={handleHide} title="Reptiles">
<Modal.Body>🦎 🐍</Modal.Body>
</Modal>
);
};
const MyCustomModalContainer = () => {
const [show, setShow] = useState(false);
const handleOnClick = () => {
setShow(true);
};
const handleHide = () => {
setShow(false);
};
return (
<TrackableFeature>
<MyCustomModal show={show} onHide={handleHide} />
<Button onClick={handleOnClick}>Show Modal</Button>
</TrackableFeature>
);
};
render(<MyCustomModalContainer />);