import React from "react";
import { withRouter } from 'react-router-dom'
import Modal from './modal'
import * as api from '../../api'
import numberFormatter from '../../number-formatter'
import {parseQuery} from '../../query'
class PagesModal extends React.Component {
constructor(props) {
super(props)
this.state = {
loading: true,
query: parseQuery(props.location.search, props.site)
}
}
componentDidMount() {
const include = this.showExtra() ? 'bounce_rate,unique_visitors' : null
api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/pages`, this.state.query, {limit: 100, include: include})
.then((res) => this.setState({loading: false, pages: res}))
}
showExtra() {
return this.state.query.period !== 'realtime' && !this.state.query.filters.goal
}
formatBounceRate(page) {
if (typeof(page.bounce_rate) === 'number') {
return page.bounce_rate + '%'
} else {
return '-'
}
}
renderPage(page) {
return (
{page.name} |
{numberFormatter(page.count)} |
{this.showExtra() && {numberFormatter(page.unique_visitors)} | }
{this.showExtra() && {this.formatBounceRate(page)} | }
)
}
label() {
return this.state.query.period === 'realtime' ? 'Active visitors' : 'Pageviews'
}
renderBody() {
if (this.state.loading) {
return (
)
} else if (this.state.pages) {
return (
Top pages
Page url |
{ this.label() } |
{this.showExtra() && Unique visitors | }
{this.showExtra() && Bounce rate | }
{ this.state.pages.map(this.renderPage.bind(this)) }
)
}
}
render() {
return (
{ this.renderBody() }
)
}
}
export default withRouter(PagesModal)