import React from "react"; import { Link, withRouter } from 'react-router-dom' import Modal from './modal' import * as api from '../../api' import numberFormatter from '../../number-formatter' import {parseQuery, toHuman} from '../../query' class ReferrerDrilldownModal 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/${this.props.site.domain}/goal/referrers/${this.props.match.params.referrer}`, this.state.query, {limit: 100}) .then((res) => this.setState({loading: false, referrers: res.referrers, totalVisitors: res.total_visitors})) } else { const include = this.showBounceRate() ? 'bounce_rate' : null api.get(`/api/stats/${this.props.site.domain}/referrers/${this.props.match.params.referrer}`, this.state.query, {limit: 100, include: include}) .then((res) => this.setState({loading: false, referrers: res.referrers, totalVisitors: res.total_visitors})) } } showBounceRate() { return !this.state.query.filters.goal } formatBounceRate(ref) { if (typeof(ref.bounce_rate) === 'number') { return ref.bounce_rate + '%' } else { return '-' } } renderReferrerName(name) { if (name) { return {name} } else { return '(no referrer)' } } renderTweet(tweet, index) { const authorUrl = `https://twitter.com/${tweet.author_handle}` const tweetUrl = `${authorUrl}/status/${tweet.tweet_id}` const border = index === 0 ? '' : ' pt-4 border-t border-grey-light' return (
{tweet.author_name}
@{tweet.author_handle}
{tweet.created}
) } renderReferrer(referrer) { if (false && referrer.tweets) { return ( { this.renderReferrerName(referrer.name) } appears in {referrer.tweets.length} tweets
{ referrer.tweets.map(this.renderTweet) }
{numberFormatter(referrer.count)} {this.showBounceRate() && {this.formatBounceRate(referrer)} } ) } else { return ( { this.renderReferrerName(referrer.name) } {numberFormatter(referrer.count)} {this.showBounceRate() && {this.formatBounceRate(referrer)} } ) } } renderGoalText() { if (this.state.query.filters.goal) { return (

completed {this.state.query.filters.goal}

) } } renderBody() { if (this.state.loading) { return (
) } else if (this.state.referrers) { return (
← All referrers

{this.state.totalVisitors} visitors from {this.props.match.params.referrer}
{toHuman(this.state.query)}

{this.renderGoalText()} {this.showBounceRate() && } { this.state.referrers.map(this.renderReferrer.bind(this)) }
Referrer VisitorsBounce rate
) } } render() { return ( { this.renderBody() } ) } } export default withRouter(ReferrerDrilldownModal)