import React from "react"; import { Link, withRouter } from 'react-router-dom' import Modal from './modal' import * as api from '../../api' import numberFormatter, {durationFormatter} from '../../number-formatter' import {parseQuery, toHuman} from '../../query' import {formatFullDate} from '../../date' 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/${encodeURIComponent(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.showExtra() ? 'bounce_rate,visit_duration' : null api.get(`/api/stats/${encodeURIComponent(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})) } } showExtra() { return this.state.query.period !== 'realtime' && !this.state.query.filters.goal } formatBounceRate(ref) { if (typeof(ref.bounce_rate) === 'number') { return ref.bounce_rate + '%' } else { return '-' } } formatDuration(referrer) { if (typeof(referrer.visit_duration) === 'number') { return durationFormatter(referrer.visit_duration) } 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-gray-300' return (
{tweet.author_name}
@{tweet.author_handle}
{formatFullDate(new Date(tweet.created))}
) } renderReferrer(referrer) { if (referrer.tweets) { return ( { this.renderReferrerName(referrer.name) } appears in {referrer.tweets.length} tweets
{ referrer.tweets.map(this.renderTweet) }
{numberFormatter(referrer.count)} {this.showExtra() && {this.formatBounceRate(referrer)} } {this.showExtra() && {this.formatDuration(referrer)} } ) } else { return ( { this.renderReferrerName(referrer.name) } {numberFormatter(referrer.count)} {this.showExtra() && {this.formatBounceRate(referrer)} } {this.showExtra() && {this.formatDuration(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.showExtra() && } {this.showExtra() && } { this.state.referrers.map(this.renderReferrer.bind(this)) }
Referrer VisitorsBounce rateVisit duration
) } } render() { return ( { this.renderBody() } ) } } export default withRouter(ReferrerDrilldownModal)