@ -1,9 +1,11 @@
import React from "react" ;
import numberFormatter , { durationFormatter } from '../../util/number-formatter'
import { METRIC _MAPPING , METRIC _LABELS } from './visitor-graph'
import classNames from "classnames" ;
import { Tooltip } from '../../util/tooltip'
import numberFormatter , { durationFormatter } from '../../util/number-formatter'
import { METRIC _MAPPING } from './visitor-graph'
export default class TopStats extends React . Component {
renderComparison ( name , comparison ) {
renderComparison ( name , comparison ) {
const formattedComparison = numberFormatter ( Math . abs ( comparison ) )
if ( comparison > 0 ) {
@ -27,64 +29,87 @@ export default class TopStats extends React.Component {
}
}
topStatTooltip ( stat ) {
if ( [ 'visit duration' , 'time on page' , 'bounce rate' , 'conversion rate' ] . includes ( stat . name . toLowerCase ( ) ) ) {
return null
topStatNumberLong ( stat ) {
if ( [ 'visit duration' , 'time on page' ] . includes ( stat . name . toLowerCase ( ) ) ) {
return durationFormatter ( stat . value )
} else if ( [ 'bounce rate' , 'conversion rate' ] . includes ( stat . name . toLowerCase ( ) ) ) {
return stat . value + '%'
} else {
let name = stat . name . toLowerCase ( )
name = stat . value === 1 ? name . slice ( 0 , - 1 ) : name
return stat . value . toLocaleString ( ) + ' ' + name
return stat . value . toLocaleString ( )
}
}
topStatTooltip ( stat ) {
let statName = stat . name . toLowerCase ( )
statName = stat . value === 1 ? statName . slice ( 0 , - 1 ) : statName
return (
< div >
< div > { this . topStatNumberLong ( stat ) } { statName } < / d i v >
{ this . canMetricBeGraphed ( stat ) && < div className = "font-normal text-xs" > { this . titleFor ( stat ) } < / d i v > }
< / d i v >
)
}
titleFor ( stat ) {
if ( this . props . metric === METRIC _MAPPING [ stat . name ] ) {
return ` Hide ${ METRIC _LABELS [ METRIC _MAPPING [ stat . name ] ] . toLowerCase ( ) } from graph `
const isClickable = this . canMetricBeGraphed ( stat )
if ( isClickable && this . props . metric === METRIC _MAPPING [ stat . name ] ) {
return "Click to hide"
} else if ( isClickable ) {
return "Click to show"
} else {
return ` Show ${ METRIC _LABELS [ METRIC _MAPPING [ stat . name ] ] . toLowerCase ( ) } on graph `
return null
}
}
canMetricBeGraphed ( stat ) {
const isTotalUniqueVisitors = this . props . query . filters . goal && stat . name === 'Unique visitors'
const isKnownMetric = Object . keys ( METRIC _MAPPING ) . includes ( stat . name )
return isKnownMetric && ! isTotalUniqueVisitors
}
maybeUpdateMetric ( stat ) {
if ( this . canMetricBeGraphed ( stat ) ) {
this . props . updateMetric ( METRIC _MAPPING [ stat . name ] )
}
}
renderStat ( stat ) {
return (
< div className = "flex items-center justify-between my-1 whitespace-nowrap" >
< b className = "mr-4 text-xl md:text-2xl dark:text-gray-100" tooltip = { this . topStatTooltip ( stat ) } > { this . topStatNumberShort ( stat ) } < / b >
< Tooltip info = { this . topStatTooltip ( stat ) } className = "flex items-center justify-between my-1 whitespace-nowrap" >
< b className = "mr-4 text-xl md:text-2xl dark:text-gray-100" > { this . topStatNumberShort ( stat ) } < / b >
{ this . renderComparison ( stat . name , stat . change ) }
< / d i v >
< / To o l t i p >
)
}
render ( ) {
const { updateMetric, metric, topStatData , query } = this . props
const { metric, topStatData , query } = this . props
const stats = topStatData && topStatData . top _stats . map ( ( stat , index ) => {
let border = index > 0 ? 'lg:border-l border-gray-300' : ''
border = index % 2 === 0 ? border + ' border-r lg:border-r-0' : border
const isClickable = Object . keys ( METRIC _MAPPING ) . includes ( stat . name ) && ! ( query . filters . goal && stat . name === 'Unique visitors' )
const isSelected = metric === METRIC _MAPPING [ stat . name ]
const [ statDisplayName , statExtraName ] = stat . name . split ( /(\(.+\))/g )
const className = classNames ( 'px-4 md:px-6 w-1/2 my-4 lg:w-auto group select-none' , {
'cursor-pointer' : this . canMetricBeGraphed ( stat ) ,
'lg:border-l border-gray-300' : index > 0 ,
'border-r lg:border-r-0' : index % 2 === 0
} )
return (
< React . Fragment key = { stat . name } >
{ isClickable ?
(
< div className = { ` px-4 md:px-6 w-1/2 my-4 lg:w-auto group cursor-pointer select-none ${ border } ` } onClick = { ( ) => { updateMetric ( METRIC _MAPPING [ stat . name ] ) } } tabIndex = { 0 } title = { this . titleFor ( stat ) } >
< div
className = { ` text-xs font-bold tracking-wide text-gray-500 uppercase dark:text-gray-400 whitespace-nowrap flex w-content border-b ${ isSelected ? 'text-indigo-700 dark:text-indigo-500 border-indigo-700 dark:border-indigo-500' : 'group-hover:text-indigo-700 dark:group-hover:text-indigo-500 border-transparent' } ` } >
{ statDisplayName }
{ statExtraName && < span className = "hidden sm:inline-block ml-1" > { statExtraName } < / s p a n > }
< / d i v >
{ this . renderStat ( stat ) }
< / d i v >
) : (
< div className = { ` px-4 md:px-6 w-1/2 my-4 lg:w-auto ${ border } ` } >
< div className = 'text-xs font-bold tracking-wide text-gray-500 uppercase dark:text-gray-400 whitespace-nowrap flex' >
{ stat . name }
< / d i v >
{ this . renderStat ( stat ) }
< / d i v >
) }
< / R e a c t . F r a g m e n t >
< Tooltip key = { stat . name } info = { this . topStatTooltip ( stat ) } className = { className } onClick = { ( ) => { this . maybeUpdateMetric ( stat ) } } >
< div
className = { ` text-xs font-bold tracking-wide text-gray-500 uppercase dark:text-gray-400 whitespace-nowrap flex w-content border-b ${ isSelected ? 'text-indigo-700 dark:text-indigo-500 border-indigo-700 dark:border-indigo-500' : 'group-hover:text-indigo-700 dark:group-hover:text-indigo-500 border-transparent' } ` } >
{ statDisplayName }
{ statExtraName && < span className = "hidden sm:inline-block ml-1" > { statExtraName } < / s p a n > }
< / d i v >
< div className = "flex items-center justify-between my-1 whitespace-nowrap" >
< b className = "mr-4 text-xl md:text-2xl dark:text-gray-100" > { this . topStatNumberShort ( stat ) } < / b >
{ this . renderComparison ( stat . name , stat . change ) }
< / d i v >
< / T o o l t i p >
)
} )