2019-11-19 07:30:42 +03:00
|
|
|
import React from "react";
|
|
|
|
import { Link, withRouter } from 'react-router-dom'
|
|
|
|
|
2020-03-03 12:13:08 +03:00
|
|
|
import FadeIn from '../../fade-in'
|
2019-11-19 07:30:42 +03:00
|
|
|
import Modal from './modal'
|
|
|
|
import * as api from '../../api'
|
|
|
|
import numberFormatter from '../../number-formatter'
|
|
|
|
import {parseQuery} from '../../query'
|
|
|
|
|
|
|
|
class ReferrersModal extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props)
|
2020-01-06 16:51:43 +03:00
|
|
|
this.state = {
|
|
|
|
loading: true,
|
|
|
|
query: parseQuery(props.location.search, props.site)
|
|
|
|
}
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
2020-01-16 16:40:06 +03:00
|
|
|
if (this.state.query.filters.goal) {
|
2020-02-04 16:44:13 +03:00
|
|
|
api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/goal/referrers`, this.state.query, {limit: 100})
|
2020-01-16 16:40:06 +03:00
|
|
|
.then((res) => this.setState({loading: false, referrers: res}))
|
|
|
|
} else {
|
|
|
|
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)}/referrers`, this.state.query, {limit: 100, include: include})
|
2020-01-16 16:40:06 +03:00
|
|
|
.then((res) => this.setState({loading: false, referrers: res}))
|
|
|
|
}
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|
|
|
|
|
2020-01-06 16:51:43 +03:00
|
|
|
showBounceRate() {
|
|
|
|
return !this.state.query.filters.goal
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
renderReferrer(referrer) {
|
|
|
|
return (
|
2020-01-06 16:51:43 +03:00
|
|
|
<tr className="text-sm" key={referrer.name}>
|
|
|
|
<td className="p-2">
|
2020-03-06 12:11:38 +03:00
|
|
|
<img src={`https://icons.duckduckgo.com/ip3/${referrer.url}.ico`} className="h-4 w-4 mr-2 align-middle inline" />
|
2020-02-04 16:44:13 +03:00
|
|
|
<Link className="hover:underline truncate" style={{maxWidth: '80%'}} to={`/${encodeURIComponent(this.props.site.domain)}/referrers/${referrer.name}${window.location.search}`}>{ referrer.name }</Link>
|
2020-01-06 16:51:43 +03:00
|
|
|
</td>
|
|
|
|
<td className="p-2 w-32 font-medium" align="right">{numberFormatter(referrer.count)}</td>
|
|
|
|
{this.showBounceRate() && <td className="p-2 w-32 font-medium" align="right">{this.formatBounceRate(referrer)}</td> }
|
|
|
|
</tr>
|
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.referrers) {
|
2019-11-19 07:30:42 +03:00
|
|
|
return (
|
|
|
|
<React.Fragment>
|
2020-03-06 12:11:38 +03:00
|
|
|
<h1 className="text-xl font-bold">Top Referrers</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-06 12:11:38 +03:00
|
|
|
<th className="p-2 text-xs tracking-wide font-bold text-gray-600" align="left">Referrer</th>
|
|
|
|
<th className="p-2 w-32 text-xs tracking-wide font-bold text-gray-600" align="right">Visitors</th>
|
|
|
|
{this.showBounceRate() && <th className="p-2 w-32 text-xs tracking-wide font-bold text-gray-600" align="right">Bounce rate</th>}
|
2020-01-06 16:51:43 +03:00
|
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
|
|
|
{ this.state.referrers.map(this.renderReferrer.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(ReferrersModal)
|