import React from "react";
import { Link , withRouter } from 'react-router-dom'
import Modal from './modal'
import * as api from '../../api'
import numberFormatter, { durationFormatter } from '../../number-formatter'
import {parseQuery} from '../../query'
class EntryPagesModal 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 {query, page} = this.state;
api.get(
`/api/stats/${encodeURIComponent(this.props.site.domain)}/entry-pages`,
query,
{limit: 100, page}
)
.then(
(res) => this.setState((state) => ({
loading: false,
pages: state.pages.concat(res),
moreResultsAvailable: res.length === 100
}))
)
}
loadMore() {
const { page } = this.state;
this.setState({loading: true, page: page + 1}, this.loadPages.bind(this))
}
formatBounceRate(page) {
if (typeof(page.bounce_rate) === 'number') {
return `${page.bounce_rate}%`;
}
return '-';
}
showConversionRate() {
return !!this.state.query.filters.goal
}
showExtra() {
return this.state.query.period !== 'realtime' && !this.showConversionRate()
}
label() {
if (this.state.query.period === 'realtime') {
return 'Current visitors'
}
if (this.showConversionRate()) {
return 'Conversions'
}
return 'Visitors'
}
renderPage(page) {
const query = new URLSearchParams(window.location.search)
query.set('entry_page', page.name)
return (
{page.name}
|
{this.showConversionRate() && {numberFormatter(page.total_visitors)} | }
{numberFormatter(page.count)} |
{this.showExtra() && {numberFormatter(page.entries)} | }
{this.showExtra() && {durationFormatter(page.visit_duration)} | }
{this.showConversionRate() && {numberFormatter(page.conversion_rate)}% | }
)
}
renderLoading() {
if (this.state.loading) {
return
} else if (this.state.moreResultsAvailable) {
return (
)
}
}
renderBody() {
if (this.state.pages) {
return (
<>
Entry Pages
Page url
|
{this.showConversionRate() && Total Visitors | }
{this.label()} |
{this.showExtra() && Total Entrances | }
{this.showExtra() && Visit Duration | }
{this.showConversionRate() && CR | }
{ this.state.pages.map(this.renderPage.bind(this)) }
>
)
}
}
render() {
return (
{ this.renderBody() }
{ this.renderLoading() }
)
}
}
export default withRouter(EntryPagesModal)