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 '../../util/number-formatter' import * as api from '../../api' import LazyLoader from '../../components/lazy-loader' 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 } this.onVisible = this.onVisible.bind(this) this.fetchReferrers = this.fetchReferrers.bind(this) } onVisible() { this.fetchReferrers() if (this.props.query.period === 'realtime') { document.addEventListener('tick', this.fetchReferrers) } } componentDidUpdate(prevProps) { if (this.props.query !== prevProps.query) { this.setState({ loading: true, referrers: null }) this.fetchReferrers() } } componentWillUnmount() { document.removeEventListener('tick', this.fetchReferrers) } showConversionRate() { return !!this.props.query.filters.goal } 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) .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) .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 maxWidthDeduction = this.showConversionRate() ? "10rem" : "5rem" const query = new URLSearchParams(window.location.search) query.set('referrer', referrer.name) return (
{referrer.name} {this.renderExternalLink(referrer)} {numberFormatter(referrer.visitors)} {this.showConversionRate() && {referrer.conversion_rate}%}
) } label() { if (this.props.query.period === 'realtime') { return 'Current visitors' } if (this.showConversionRate()) { return 'Conversions' } return 'Visitors' } renderList() { if (this.state.referrers.length > 0) { return (
Referrer
{this.label()} {this.showConversionRate() && CR}
{this.state.referrers.map(this.renderReferrer.bind(this))}
) } return (
No data yet
) } renderContent() { if (this.state.referrers) { return ( {this.renderList()} ) } } render() { return (

Top Referrers

{this.state.loading &&
} {this.renderContent()}
) } }