|
|
|
|
@ -14,100 +14,102 @@ function Maybe({condition, children}) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default class TopStats extends React.Component {
|
|
|
|
|
renderPercentageComparison(name, comparison, forceDarkBg = false) {
|
|
|
|
|
const formattedComparison = numberFormatter(Math.abs(comparison))
|
|
|
|
|
|
|
|
|
|
const defaultClassName = classNames({
|
|
|
|
|
"pl-2 text-xs dark:text-gray-100": !forceDarkBg,
|
|
|
|
|
"pl-2 text-xs text-gray-100": forceDarkBg
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const noChangeClassName = classNames({
|
|
|
|
|
"pl-2 text-xs text-gray-700 dark:text-gray-300": !forceDarkBg,
|
|
|
|
|
"pl-2 text-xs text-gray-300": forceDarkBg
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if (comparison > 0) {
|
|
|
|
|
const color = name === 'Bounce rate' ? 'text-red-400' : 'text-green-500'
|
|
|
|
|
return <span className={defaultClassName}><span className={color + ' font-bold'}>↑</span> {formattedComparison}%</span>
|
|
|
|
|
} else if (comparison < 0) {
|
|
|
|
|
const color = name === 'Bounce rate' ? 'text-green-500' : 'text-red-400'
|
|
|
|
|
return <span className={defaultClassName}><span className={color + ' font-bold'}>↓</span> {formattedComparison}%</span>
|
|
|
|
|
} else if (comparison === 0) {
|
|
|
|
|
return <span className={noChangeClassName}>〰 0%</span>
|
|
|
|
|
} else {
|
|
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
function renderPercentageComparison(name, comparison, forceDarkBg = false) {
|
|
|
|
|
const formattedComparison = numberFormatter(Math.abs(comparison))
|
|
|
|
|
|
|
|
|
|
const defaultClassName = classNames({
|
|
|
|
|
"pl-2 text-xs dark:text-gray-100": !forceDarkBg,
|
|
|
|
|
"pl-2 text-xs text-gray-100": forceDarkBg
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const noChangeClassName = classNames({
|
|
|
|
|
"pl-2 text-xs text-gray-700 dark:text-gray-300": !forceDarkBg,
|
|
|
|
|
"pl-2 text-xs text-gray-300": forceDarkBg
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if (comparison > 0) {
|
|
|
|
|
const color = name === 'Bounce rate' ? 'text-red-400' : 'text-green-500'
|
|
|
|
|
return <span className={defaultClassName}><span className={color + ' font-bold'}>↑</span> {formattedComparison}%</span>
|
|
|
|
|
} else if (comparison < 0) {
|
|
|
|
|
const color = name === 'Bounce rate' ? 'text-green-500' : 'text-red-400'
|
|
|
|
|
return <span className={defaultClassName}><span className={color + ' font-bold'}>↓</span> {formattedComparison}%</span>
|
|
|
|
|
} else if (comparison === 0) {
|
|
|
|
|
return <span className={noChangeClassName}>〰 0%</span>
|
|
|
|
|
} else {
|
|
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
topStatNumberShort(name, value) {
|
|
|
|
|
if (['visit duration', 'time on page'].includes(name.toLowerCase())) {
|
|
|
|
|
return durationFormatter(value)
|
|
|
|
|
} else if (['bounce rate', 'conversion rate'].includes(name.toLowerCase())) {
|
|
|
|
|
return value + '%'
|
|
|
|
|
} else if (['average revenue', 'total revenue'].includes(name.toLowerCase())) {
|
|
|
|
|
return value?.short
|
|
|
|
|
} else {
|
|
|
|
|
return numberFormatter(value)
|
|
|
|
|
}
|
|
|
|
|
function topStatNumberShort(name, value) {
|
|
|
|
|
if (['visit duration', 'time on page'].includes(name.toLowerCase())) {
|
|
|
|
|
return durationFormatter(value)
|
|
|
|
|
} else if (['bounce rate', 'conversion rate'].includes(name.toLowerCase())) {
|
|
|
|
|
return value + '%'
|
|
|
|
|
} else if (['average revenue', 'total revenue'].includes(name.toLowerCase())) {
|
|
|
|
|
return value?.short
|
|
|
|
|
} else {
|
|
|
|
|
return numberFormatter(value)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
topStatNumberLong(name, value) {
|
|
|
|
|
if (['visit duration', 'time on page'].includes(name.toLowerCase())) {
|
|
|
|
|
return durationFormatter(value)
|
|
|
|
|
} else if (['bounce rate', 'conversion rate'].includes(name.toLowerCase())) {
|
|
|
|
|
return value + '%'
|
|
|
|
|
} else if (['average revenue', 'total revenue'].includes(name.toLowerCase())) {
|
|
|
|
|
return value?.long
|
|
|
|
|
} else {
|
|
|
|
|
return (value || 0).toLocaleString()
|
|
|
|
|
}
|
|
|
|
|
function topStatNumberLong(name, value) {
|
|
|
|
|
if (['visit duration', 'time on page'].includes(name.toLowerCase())) {
|
|
|
|
|
return durationFormatter(value)
|
|
|
|
|
} else if (['bounce rate', 'conversion rate'].includes(name.toLowerCase())) {
|
|
|
|
|
return value + '%'
|
|
|
|
|
} else if (['average revenue', 'total revenue'].includes(name.toLowerCase())) {
|
|
|
|
|
return value?.long
|
|
|
|
|
} else {
|
|
|
|
|
return (value || 0).toLocaleString()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
topStatTooltip(stat, query) {
|
|
|
|
|
export default function TopStats(props) {
|
|
|
|
|
const {site, query, data, onMetricUpdate, tooltipBoundary, lastLoadTimestamp} = props
|
|
|
|
|
|
|
|
|
|
function tooltip(stat) {
|
|
|
|
|
let statName = stat.name.toLowerCase()
|
|
|
|
|
statName = stat.value === 1 ? statName.slice(0, -1) : statName
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div>
|
|
|
|
|
{query.comparison && <div className="whitespace-nowrap">
|
|
|
|
|
{this.topStatNumberLong(stat.name, stat.value)} vs. {this.topStatNumberLong(stat.name, stat.comparison_value)} {statName}
|
|
|
|
|
<span className="ml-2">{this.renderPercentageComparison(stat.name, stat.change, true)}</span>
|
|
|
|
|
{topStatNumberLong(stat.name, stat.value)} vs. {topStatNumberLong(stat.name, stat.comparison_value)} {statName}
|
|
|
|
|
<span className="ml-2">{renderPercentageComparison(stat.name, stat.change, true)}</span>
|
|
|
|
|
</div>}
|
|
|
|
|
|
|
|
|
|
{!query.comparison && <div className="whitespace-nowrap">
|
|
|
|
|
{this.topStatNumberLong(stat.name, stat.value)} {statName}
|
|
|
|
|
{topStatNumberLong(stat.name, stat.value)} {statName}
|
|
|
|
|
</div>}
|
|
|
|
|
|
|
|
|
|
{stat.name === 'Current visitors' && <p className="font-normal text-xs">Last updated <SecondsSinceLastLoad lastLoadTimestamp={this.props.lastLoadTimestamp}/>s ago</p>}
|
|
|
|
|
{stat.name === 'Current visitors' && <p className="font-normal text-xs">Last updated <SecondsSinceLastLoad lastLoadTimestamp={lastLoadTimestamp}/>s ago</p>}
|
|
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
canMetricBeGraphed(stat) {
|
|
|
|
|
function canMetricBeGraphed(stat) {
|
|
|
|
|
return !!stat.graph_metric
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
maybeUpdateMetric(stat) {
|
|
|
|
|
if (this.canMetricBeGraphed(stat)) {
|
|
|
|
|
storage.setItem(`metric__${this.props.site.domain}`, stat.graph_metric)
|
|
|
|
|
this.props.onMetricUpdate(stat.graph_metric)
|
|
|
|
|
function maybeUpdateMetric(stat) {
|
|
|
|
|
if (canMetricBeGraphed(stat)) {
|
|
|
|
|
storage.setItem(`metric__${site.domain}`, stat.graph_metric)
|
|
|
|
|
onMetricUpdate(stat.graph_metric)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
blinkingDot() {
|
|
|
|
|
function blinkingDot() {
|
|
|
|
|
return (
|
|
|
|
|
<div key="dot" className="block pulsating-circle" style={{ left: '125px', top: '52px' }}></div>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getStoredMetric() {
|
|
|
|
|
return storage.getItem(`metric__${this.props.site.domain}`)
|
|
|
|
|
function getStoredMetric() {
|
|
|
|
|
return storage.getItem(`metric__${site.domain}`)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
renderStatName(stat) {
|
|
|
|
|
const isSelected = stat.graph_metric === this.getStoredMetric()
|
|
|
|
|
function renderStatName(stat) {
|
|
|
|
|
const isSelected = stat.graph_metric === getStoredMetric()
|
|
|
|
|
|
|
|
|
|
const [statDisplayName, statExtraName] = stat.name.split(/(\(.+\))/g)
|
|
|
|
|
|
|
|
|
|
@ -124,48 +126,45 @@ export default class TopStats extends React.Component {
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
render() {
|
|
|
|
|
const { topStatData, query, site } = this.props
|
|
|
|
|
|
|
|
|
|
const stats = topStatData && topStatData.top_stats.map((stat, index) => {
|
|
|
|
|
|
|
|
|
|
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 (
|
|
|
|
|
<Tooltip key={stat.name} info={this.topStatTooltip(stat, query)} className={className} onClick={() => { this.maybeUpdateMetric(stat) }} boundary={this.props.tooltipBoundary}>
|
|
|
|
|
{this.renderStatName(stat)}
|
|
|
|
|
<div className="my-1 space-y-2">
|
|
|
|
|
<div>
|
|
|
|
|
<span className="flex items-center justify-between whitespace-nowrap">
|
|
|
|
|
<p className="font-bold text-xl dark:text-gray-100" id={stat.graph_metric}>{this.topStatNumberShort(stat.name, stat.value)}</p>
|
|
|
|
|
<Maybe condition={!query.comparison}>
|
|
|
|
|
{ this.renderPercentageComparison(stat.name, stat.change) }
|
|
|
|
|
</Maybe>
|
|
|
|
|
</span>
|
|
|
|
|
<Maybe condition={query.comparison}>
|
|
|
|
|
<p className="text-xs dark:text-gray-100">{ formatDateRange(site, topStatData.from, topStatData.to) }</p>
|
|
|
|
|
</Maybe>
|
|
|
|
|
</div>
|
|
|
|
|
function renderStat(stat, index) {
|
|
|
|
|
const className = classNames('px-4 md:px-6 w-1/2 my-4 lg:w-auto group select-none', {
|
|
|
|
|
'cursor-pointer': canMetricBeGraphed(stat),
|
|
|
|
|
'lg:border-l border-gray-300': index > 0,
|
|
|
|
|
'border-r lg:border-r-0': index % 2 === 0
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Tooltip key={stat.name} info={tooltip(stat, query)} className={className} onClick={() => { maybeUpdateMetric(stat) }} boundary={tooltipBoundary}>
|
|
|
|
|
{renderStatName(stat)}
|
|
|
|
|
<div className="my-1 space-y-2">
|
|
|
|
|
<div>
|
|
|
|
|
<span className="flex items-center justify-between whitespace-nowrap">
|
|
|
|
|
<p className="font-bold text-xl dark:text-gray-100" id={stat.graph_metric}>{topStatNumberShort(stat.name, stat.value)}</p>
|
|
|
|
|
<Maybe condition={!query.comparison}>
|
|
|
|
|
{ renderPercentageComparison(stat.name, stat.change) }
|
|
|
|
|
</Maybe>
|
|
|
|
|
</span>
|
|
|
|
|
<Maybe condition={query.comparison}>
|
|
|
|
|
<div>
|
|
|
|
|
<p className="font-bold text-xl text-gray-500 dark:text-gray-400">{ this.topStatNumberShort(stat.name, stat.comparison_value) }</p>
|
|
|
|
|
<p className="text-xs text-gray-500 dark:text-gray-400">{ formatDateRange(site, topStatData.comparing_from, topStatData.comparing_to) }</p>
|
|
|
|
|
</div>
|
|
|
|
|
<p className="text-xs dark:text-gray-100">{ formatDateRange(site, data.from, data.to) }</p>
|
|
|
|
|
</Maybe>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<Maybe condition={query.comparison}>
|
|
|
|
|
<div>
|
|
|
|
|
<p className="font-bold text-xl text-gray-500 dark:text-gray-400">{ topStatNumberShort(stat.name, stat.comparison_value) }</p>
|
|
|
|
|
<p className="text-xs text-gray-500 dark:text-gray-400">{ formatDateRange(site, data.comparing_from, data.comparing_to) }</p>
|
|
|
|
|
</div>
|
|
|
|
|
</Tooltip>
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
</Maybe>
|
|
|
|
|
</div>
|
|
|
|
|
</Tooltip>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (stats && query && query.period === 'realtime') {
|
|
|
|
|
stats.push(this.blinkingDot())
|
|
|
|
|
}
|
|
|
|
|
const stats = data && data.top_stats.map(renderStat)
|
|
|
|
|
|
|
|
|
|
return stats || null;
|
|
|
|
|
if (stats && query.period === 'realtime') {
|
|
|
|
|
stats.push(blinkingDot())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return stats || null;
|
|
|
|
|
}
|
|
|
|
|
|