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, {durationFormatter} from '../../util/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),
pages: [],
page: 1,
moreResultsAvailable: false
}
}
componentDidMount() {
this.loadPages();
}
loadPages() {
const detailed = this.showExtra()
const {query, page} = this.state;
api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/pages`, query, {limit: 100, page, detailed})
.then((res) => this.setState((state) => ({loading: false, pages: state.pages.concat(res), moreResultsAvailable: res.length === 100})))
}
loadMore() {
this.setState({loading: true, page: this.state.page + 1}, this.loadPages.bind(this))
}
showExtra() {
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)
}
showConversionRate() {
return !!this.state.query.filters.goal
}
formatBounceRate(page) {
if (typeof(page.bounce_rate) === 'number') {
return page.bounce_rate + '%'
} else {
return '-'
}
}
renderPage(page) {
const query = new URLSearchParams(window.location.search)
const timeOnPage = page['time_on_page'] ? durationFormatter(page['time_on_page']) : '-';
query.set('page', page.name)
return (
{page.name}
|
{this.showConversionRate() && {page.total_visitors} | }
{numberFormatter(page.visitors)} |
{this.showPageviews() && {numberFormatter(page.pageviews)} | }
{this.showExtra() && {this.formatBounceRate(page)} | }
{this.showExtra() && {timeOnPage} | }
{this.showConversionRate() && {page.conversion_rate}% | }
)
}
label() {
if (this.state.query.period === 'realtime') {
return 'Current visitors'
}
if (this.showConversionRate()) {
return 'Conversions'
}
return 'Visitors'
}
renderLoading() {
if (this.state.loading) {
return
} else if (this.state.moreResultsAvailable) {
return (
)
}
}
renderBody() {
if (this.state.pages) {
return (
Top Pages
Page url |
{this.showConversionRate() && Total visitors | }
{ this.label() } |
{this.showPageviews() && Pageviews | }
{this.showExtra() && Bounce rate | }
{this.showExtra() && Time on Page | }
{this.showConversionRate() && CR | }
{ this.state.pages.map(this.renderPage.bind(this)) }
)
}
}
render() {
return (
{ this.renderBody() }
{ this.renderLoading() }
)
}
}
export default withRouter(PagesModal)