2019-11-19 07:30:42 +03:00
|
|
|
import React from "react";
|
|
|
|
import { Link, withRouter } from 'react-router-dom'
|
|
|
|
|
|
|
|
import Modal from './modal'
|
|
|
|
import * as api from '../../api'
|
2021-12-03 14:59:32 +03:00
|
|
|
import numberFormatter, {durationFormatter} from '../../util/number-formatter'
|
2019-11-19 07:30:42 +03:00
|
|
|
import {parseQuery, toHuman} from '../../query'
|
|
|
|
|
|
|
|
class ReferrerDrilldownModal extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props)
|
|
|
|
this.state = {
|
|
|
|
loading: true,
|
|
|
|
query: parseQuery(props.location.search, props.site)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
2021-05-18 15:14:33 +03:00
|
|
|
const detailed = this.showExtra()
|
2020-01-15 11:57:47 +03:00
|
|
|
|
2021-05-18 15:14:33 +03:00
|
|
|
api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/referrers/${this.props.match.params.referrer}`, this.state.query, {limit: 100, detailed})
|
2020-10-20 11:24:20 +03:00
|
|
|
.then((res) => this.setState({loading: false, referrers: res.referrers, totalVisitors: res.total_visitors}))
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|
|
|
|
|
2020-07-17 11:25:20 +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
|
|
|
}
|
|
|
|
|
2021-09-29 14:28:29 +03:00
|
|
|
showConversionRate() {
|
|
|
|
return !!this.state.query.filters.goal
|
|
|
|
}
|
|
|
|
|
|
|
|
label() {
|
|
|
|
if (this.state.query.period === 'realtime') {
|
|
|
|
return 'Current visitors'
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.showConversionRate()) {
|
|
|
|
return 'Conversions'
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'Visitors'
|
|
|
|
}
|
|
|
|
|
2020-01-06 16:51:43 +03:00
|
|
|
formatBounceRate(ref) {
|
2020-01-29 16:49:46 +03:00
|
|
|
if (typeof(ref.bounce_rate) === 'number') {
|
2020-01-06 16:51:43 +03:00
|
|
|
return ref.bounce_rate + '%'
|
|
|
|
} else {
|
|
|
|
return '-'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-17 11:25:20 +03:00
|
|
|
formatDuration(referrer) {
|
|
|
|
if (typeof(referrer.visit_duration) === 'number') {
|
|
|
|
return durationFormatter(referrer.visit_duration)
|
|
|
|
} else {
|
|
|
|
return '-'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-30 11:18:28 +03:00
|
|
|
renderExternalLink(name) {
|
2020-08-05 14:45:20 +03:00
|
|
|
if (name !== 'Direct / None') {
|
|
|
|
return (
|
2021-10-11 15:48:19 +03:00
|
|
|
<a target="_blank" href={'//' + name} rel="noreferrer" className="hidden group-hover:block">
|
2020-12-16 12:57:28 +03:00
|
|
|
<svg className="inline h-4 w-4 ml-1 -mt-1 text-gray-600 dark:text-gray-400" fill="currentColor" viewBox="0 0 20 20"><path d="M11 3a1 1 0 100 2h2.586l-6.293 6.293a1 1 0 101.414 1.414L15 6.414V9a1 1 0 102 0V4a1 1 0 00-1-1h-5z"></path><path d="M5 5a2 2 0 00-2 2v8a2 2 0 002 2h8a2 2 0 002-2v-3a1 1 0 10-2 0v3H5V7h3a1 1 0 000-2H5z"></path></svg>
|
2020-08-05 14:45:20 +03:00
|
|
|
</a>
|
|
|
|
)
|
|
|
|
}
|
2020-07-30 11:18:28 +03:00
|
|
|
}
|
|
|
|
|
2020-10-20 11:24:20 +03:00
|
|
|
renderReferrerName(referrer) {
|
2020-07-30 11:18:28 +03:00
|
|
|
const query = new URLSearchParams(window.location.search)
|
2020-10-20 11:24:20 +03:00
|
|
|
query.set('referrer', referrer.name)
|
2020-07-30 11:18:28 +03:00
|
|
|
|
|
|
|
return (
|
2020-10-20 11:24:20 +03:00
|
|
|
<span className="flex group items-center">
|
|
|
|
<img src={`https://icons.duckduckgo.com/ip3/${referrer.url}.ico`} referrerPolicy="no-referrer" className="h-4 w-4 mr-2 inline" />
|
2020-12-16 12:57:28 +03:00
|
|
|
<Link className="block truncate hover:underline dark:text-gray-200" to={{search: query.toString(), pathname: '/' + this.props.site.domain}} title={referrer.name}>
|
2020-10-20 11:24:20 +03:00
|
|
|
{referrer.name}
|
2020-07-30 11:18:28 +03:00
|
|
|
</Link>
|
2021-05-26 10:52:25 +03:00
|
|
|
{ this.renderExternalLink(referrer.name) }
|
2020-07-30 11:18:28 +03:00
|
|
|
</span>
|
|
|
|
)
|
2020-01-16 14:39:47 +03:00
|
|
|
}
|
|
|
|
|
2021-12-02 12:53:29 +03:00
|
|
|
renderReferrer(referrer) {
|
2019-11-19 07:30:42 +03:00
|
|
|
return (
|
2021-12-02 12:53:29 +03:00
|
|
|
<tr className="text-sm dark:text-gray-200" key={referrer.name}>
|
|
|
|
<td className="p-2">
|
|
|
|
{ this.renderReferrerName(referrer) }
|
|
|
|
</td>
|
|
|
|
{this.showConversionRate() && <td className="p-2 w-32 font-medium" align="right">{numberFormatter(referrer.total_visitors)}</td> }
|
|
|
|
<td className="p-2 w-32 font-medium" align="right">{numberFormatter(referrer.visitors)}</td>
|
|
|
|
{this.showExtra() && <td className="p-2 w-32 font-medium" align="right">{this.formatBounceRate(referrer)}</td> }
|
|
|
|
{this.showExtra() && <td className="p-2 w-32 font-medium" align="right">{this.formatDuration(referrer)}</td> }
|
|
|
|
{this.showConversionRate() && <td className="p-2 w-32 font-medium" align="right">{referrer.conversion_rate}%</td> }
|
|
|
|
</tr>
|
2019-11-19 07:30:42 +03:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-11-20 08:48:27 +03:00
|
|
|
renderGoalText() {
|
|
|
|
if (this.state.query.filters.goal) {
|
|
|
|
return (
|
2020-12-16 12:57:28 +03:00
|
|
|
<h1 className="text-xl font-semibold text-gray-500 dark:text-gray-300 leading-none">completed {this.state.query.filters.goal}</h1>
|
2019-11-20 08:48:27 +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.referrers) {
|
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">Referrer drilldown</h1>
|
2019-11-19 07:30:42 +03:00
|
|
|
|
2020-12-16 12:57:28 +03:00
|
|
|
<div className="my-4 border-b border-gray-300 dark:border-gray-500"></div>
|
2019-11-19 07:30:42 +03:00
|
|
|
<main className="modal__content mt-0">
|
2020-12-16 12:57:28 +03:00
|
|
|
<h1 className="text-xl font-semibold mb-0 leading-none dark:text-gray-200">{this.state.totalVisitors} visitors from {decodeURIComponent(this.props.match.params.referrer)}<br /> {toHuman(this.state.query)}</h1>
|
2019-11-20 08:48:27 +03:00
|
|
|
{this.renderGoalText()}
|
2019-11-19 07:30:42 +03:00
|
|
|
|
2021-08-13 11:00:42 +03:00
|
|
|
<table className="w-max overflow-x-auto md:w-full table-striped table-fixed mt-4">
|
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">Referrer</th>
|
2021-09-29 14:28:29 +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">Total visitors</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>
|
2020-12-16 12:57:28 +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">Visit duration</th>}
|
2021-09-29 14:28:29 +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.referrers.map(this.renderReferrer.bind(this)) }
|
|
|
|
</tbody>
|
|
|
|
</table>
|
2019-11-19 07:30:42 +03:00
|
|
|
</main>
|
|
|
|
</React.Fragment>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
2020-03-04 18:24:18 +03:00
|
|
|
<Modal site={this.props.site}>
|
2019-11-19 07:30:42 +03:00
|
|
|
{ this.renderBody() }
|
|
|
|
</Modal>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default withRouter(ReferrerDrilldownModal)
|