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 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.showBounceRate() ? 'bounce_rate' : null
api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/referrers`, this.state.query, {limit: 100, include: include})
.then((res) => this.setState({loading: false, referrers: res}))
}
}
showBounceRate() {
return !this.state.query.filters.goal
}
formatBounceRate(page) {
if (typeof(page.bounce_rate) === 'number') {
return page.bounce_rate + '%'
} else {
return '-'
}
}
renderReferrer(referrer) {
return (
{ referrer.name }
|
{numberFormatter(referrer.count)} |
{this.showBounceRate() && {this.formatBounceRate(referrer)} | }
)
}
renderBody() {
if (this.state.loading) {
return (
)
} else if (this.state.referrers) {
return (
Top Referrers
Referrer |
Visitors |
{this.showBounceRate() && Bounce rate | }
{ this.state.referrers.map(this.renderReferrer.bind(this)) }
)
}
}
render() {
return (
{ this.renderBody() }
)
}
}
export default withRouter(ReferrersModal)