2019-11-19 07:30:42 +03:00
|
|
|
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)
|
2020-01-15 11:57:47 +03:00
|
|
|
this.state = {
|
|
|
|
loading: true,
|
|
|
|
query: parseQuery(props.location.search, props.site)
|
|
|
|
}
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
2020-07-30 11:18:28 +03:00
|
|
|
const include = this.showBounceRate() ? 'bounce_rate' : null
|
2019-11-19 07:30:42 +03:00
|
|
|
|
2020-02-04 16:44:13 +03:00
|
|
|
api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/pages`, this.state.query, {limit: 100, include: include})
|
2019-11-19 07:30:42 +03:00
|
|
|
.then((res) => this.setState({loading: false, pages: res}))
|
|
|
|
}
|
|
|
|
|
2020-07-30 11:18:28 +03:00
|
|
|
showBounceRate() {
|
2020-07-14 16:52:26 +03:00
|
|
|
return this.state.query.period !== 'realtime' && !this.state.query.filters.goal
|
2020-01-15 11:57:47 +03:00
|
|
|
}
|
|
|
|
|
2020-07-30 11:18:28 +03:00
|
|
|
showPageviews() {
|
|
|
|
const {filters} = this.state.query
|
|
|
|
return this.state.query.period !== 'realtime' && !(filters.goal || filters.source || filters.referrer)
|
|
|
|
}
|
|
|
|
|
2020-01-06 16:51:43 +03:00
|
|
|
formatBounceRate(page) {
|
2020-01-29 16:49:46 +03:00
|
|
|
if (typeof(page.bounce_rate) === 'number') {
|
2020-01-06 16:51:43 +03:00
|
|
|
return page.bounce_rate + '%'
|
|
|
|
} else {
|
|
|
|
return '-'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-19 07:30:42 +03:00
|
|
|
renderPage(page) {
|
|
|
|
return (
|
2020-01-06 16:51:43 +03:00
|
|
|
<tr className="text-sm" key={page.name}>
|
|
|
|
<td className="p-2 truncate">{page.name}</td>
|
|
|
|
<td className="p-2 w-32 font-medium" align="right">{numberFormatter(page.count)}</td>
|
2020-07-30 11:18:28 +03:00
|
|
|
{this.showPageviews() && <td className="p-2 w-32 font-medium" align="right">{numberFormatter(page.pageviews)}</td> }
|
|
|
|
{this.showBounceRate() && <td className="p-2 w-32 font-medium" align="right">{this.formatBounceRate(page)}</td> }
|
2020-01-06 16:51:43 +03:00
|
|
|
</tr>
|
2019-11-19 07:30:42 +03:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-07-14 16:52:26 +03:00
|
|
|
label() {
|
2020-07-30 11:18:28 +03:00
|
|
|
return this.state.query.period === 'realtime' ? 'Active visitors' : 'Visitors'
|
|
|
|
}
|
|
|
|
|
|
|
|
title() {
|
|
|
|
const {filters} = this.state.query
|
|
|
|
return (filters.source || filters.referrer) ? 'Entry Pages' : 'Top Pages'
|
2020-07-14 16:52:26 +03:00
|
|
|
}
|
|
|
|
|
2019-11-19 07:30:42 +03:00
|
|
|
renderBody() {
|
2020-03-03 17:18:02 +03:00
|
|
|
if (this.state.loading) {
|
|
|
|
return (
|
|
|
|
<div className="loading mt-32 mx-auto"><div></div></div>
|
|
|
|
)
|
|
|
|
} else if (this.state.pages) {
|
2019-11-19 07:30:42 +03:00
|
|
|
return (
|
|
|
|
<React.Fragment>
|
2020-07-30 11:18:28 +03:00
|
|
|
<h1 className="text-xl font-bold">{this.title()}</h1>
|
2019-11-19 07:30:42 +03:00
|
|
|
|
2020-03-06 12:11:38 +03:00
|
|
|
<div className="my-4 border-b border-gray-300"></div>
|
2019-11-19 07:30:42 +03:00
|
|
|
<main className="modal__content">
|
2020-01-06 16:51:43 +03:00
|
|
|
<table className="w-full table-striped table-fixed">
|
|
|
|
<thead>
|
|
|
|
<tr>
|
2020-03-26 16:43:55 +03:00
|
|
|
<th className="p-2 text-xs tracking-wide font-bold text-gray-500" align="left">Page url</th>
|
2020-07-14 16:52:26 +03:00
|
|
|
<th className="p-2 w-32 text-xs tracking-wide font-bold text-gray-500" align="right">{ this.label() }</th>
|
2020-07-30 11:18:28 +03:00
|
|
|
{this.showPageviews() && <th className="p-2 w-32 text-xs tracking-wide font-bold text-gray-500" align="right">Pageviews</th>}
|
|
|
|
{this.showBounceRate() && <th className="p-2 w-32 text-xs tracking-wide font-bold text-gray-500" align="right">Bounce rate</th>}
|
2020-01-06 16:51:43 +03:00
|
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
|
|
|
{ this.state.pages.map(this.renderPage.bind(this)) }
|
|
|
|
</tbody>
|
|
|
|
</table>
|
2019-11-19 07:30:42 +03:00
|
|
|
</main>
|
|
|
|
</React.Fragment>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
2020-03-03 17:18:02 +03:00
|
|
|
<Modal site={this.props.site}>
|
2019-11-19 07:30:42 +03:00
|
|
|
{ this.renderBody() }
|
|
|
|
</Modal>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default withRouter(PagesModal)
|