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() {
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 '-'
}
}
renderExternalLink(name) {
if (name !== 'Direct / None') {
return (
)
}
}
renderReferrerName(referrer) {
const query = new URLSearchParams(window.location.search)
query.set('referrer', referrer.name)
return (
{referrer.name}
{ this.renderExternalLink(name) }
)
}
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 dark:border-gray-500'
return (
{formatFullDate(new Date(tweet.created))}
)
}
renderReferrer(referrer) {
if (referrer.tweets) {
return (
{ this.renderReferrerName(referrer) }
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) }
|
{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 (
Referrer drilldown
{this.state.totalVisitors} visitors from {decodeURIComponent(this.props.match.params.referrer)}
{toHuman(this.state.query)}
{this.renderGoalText()}
Referrer |
Visitors |
{this.showExtra() && Bounce rate | }
{this.showExtra() && Visit duration | }
{ this.state.referrers.map(this.renderReferrer.bind(this)) }
)
}
}
render() {
return (
{ this.renderBody() }
)
}
}
export default withRouter(ReferrerDrilldownModal)