import * as React from 'react' ; import { RouteComponentProps } from 'react-router' ; import { EVENT , trackEvent } from '../util/MetricUtils' ; import { ofNullable , trimToEmpty } from '../util/OptionalUtils' ; interface Props extends RouteComponentProps { } interface PathMapping { path : string ; regex : RegExp ; } const pathMappingOverrides = [ { path : '/reports/canary' , regex : /\/reports\/canary\/.*/ } , { path : '/reports/standalone_canary_analysis' , regex : /\/reports\/standalone_canary_analysis\/.*/ } , { path : '/dev-tools/canary-executor' , regex : /\/dev-tools\/canary-executor\/results\/.*/ } ] ; param path const mapPathToReduceCardinality = ( path ) : string => { return ofNullable ( pathMappingOverrides . find ( mapping => mapping . regex . test ( path ) ) ) . map ( mapping => mapping . path ) . orElse ( path ) ; } ; const trackPageView = async ( nullablePreTrimmedPath ) : Promise < void > => { trimToEmpty ( nullablePreTrimmedPath ) . ifPresent ( preMappedPath => { const path = mapPathToReduceCardinality ( preMappedPath ) ; trackEvent ( EVENT . PAGE_VIEW , { path } ) ; } ) ; } ; /**
 * Simple React component that get the history object as props so that we can do page view tracking.
 */ export default class PageViewTracker extends React . Component < Props > { constructor ( props ) { super ( props ) ; trackPageView ( this . props . history . location . pathname ) ; this . props . history . listen ( location => { trackPageView ( location . pathname ) ; } ) ; } render ( ) { return < div />; } }