2019-11-19 07:30:42 +03:00
|
|
|
import React from 'react';
|
|
|
|
import { Link } from 'react-router-dom'
|
2020-07-14 16:52:26 +03:00
|
|
|
import FlipMove from 'react-flip-move';
|
2019-11-19 07:30:42 +03:00
|
|
|
|
2020-08-17 15:13:30 +03:00
|
|
|
import FadeIn from '../../fade-in'
|
|
|
|
import Bar from '../bar'
|
|
|
|
import MoreLink from '../more-link'
|
2021-12-03 14:59:32 +03:00
|
|
|
import numberFormatter from '../../util/number-formatter'
|
2020-08-17 15:13:30 +03:00
|
|
|
import * as api from '../../api'
|
2021-12-03 14:59:32 +03:00
|
|
|
import LazyLoader from '../../components/lazy-loader'
|
2019-11-19 07:30:42 +03:00
|
|
|
|
2020-07-30 11:18:28 +03:00
|
|
|
function LinkOption(props) {
|
|
|
|
if (props.disabled) {
|
|
|
|
return <span {...props}>{props.children}</span>
|
|
|
|
} else {
|
|
|
|
props = Object.assign({}, props, {className: props.className + ' hover:underline'})
|
|
|
|
return <Link {...props}>{props.children}</Link>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-19 07:30:42 +03:00
|
|
|
export default class Referrers extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props)
|
|
|
|
this.state = {loading: true}
|
2021-03-25 12:55:15 +03:00
|
|
|
this.onVisible = this.onVisible.bind(this)
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|
|
|
|
|
2021-03-25 12:55:15 +03:00
|
|
|
onVisible() {
|
2019-11-19 07:30:42 +03:00
|
|
|
this.fetchReferrers()
|
2020-07-14 16:52:26 +03:00
|
|
|
if (this.props.timer) this.props.timer.onTick(this.fetchReferrers.bind(this))
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate(prevProps) {
|
|
|
|
if (this.props.query !== prevProps.query) {
|
|
|
|
this.setState({loading: true, referrers: null})
|
|
|
|
this.fetchReferrers()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-05 14:45:20 +03:00
|
|
|
showNoRef() {
|
|
|
|
return this.props.query.period === 'realtime'
|
|
|
|
}
|
|
|
|
|
2021-09-29 14:28:29 +03:00
|
|
|
showConversionRate() {
|
|
|
|
return !!this.props.query.filters.goal
|
|
|
|
}
|
|
|
|
|
2019-11-19 07:30:42 +03:00
|
|
|
fetchReferrers() {
|
2020-07-30 11:18:28 +03:00
|
|
|
if (this.props.query.filters.source) {
|
2020-08-05 14:45:20 +03:00
|
|
|
api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/referrers/${encodeURIComponent(this.props.query.filters.source)}`, this.props.query, {show_noref: this.showNoRef()})
|
2020-07-30 11:18:28 +03:00
|
|
|
.then((res) => res.search_terms || res.referrers)
|
|
|
|
.then((referrers) => this.setState({loading: false, referrers: referrers}))
|
|
|
|
} else if (this.props.query.filters.goal) {
|
2020-02-04 16:44:13 +03:00
|
|
|
api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/goal/referrers`, this.props.query)
|
2020-01-16 16:40:06 +03:00
|
|
|
.then((res) => this.setState({loading: false, referrers: res}))
|
|
|
|
} else {
|
2020-08-05 14:45:20 +03:00
|
|
|
api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/referrers`, this.props.query, {show_noref: this.showNoRef()})
|
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-07-30 11:18:28 +03:00
|
|
|
renderExternalLink(referrer) {
|
2020-08-05 14:45:20 +03:00
|
|
|
if (this.props.query.filters.source && this.props.query.filters.source !== 'Google' && referrer.name !== 'Direct / None') {
|
2020-07-30 11:18:28 +03:00
|
|
|
return (
|
2021-10-11 15:48:19 +03:00
|
|
|
<a target="_blank" href={'//' + referrer.name} rel="noreferrer" className="hidden group-hover:block">
|
2021-03-25 12:55:15 +03:00
|
|
|
<svg className="inline w-4 h-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-07-30 11:18:28 +03:00
|
|
|
</a>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
2019-11-19 07:30:42 +03:00
|
|
|
renderReferrer(referrer) {
|
2021-09-29 14:28:29 +03:00
|
|
|
const maxWidthDeduction = this.showConversionRate() ? "10rem" : "5rem"
|
2020-07-30 11:18:28 +03:00
|
|
|
const query = new URLSearchParams(window.location.search)
|
2020-08-17 15:13:30 +03:00
|
|
|
query.set('referrer', referrer.name)
|
2020-07-30 11:18:28 +03:00
|
|
|
|
2019-11-19 07:30:42 +03:00
|
|
|
return (
|
2020-02-10 16:17:00 +03:00
|
|
|
<div className="flex items-center justify-between my-1 text-sm" key={referrer.name}>
|
2021-07-08 09:42:30 +03:00
|
|
|
<Bar
|
2021-11-04 15:20:39 +03:00
|
|
|
count={referrer.visitors}
|
2021-07-08 09:42:30 +03:00
|
|
|
all={this.state.referrers}
|
|
|
|
bg="bg-blue-50 dark:bg-gray-500 dark:bg-opacity-15"
|
2021-09-29 14:28:29 +03:00
|
|
|
maxWidthDeduction={maxWidthDeduction}
|
2021-07-08 09:42:30 +03:00
|
|
|
>
|
2021-07-29 09:54:52 +03:00
|
|
|
<span className="flex px-2 py-1.5 z-9 relative break-all group">
|
2021-07-08 09:42:30 +03:00
|
|
|
<LinkOption
|
2021-07-29 09:54:52 +03:00
|
|
|
className="block md:truncate dark:text-gray-300"
|
2021-07-08 09:42:30 +03:00
|
|
|
to={{search: query.toString()}}
|
|
|
|
disabled={referrer.name === 'Direct / None'}
|
|
|
|
>
|
|
|
|
<img
|
2021-09-29 14:28:29 +03:00
|
|
|
src={`/favicon/sources/${encodeURIComponent(referrer.name)}`}
|
2021-07-08 09:42:30 +03:00
|
|
|
referrerPolicy="no-referrer"
|
|
|
|
className="inline w-4 h-4 mr-2 -mt-px align-middle"
|
|
|
|
/>
|
2020-08-05 14:45:20 +03:00
|
|
|
{ referrer.name }
|
2020-07-30 11:18:28 +03:00
|
|
|
</LinkOption>
|
|
|
|
{ this.renderExternalLink(referrer) }
|
|
|
|
</span>
|
2021-07-08 09:42:30 +03:00
|
|
|
</Bar>
|
2021-11-04 15:20:39 +03:00
|
|
|
<span className="font-medium dark:text-gray-200">{numberFormatter(referrer.visitors)}</span>
|
2021-09-29 14:28:29 +03:00
|
|
|
{this.showConversionRate() && <span className="font-medium dark:text-gray-200 w-20 text-right">{referrer.conversion_rate}%</span>}
|
2020-02-10 16:17:00 +03:00
|
|
|
</div>
|
2019-11-19 07:30:42 +03:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-07-14 16:52:26 +03:00
|
|
|
label() {
|
2021-09-29 14:28:29 +03:00
|
|
|
if (this.props.query.period === 'realtime') {
|
|
|
|
return 'Current visitors'
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.showConversionRate()) {
|
|
|
|
return 'Conversions'
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'Visitors'
|
2020-07-14 16:52:26 +03:00
|
|
|
}
|
|
|
|
|
2020-03-03 16:15:09 +03:00
|
|
|
renderList() {
|
|
|
|
if (this.state.referrers.length > 0) {
|
2019-11-19 07:30:42 +03:00
|
|
|
return (
|
2021-06-15 10:34:43 +03:00
|
|
|
<div className="flex flex-col flex-grow">
|
|
|
|
<div
|
2021-09-29 14:28:29 +03:00
|
|
|
className="flex items-center justify-between mt-3 mb-2 text-xs font-bold tracking-wide text-gray-500"
|
2021-06-15 10:34:43 +03:00
|
|
|
>
|
2020-08-17 15:13:30 +03:00
|
|
|
<span>Referrer</span>
|
2021-09-29 14:28:29 +03:00
|
|
|
|
|
|
|
<div className="text-right">
|
|
|
|
<span className="inline-block w-20">{this.label()}</span>
|
|
|
|
{this.showConversionRate() && <span className="inline-block w-20">CR</span>}
|
|
|
|
</div>
|
2019-11-19 07:30:42 +03:00
|
|
|
</div>
|
2020-02-10 16:17:00 +03:00
|
|
|
|
2021-06-15 10:34:43 +03:00
|
|
|
<FlipMove className="flex-grow">
|
2020-07-14 16:52:26 +03:00
|
|
|
{this.state.referrers.map(this.renderReferrer.bind(this))}
|
|
|
|
</FlipMove>
|
2021-06-15 10:34:43 +03:00
|
|
|
</div>
|
2020-03-03 16:15:09 +03:00
|
|
|
)
|
|
|
|
}
|
2021-06-15 10:34:43 +03:00
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className="font-medium text-center text-gray-500 mt-44 dark:text-gray-400"
|
|
|
|
>
|
|
|
|
No data yet
|
|
|
|
</div>
|
|
|
|
)
|
2020-03-03 16:15:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
renderContent() {
|
|
|
|
if (this.state.referrers) {
|
|
|
|
return (
|
|
|
|
<React.Fragment>
|
|
|
|
{ this.renderList() }
|
2020-08-17 15:13:30 +03:00
|
|
|
<MoreLink site={this.props.site} list={this.state.referrers} endpoint={`referrers/${this.props.query.filters.source}`} />
|
2020-02-10 16:17:00 +03:00
|
|
|
</React.Fragment>
|
2019-11-19 07:30:42 +03:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2020-02-10 16:17:00 +03:00
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
2021-06-15 10:34:43 +03:00
|
|
|
<div
|
|
|
|
className="relative p-4 bg-white rounded shadow-xl stats-item flex flex-col dark:bg-gray-825 mt-6 w-full"
|
|
|
|
>
|
|
|
|
<LazyLoader onVisible={this.onVisible} className="flex flex-col flex-grow">
|
2021-03-29 11:50:23 +03:00
|
|
|
<h3 className="font-bold dark:text-gray-100">Top Referrers</h3>
|
2021-03-25 12:55:15 +03:00
|
|
|
{ this.state.loading && <div className="mx-auto loading mt-44"><div></div></div> }
|
2021-06-15 10:34:43 +03:00
|
|
|
<FadeIn show={!this.state.loading}>
|
|
|
|
{ this.renderContent() }
|
|
|
|
</FadeIn>
|
2021-03-29 11:50:23 +03:00
|
|
|
</LazyLoader>
|
|
|
|
</div>
|
2020-02-10 16:17:00 +03:00
|
|
|
)
|
|
|
|
}
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|