import React from "react"; import { Link, withRouter } from 'react-router-dom' import FadeIn from '../../fade-in' import Modal from './modal' import * as api from '../../api' import numberFormatter, {durationFormatter} from '../../number-formatter' import {parseQuery} from '../../query' class ReferrersModal extends React.Component { constructor(props) { super(props) this.state = { loading: true, query: parseQuery(props.location.search, props.site) } } componentDidMount() { if (this.state.query.filters.goal) { api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/goal/referrers`, this.state.query, {limit: 100}) .then((res) => this.setState({loading: false, referrers: res})) } else { const include = this.showExtra() ? 'bounce_rate,visit_duration' : null api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/referrers`, this.state.query, { limit: 100, include: include, show_noref: true}) .then((res) => this.setState({loading: false, referrers: res})) } } showExtra() { return this.state.query.period !== 'realtime' && !this.state.query.filters.goal } formatBounceRate(page) { if (typeof(page.bounce_rate) === 'number') { return page.bounce_rate + '%' } else { return '-' } } formatDuration(referrer) { if (typeof(referrer.visit_duration) === 'number') { return durationFormatter(referrer.visit_duration) } else { return '-' } } renderReferrer(referrer) { const query = new URLSearchParams(window.location.search) query.set('source', referrer.name) return ( { referrer.name } {numberFormatter(referrer.count)} {this.showExtra() && {this.formatBounceRate(referrer)} } {this.showExtra() && {this.formatDuration(referrer)} } ) } label() { return this.state.query.period === 'realtime' ? 'Current visitors' : 'Visitors' } renderBody() { if (this.state.loading) { return (
) } else if (this.state.referrers) { return (

Top Sources

{this.showExtra() && } {this.showExtra() && } { this.state.referrers.map(this.renderReferrer.bind(this)) }
Referrer {this.label()}Bounce rateVisit duration
) } } render() { return ( { this.renderBody() } ) } } export default withRouter(ReferrersModal)