import React from 'react'; import { Link } from 'react-router-dom' import FlipMove from 'react-flip-move'; import FadeIn from '../fade-in' import Bar from './bar' import MoreLink from './more-link' import numberFormatter from '../number-formatter' import * as api from '../api' function LinkOption(props) { if (props.disabled) { return {props.children} } else { props = Object.assign({}, props, {className: props.className + ' hover:underline'}) return {props.children} } } export default class Referrers extends React.Component { constructor(props) { super(props) this.state = {loading: true} } componentDidMount() { this.fetchReferrers() if (this.props.timer) this.props.timer.onTick(this.fetchReferrers.bind(this)) } componentDidUpdate(prevProps) { if (this.props.query !== prevProps.query) { this.setState({loading: true, referrers: null}) this.fetchReferrers() } } showNoRef() { return this.props.query.period === 'realtime' } fetchReferrers() { if (this.props.query.filters.source) { api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/referrers/${encodeURIComponent(this.props.query.filters.source)}`, this.props.query, {show_noref: this.showNoRef()}) .then((res) => res.search_terms || res.referrers) .then((referrers) => this.setState({loading: false, referrers: referrers})) } else if (this.props.query.filters.goal) { api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/goal/referrers`, this.props.query) .then((res) => this.setState({loading: false, referrers: res})) } else { api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/referrers`, this.props.query, {show_noref: this.showNoRef()}) .then((res) => this.setState({loading: false, referrers: res})) } } renderExternalLink(referrer) { if (this.props.query.filters.source && this.props.query.filters.source !== 'Google' && referrer.name !== 'Direct / None') { return ( ) } return null } renderReferrer(referrer) { const query = new URLSearchParams(window.location.search) if (this.props.query.filters.source) { query.set('referrer', referrer.name) } else { query.set('source', referrer.name) } return (
{ referrer.name } { this.renderExternalLink(referrer) }
{numberFormatter(referrer.count)}
) } label() { return this.props.query.period === 'realtime' ? 'Active visitors' : 'Visitors' } renderList() { if (this.state.referrers.length > 0) { const source = this.props.query.filters.source const keyLabel = source === 'Google' ? 'Search term' : source ? 'Referrer' : 'Source' const valLabel = this.props.query.period === 'realtime' ? 'Active visitors' : 'Visitors' return (
{ keyLabel } { valLabel }
{this.state.referrers.map(this.renderReferrer.bind(this))}
) } else { return
No data yet
} } renderContent() { const source = this.props.query.filters.source const title = source === 'Google' ? 'Search terms' : source ? 'Top Referrers' : 'Top Sources' const endpoint = source ? 'referrers/' + encodeURIComponent(source) : 'referrers' if (this.state.referrers) { return (

{title}

{ this.renderList() }
) } } render() { return (
{ this.state.loading &&
} { this.renderContent() }
) } }