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'
|
|
|
|
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,
|
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() {
|
2020-07-30 11:18:28 +03:00
|
|
|
const include = this.showBounceRate() ? 'bounce_rate' : null
|
2020-12-17 12:35:27 +03:00
|
|
|
const {query, page, pages} = this.state;
|
2019-11-19 07:30:42 +03:00
|
|
|
|
2020-12-17 12:35:27 +03:00
|
|
|
const {filters} = query
|
2020-08-10 16:16:12 +03:00
|
|
|
if (filters.source || filters.referrer) {
|
2020-12-17 12:35:27 +03:00
|
|
|
api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/entry-pages`, query, {limit: 100, page, include})
|
|
|
|
.then((res) => this.setState((state) => ({loading: false, pages: state.pages.concat(res), moreResultsAvailable: res.length === 100})))
|
2020-08-10 16:16:12 +03:00
|
|
|
} else {
|
2020-12-17 12:35:27 +03:00
|
|
|
api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/pages`, query, {limit: 100, page, include})
|
|
|
|
.then((res) => this.setState((state) => ({loading: false, pages: state.pages.concat(res), moreResultsAvailable: res.length === 100})))
|
2020-08-10 16:16:12 +03:00
|
|
|
}
|
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))
|
|
|
|
}
|
|
|
|
|
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) {
|
2020-08-10 16:16:12 +03:00
|
|
|
const query = new URLSearchParams(window.location.search)
|
|
|
|
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">
|
2020-08-10 16:16:12 +03:00
|
|
|
<Link to={{pathname: `/${encodeURIComponent(this.props.site.domain)}`, search: query.toString()}} className="hover:underline">{page.name}</Link>
|
|
|
|
</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> }
|
|
|
|
{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-08-18 14:14:56 +03:00
|
|
|
return this.state.query.period === 'realtime' ? 'Current visitors' : 'Visitors'
|
2020-07-30 11:18:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
title() {
|
|
|
|
const {filters} = this.state.query
|
|
|
|
return (filters.source || filters.referrer) ? 'Entry Pages' : 'Top Pages'
|
2020-07-14 16:52:26 +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>
|
2020-12-16 12:57:28 +03:00
|
|
|
<h1 className="text-xl font-bold dark:text-gray-100">{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-12-16 12:57:28 +03:00
|
|
|
<th className="p-2 text-xs tracking-wide font-bold text-gray-500 dark:text-gray-400" align="left">Page url</th>
|
|
|
|
<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>}
|
|
|
|
{this.showBounceRate() && <th className="p-2 w-32 text-xs tracking-wide font-bold text-gray-500 dark:text-gray-400" 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() }
|
2020-12-17 12:35:27 +03:00
|
|
|
{ this.renderLoading() }
|
2019-11-19 07:30:42 +03:00
|
|
|
</Modal>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default withRouter(PagesModal)
|