import React from "react"; import { Link } from 'react-router-dom' 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.showBounceRate() ? 'bounce_rate' : null const {filters} = this.state.query if (filters.source || filters.referrer) { api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/entry-pages`, this.state.query, {limit: 100, include: include}) .then((res) => this.setState({loading: false, pages: res})) } else { 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})) } } showBounceRate() { return this.state.query.period !== 'realtime' && !this.state.query.filters.goal } showPageviews() { const {filters} = this.state.query return this.state.query.period !== 'realtime' && !(filters.goal || filters.source || filters.referrer) } formatBounceRate(page) { if (typeof(page.bounce_rate) === 'number') { return page.bounce_rate + '%' } else { return '-' } } renderPage(page) { const query = new URLSearchParams(window.location.search) query.set('page', page.name) return ( {page.name} {numberFormatter(page.count)} {this.showPageviews() && {numberFormatter(page.pageviews)} } {this.showBounceRate() && {this.formatBounceRate(page)} } ) } label() { return this.state.query.period === 'realtime' ? 'Current visitors' : 'Visitors' } title() { const {filters} = this.state.query return (filters.source || filters.referrer) ? 'Entry Pages' : 'Top Pages' } renderBody() { if (this.state.loading) { return (
) } else if (this.state.pages) { return (

{this.title()}

{this.showPageviews() && } {this.showBounceRate() && } { this.state.pages.map(this.renderPage.bind(this)) }
Page url { this.label() }PageviewsBounce rate
) } } render() { return ( { this.renderBody() } ) } } export default withRouter(PagesModal)