import React , { FunctionComponent , ReactNode , useEffect } from 'react' ; import ReactGA from 'react-ga' ; import { isExternal } from '../../../utils/domain' ; type Props = { children : ReactNode ; } ; const OutboundLinkTracker = ( { children } ) => { function handleClick ( event ) { if ( event . target instanceof HTMLAnchorElement ) { const url = event . target . href ; if ( isExternal ( url ) ) { ReactGA . outboundLink ( { label : url , } , ( ) => { } ) ; } } } useEffect ( ( ) => { document . addEventListener ( 'click' , handleClick ) ; return ( ) => { document . removeEventListener ( 'click' , handleClick ) ; } ; } ) ; return < > { children } < />; } ; export default OutboundLinkTracker ;