2019-11-19 07:30:42 +03:00
|
|
|
import React from "react";
|
2020-08-10 16:16:12 +03:00
|
|
|
import { Link } from 'react-router-dom'
|
2019-11-19 07:30:42 +03:00
|
|
|
import { withRouter } from 'react-router-dom'
|
|
|
|
|
|
|
|
import Modal from './modal'
|
|
|
|
import * as api from '../../api'
|
2021-05-18 15:14:33 +03:00
|
|
|
import numberFormatter, {durationFormatter} from '../../number-formatter'
|
2019-11-19 07:30:42 +03:00
|
|
|
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,
|
2020-12-17 12:35:27 +03:00
|
|
|
query: parseQuery(props.location.search, props.site),
|
|
|
|
pages: [],
|
|
|
|
page: 1,
|
|
|
|
moreResultsAvailable: false
|
2020-01-15 11:57:47 +03:00
|
|
|
}
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
2020-12-17 12:35:27 +03:00
|
|
|
this.loadPages();
|
|
|
|
}
|
|
|
|
|
|
|
|
loadPages() {
|
2021-05-18 15:14:33 +03:00
|
|
|
const detailed = this.showExtra()
|
2020-12-17 12:35:27 +03:00
|
|
|
const {query, page, pages} = this.state;
|
2019-11-19 07:30:42 +03:00
|
|
|
|
2021-05-18 15:14:33 +03:00
|
|
|
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})))
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|
|
|
|
|
2020-12-17 12:35:27 +03:00
|
|
|
loadMore() {
|
|
|
|
this.setState({loading: true, page: this.state.page + 1}, this.loadPages.bind(this))
|
|
|
|
}
|
|
|
|
|
2021-05-18 15:14:33 +03:00
|
|
|
showExtra() {
|
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)
|
|
|
|
}
|
|
|
|
|
2021-09-20 17:17:11 +03:00
|
|
|
showConversionRate() {
|
|
|
|
return !!this.state.query.filters.goal
|
|
|
|
}
|
|
|
|
|
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) {
|
2020-08-10 16:16:12 +03:00
|
|
|
const query = new URLSearchParams(window.location.search)
|
2021-05-18 15:14:33 +03:00
|
|
|
const timeOnPage = page['time_on_page'] ? durationFormatter(page['time_on_page']) : '-';
|
2020-08-10 16:16:12 +03:00
|
|
|
query.set('page', page.name)
|
|
|
|
|
2019-11-19 07:30:42 +03:00
|
|
|
return (
|
2020-12-16 12:57:28 +03:00
|
|
|
<tr className="text-sm dark:text-gray-200" key={page.name}>
|
2020-08-18 15:04:00 +03:00
|
|
|
<td className="p-2">
|
2021-08-26 09:33:13 +03:00
|
|
|
<Link to={{pathname: `/${encodeURIComponent(this.props.site.domain)}`, search: query.toString()}} className="hover:underline block truncate">{page.name}</Link>
|
2020-08-10 16:16:12 +03:00
|
|
|
</td>
|
2020-01-06 16:51:43 +03:00
|
|
|
<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> }
|
2021-05-18 15:14:33 +03:00
|
|
|
{this.showExtra() && <td className="p-2 w-32 font-medium" align="right">{this.formatBounceRate(page)}</td> }
|
|
|
|
{this.showExtra() && <td className="p-2 w-32 font-medium" align="right">{timeOnPage}</td> }
|
2021-09-20 17:17:11 +03:00
|
|
|
{this.showConversionRate() && <td className="p-2 w-32 font-medium" align="right">{page.conversion_rate}%</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-08-18 14:14:56 +03:00
|
|
|
return this.state.query.period === 'realtime' ? 'Current visitors' : 'Visitors'
|
2020-07-30 11:18:28 +03:00
|
|
|
}
|
|
|
|
|
2020-12-17 12:35:27 +03:00
|
|
|
renderLoading() {
|
2020-03-03 17:18:02 +03:00
|
|
|
if (this.state.loading) {
|
2020-12-17 12:35:27 +03:00
|
|
|
return <div className="loading my-16 mx-auto"><div></div></div>
|
|
|
|
} else if (this.state.moreResultsAvailable) {
|
2020-03-03 17:18:02 +03:00
|
|
|
return (
|
2020-12-17 12:35:27 +03:00
|
|
|
<div className="w-full text-center my-4">
|
|
|
|
<button onClick={this.loadMore.bind(this)} type="button" className="button">
|
|
|
|
Load more
|
|
|
|
</button>
|
|
|
|
</div>
|
2020-03-03 17:18:02 +03:00
|
|
|
)
|
2020-12-17 12:35:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
renderBody() {
|
|
|
|
if (this.state.pages) {
|
2019-11-19 07:30:42 +03:00
|
|
|
return (
|
|
|
|
<React.Fragment>
|
2021-05-18 15:14:33 +03:00
|
|
|
<h1 className="text-xl font-bold dark:text-gray-100">Top Pages</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">
|
2021-08-13 11:00:42 +03:00
|
|
|
<table className="w-max overflow-x-auto md:w-full table-striped table-fixed">
|
2020-01-06 16:51:43 +03:00
|
|
|
<thead>
|
|
|
|
<tr>
|
2021-08-13 11:00:42 +03:00
|
|
|
<th className="p-2 w-48 md:w-56 lg:w-1/3 text-xs tracking-wide font-bold text-gray-500 dark:text-gray-400" align="left">Page url</th>
|
2020-12-16 12:57:28 +03:00
|
|
|
<th className="p-2 w-32 text-xs tracking-wide font-bold text-gray-500 dark:text-gray-400" align="right">{ this.label() }</th>
|
|
|
|
{this.showPageviews() && <th className="p-2 w-32 text-xs tracking-wide font-bold text-gray-500 dark:text-gray-400" align="right">Pageviews</th>}
|
2021-05-18 15:14:33 +03:00
|
|
|
{this.showExtra() && <th className="p-2 w-32 text-xs tracking-wide font-bold text-gray-500 dark:text-gray-400" align="right">Bounce rate</th>}
|
|
|
|
{this.showExtra() && <th className="p-2 w-32 text-xs tracking-wide font-bold text-gray-500 dark:text-gray-400" align="right">Time on Page</th>}
|
2021-09-20 17:17:11 +03:00
|
|
|
{this.showConversionRate() && <th className="p-2 w-32 text-xs tracking-wide font-bold text-gray-500 dark:text-gray-400" align="right">CR</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() }
|
2020-12-17 12:35:27 +03:00
|
|
|
{ this.renderLoading() }
|
2019-11-19 07:30:42 +03:00
|
|
|
</Modal>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default withRouter(PagesModal)
|