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'
|
|
|
|
import numberFormatter from '../../number-formatter'
|
|
|
|
import * as api from '../../api'
|
2021-03-25 12:55:15 +03:00
|
|
|
import LazyLoader from '../../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'
|
|
|
|
}
|
|
|
|
|
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 (
|
2020-10-09 15:23:08 +03:00
|
|
|
<a target="_blank" href={'//' + referrer.name} 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) {
|
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
|
|
|
|
count={referrer.count}
|
|
|
|
all={this.state.referrers}
|
|
|
|
bg="bg-blue-50 dark:bg-gray-500 dark:bg-opacity-15"
|
|
|
|
maxWidthDeduction="4rem"
|
|
|
|
>
|
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
|
|
|
|
src={`https://icons.duckduckgo.com/ip3/${referrer.url}.ico`}
|
|
|
|
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>
|
2020-12-16 12:57:28 +03:00
|
|
|
<span className="font-medium dark:text-gray-200">{numberFormatter(referrer.count)}</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() {
|
2020-08-18 14:14:56 +03:00
|
|
|
return this.props.query.period === 'realtime' ? 'Current visitors' : '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
|
|
|
|
className="flex items-center justify-between mt-3 mb-2 text-xs font-bold tracking-wide text-gray-500 dark:text-gray-400"
|
|
|
|
>
|
2020-08-17 15:13:30 +03:00
|
|
|
<span>Referrer</span>
|
|
|
|
<span>{ this.label() }</span>
|
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
|
|
|
}
|