2019-11-19 07:30:42 +03:00
|
|
|
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'
|
2020-02-12 16:55:40 +03:00
|
|
|
import {formatFullDate} from '../../date'
|
2019-11-19 07:30:42 +03:00
|
|
|
|
|
|
|
class ReferrerDrilldownModal extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props)
|
|
|
|
this.state = {
|
|
|
|
loading: true,
|
|
|
|
query: parseQuery(props.location.search, props.site)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
2020-01-16 16:40:06 +03:00
|
|
|
if (this.state.query.filters.goal) {
|
2020-02-04 16:44:13 +03:00
|
|
|
api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/goal/referrers/${this.props.match.params.referrer}`, this.state.query, {limit: 100})
|
2020-01-16 16:40:06 +03:00
|
|
|
.then((res) => this.setState({loading: false, referrers: res.referrers, totalVisitors: res.total_visitors}))
|
|
|
|
} else {
|
|
|
|
const include = this.showBounceRate() ? 'bounce_rate' : null
|
2020-01-15 11:57:47 +03:00
|
|
|
|
2020-02-04 16:44:13 +03:00
|
|
|
api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/referrers/${this.props.match.params.referrer}`, this.state.query, {limit: 100, include: include})
|
2020-01-16 16:40:06 +03:00
|
|
|
.then((res) => this.setState({loading: false, referrers: res.referrers, totalVisitors: res.total_visitors}))
|
|
|
|
}
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|
|
|
|
|
2020-01-15 11:57:47 +03:00
|
|
|
showBounceRate() {
|
|
|
|
return !this.state.query.filters.goal
|
|
|
|
}
|
|
|
|
|
2020-01-06 16:51:43 +03:00
|
|
|
formatBounceRate(ref) {
|
2020-01-29 16:49:46 +03:00
|
|
|
if (typeof(ref.bounce_rate) === 'number') {
|
2020-01-06 16:51:43 +03:00
|
|
|
return ref.bounce_rate + '%'
|
|
|
|
} else {
|
|
|
|
return '-'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-16 14:39:47 +03:00
|
|
|
renderReferrerName(name) {
|
|
|
|
if (name) {
|
|
|
|
return <a className="hover:underline" target="_blank" href={'//' + name}>{name}</a>
|
|
|
|
} 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'
|
|
|
|
|
2019-11-19 07:30:42 +03:00
|
|
|
return (
|
2020-01-16 14:39:47 +03:00
|
|
|
<div key={tweet.tweet_id}>
|
|
|
|
<div className={"flex items-center my-4" + border} >
|
|
|
|
<a className="flex items-center group" href={authorUrl} target="_blank">
|
2020-02-12 16:55:40 +03:00
|
|
|
<img className="rounded-full w-8" src={tweet.author_image} />
|
|
|
|
<div className="ml-2 leading-tight">
|
|
|
|
<div className="font-bold group-hover:text-blue">{tweet.author_name}</div>
|
|
|
|
<div className="text-xs text-grey-dark">@{tweet.author_handle}</div>
|
|
|
|
</div>
|
2020-01-16 14:39:47 +03:00
|
|
|
</a>
|
|
|
|
<a className="ml-auto twitter-icon" href={tweetUrl} target="_blank"></a>
|
|
|
|
</div>
|
|
|
|
<div className="my-2 cursor-text tweet-text" dangerouslySetInnerHTML={{__html: tweet.text}}>
|
|
|
|
</div>
|
|
|
|
<div className="text-xs text-grey-darker font-medium">
|
2020-02-12 16:55:40 +03:00
|
|
|
{formatFullDate(new Date(tweet.created))}
|
2020-01-16 14:39:47 +03:00
|
|
|
</div>
|
|
|
|
</div>
|
2019-11-19 07:30:42 +03:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-01-16 14:39:47 +03:00
|
|
|
renderReferrer(referrer) {
|
2020-02-12 16:55:40 +03:00
|
|
|
if (referrer.tweets) {
|
2020-01-16 14:39:47 +03:00
|
|
|
return (
|
|
|
|
<tr className="text-sm" key={referrer.name}>
|
|
|
|
<td className="p-2">
|
|
|
|
{ this.renderReferrerName(referrer.name) }
|
|
|
|
<span className="text-grey-dark ml-2 text-xs">
|
|
|
|
appears in {referrer.tweets.length} tweets
|
|
|
|
</span>
|
2020-02-12 16:55:40 +03:00
|
|
|
<div className="my-4 pl-4 border-l-2 border-grey-light">
|
2020-01-16 14:39:47 +03:00
|
|
|
{ referrer.tweets.map(this.renderTweet) }
|
|
|
|
</div>
|
|
|
|
</td>
|
|
|
|
<td className="p-2 w-32 font-medium" align="right" valign="top">{numberFormatter(referrer.count)}</td>
|
|
|
|
{this.showBounceRate() && <td className="p-2 w-32 font-medium" align="right" valign="top">{this.formatBounceRate(referrer)}</td> }
|
|
|
|
</tr>
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
return (
|
|
|
|
<tr className="text-sm" key={referrer.name}>
|
|
|
|
<td className="p-2 truncate">
|
|
|
|
{ this.renderReferrerName(referrer.name) }
|
|
|
|
</td>
|
|
|
|
<td className="p-2 w-32 font-medium" align="right">{numberFormatter(referrer.count)}</td>
|
|
|
|
{this.showBounceRate() && <td className="p-2 w-32 font-medium" align="right">{this.formatBounceRate(referrer)}</td> }
|
|
|
|
</tr>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-20 08:48:27 +03:00
|
|
|
renderGoalText() {
|
|
|
|
if (this.state.query.filters.goal) {
|
|
|
|
return (
|
|
|
|
<h1 className="text-grey-darker leading-none">completed {this.state.query.filters.goal}</h1>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-19 07:30:42 +03:00
|
|
|
renderBody() {
|
|
|
|
if (this.state.loading) {
|
|
|
|
return (
|
|
|
|
<div className="loading my-32 mx-auto"><div></div></div>
|
|
|
|
)
|
|
|
|
} else if (this.state.referrers) {
|
|
|
|
return (
|
|
|
|
<React.Fragment>
|
|
|
|
<header className="modal__header">
|
2020-02-04 16:44:13 +03:00
|
|
|
<Link to={`/${encodeURIComponent(this.props.site.domain)}/referrers${window.location.search}`} className="font-bold text-grey-darker hover:underline">← All referrers</Link>
|
2019-11-19 07:30:42 +03:00
|
|
|
</header>
|
|
|
|
|
|
|
|
<div className="my-4 border-b border-grey-light"></div>
|
|
|
|
<main className="modal__content mt-0">
|
2019-11-20 08:48:27 +03:00
|
|
|
<h1 className="mb-0 leading-none">{this.state.totalVisitors} visitors from {this.props.match.params.referrer}<br /> {toHuman(this.state.query)}</h1>
|
|
|
|
{this.renderGoalText()}
|
2019-11-19 07:30:42 +03:00
|
|
|
|
2020-01-06 16:51:43 +03:00
|
|
|
<table className="w-full table-striped table-fixed mt-4">
|
|
|
|
<thead>
|
|
|
|
<tr>
|
|
|
|
<th className="p-2 text-xs tracking-wide font-bold text-grey-dark" align="left">Referrer</th>
|
|
|
|
<th className="p-2 w-32 text-xs tracking-wide font-bold text-grey-dark" align="right">Visitors</th>
|
2020-01-15 11:57:47 +03:00
|
|
|
{this.showBounceRate() && <th className="p-2 w-32 text-xs tracking-wide font-bold text-grey-dark" align="right">Bounce rate</th>}
|
2020-01-06 16:51:43 +03:00
|
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
|
|
|
{ this.state.referrers.map(this.renderReferrer.bind(this)) }
|
|
|
|
</tbody>
|
|
|
|
</table>
|
2019-11-19 07:30:42 +03:00
|
|
|
</main>
|
|
|
|
</React.Fragment>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<Modal site={this.props.site}>
|
|
|
|
{ this.renderBody() }
|
|
|
|
</Modal>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default withRouter(ReferrerDrilldownModal)
|