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' class AllSources 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() { api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/sources`, this.props.query, {show_noref: this.showNoRef()}) .then((res) => this.setState({loading: false, referrers: res})) } renderReferrer(referrer) { const query = new URLSearchParams(window.location.search) query.set('source', referrer.name) return (
{ referrer.name }
{numberFormatter(referrer.count)}
) } label() { return this.props.query.period === 'realtime' ? 'Current visitors' : 'Visitors' } renderList() { if (this.state.referrers.length > 0) { return (
Source {this.label()}
{this.state.referrers.map(this.renderReferrer.bind(this))}
) } else { return
No data yet
} } renderContent() { if (this.state.referrers) { return (

Top sources

{ this.props.renderTabs() }
{ this.renderList() }
) } } render() { return (
{ this.state.loading &&
} { this.renderContent() }
) } } const UTM_TAGS = { utm_medium: {label: 'UTM Medium', endpoint: 'utm_mediums'}, utm_source: {label: 'UTM Source', endpoint: 'utm_sources'}, utm_campaign: {label: 'UTM Campaign', endpoint: 'utm_campaigns'}, } class UTMSources 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.props.tab !== prevProps.tab) { this.setState({loading: true, referrers: null}) this.fetchReferrers() } } showNoRef() { return this.props.query.period === 'realtime' } fetchReferrers() { const endpoint = UTM_TAGS[this.props.tab].endpoint api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/${endpoint}`, this.props.query, {show_noref: this.showNoRef()}) .then((res) => this.setState({loading: false, referrers: res})) } renderReferrer(referrer) { const query = new URLSearchParams(window.location.search) query.set(this.props.tab, referrer.name) return (
{ referrer.name }
{numberFormatter(referrer.count)}
) } label() { return this.props.query.period === 'realtime' ? 'Current visitors' : 'Visitors' } renderList() { if (this.state.referrers.length > 0) { return (
{UTM_TAGS[this.props.tab].label} {this.label()}
{this.state.referrers.map(this.renderReferrer.bind(this))}
) } else { return
No data yet
} } renderContent() { if (this.state.referrers) { return (

Top sources

{ this.props.renderTabs() }
{ this.renderList() }
) } } render() { return (
{ this.state.loading &&
} { this.renderContent() }
) } } export default class SourceList extends React.Component { constructor(props) { super(props) this.tabKey = 'sourceTab__' + props.site.domain const storedTab = window.localStorage[this.tabKey] this.state = { tab: storedTab || 'all' } } setTab(tab) { return () => { window.localStorage[this.tabKey] = tab this.setState({tab}) } } renderTabs() { const activeClass = 'inline-block h-5 text-indigo-700 dark:text-indigo-500 font-bold border-b-2 border-indigo-700 dark:border-indigo-500' const defaultClass = 'hover:text-indigo-600 cursor-pointer' return ( ) } render() { if (this.state.tab === 'all') { return } else if (this.state.tab === 'utm_medium') { return } else if (this.state.tab === 'utm_source') { return } else if (this.state.tab === 'utm_campaign') { return } } }