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'
|
2020-07-17 11:25:20 +03:00
|
|
|
import numberFormatter, {durationFormatter} from '../../number-formatter'
|
2019-11-19 07:30:42 +03:00
|
|
|
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 {
|
2020-07-17 11:25:20 +03:00
|
|
|
const include = this.showExtra() ? 'bounce_rate,visit_duration' : 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-07-17 11:25:20 +03:00
|
|
|
showExtra() {
|
2020-07-14 16:52:26 +03:00
|
|
|
return this.state.query.period !== 'realtime' && !this.state.query.filters.goal
|
2020-01-15 11:57:47 +03:00
|
|
|
}
|
|
|
|
|
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-07-17 11:25:20 +03:00
|
|
|
formatDuration(referrer) {
|
|
|
|
if (typeof(referrer.visit_duration) === 'number') {
|
|
|
|
return durationFormatter(referrer.visit_duration)
|
|
|
|
} else {
|
|
|
|
return '-'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-30 11:18:28 +03:00
|
|
|
renderExternalLink(name) {
|
2020-08-05 14:45:20 +03:00
|
|
|
if (name !== 'Direct / None') {
|
|
|
|
return (
|
2020-10-09 15:23:08 +03:00
|
|
|
<a target="_blank" href={'//' + name} className="hidden group-hover:block">
|
2020-08-05 14:45:20 +03:00
|
|
|
<svg className="inline h-4 w-4 ml-1 -mt-1 text-gray-600" fill="currentColor" viewBox="0 0 20 20"><path d="M11 3a1 1 0 100 2h2.586l-6.293 6.293a1 1 0 101.414 1.414L15 6.414V9a1 1 0 102 0V4a1 1 0 00-1-1h-5z"></path><path d="M5 5a2 2 0 00-2 2v8a2 2 0 002 2h8a2 2 0 002-2v-3a1 1 0 10-2 0v3H5V7h3a1 1 0 000-2H5z"></path></svg>
|
|
|
|
</a>
|
|
|
|
)
|
|
|
|
}
|
2020-07-30 11:18:28 +03:00
|
|
|
}
|
|
|
|
|
2020-01-16 14:39:47 +03:00
|
|
|
renderReferrerName(name) {
|
2020-07-30 11:18:28 +03:00
|
|
|
const query = new URLSearchParams(window.location.search)
|
|
|
|
query.set('referrer', name)
|
|
|
|
|
|
|
|
return (
|
2020-10-09 15:23:08 +03:00
|
|
|
<span className="flex group">
|
2020-07-30 11:18:28 +03:00
|
|
|
<Link className="block truncate hover:underline" to={{search: query.toString(), pathname: '/' + this.props.site.domain}} title={name}>
|
2020-08-05 14:45:20 +03:00
|
|
|
{name}
|
2020-07-30 11:18:28 +03:00
|
|
|
</Link>
|
|
|
|
{ this.renderExternalLink(name) }
|
|
|
|
</span>
|
|
|
|
)
|
2020-01-16 14:39:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
renderTweet(tweet, index) {
|
|
|
|
const authorUrl = `https://twitter.com/${tweet.author_handle}`
|
|
|
|
const tweetUrl = `${authorUrl}/status/${tweet.tweet_id}`
|
2020-03-06 12:11:38 +03:00
|
|
|
const border = index === 0 ? '' : ' pt-4 border-t border-gray-300'
|
2020-01-16 14:39:47 +03:00
|
|
|
|
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">
|
2020-03-06 12:11:38 +03:00
|
|
|
<div className="font-bold group-hover:text-blue-500">{tweet.author_name}</div>
|
2020-03-26 16:43:55 +03:00
|
|
|
<div className="text-xs text-gray-500">@{tweet.author_handle}</div>
|
2020-02-12 16:55:40 +03:00
|
|
|
</div>
|
2020-01-16 14:39:47 +03:00
|
|
|
</a>
|
|
|
|
<a className="ml-auto twitter-icon" href={tweetUrl} target="_blank"></a>
|
|
|
|
</div>
|
2020-02-26 16:31:12 +03:00
|
|
|
<div className="my-2 cursor-text tweet-text whitespace-pre-wrap" dangerouslySetInnerHTML={{__html: tweet.text}}>
|
2020-01-16 14:39:47 +03:00
|
|
|
</div>
|
2020-03-06 12:11:38 +03:00
|
|
|
<div className="text-xs text-gray-700 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) }
|
2020-03-26 16:43:55 +03:00
|
|
|
<span className="text-gray-500 ml-2 text-xs">
|
2020-01-16 14:39:47 +03:00
|
|
|
appears in {referrer.tweets.length} tweets
|
|
|
|
</span>
|
2020-03-06 12:11:38 +03:00
|
|
|
<div className="my-4 pl-4 border-l-2 border-gray-300">
|
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>
|
2020-07-17 11:25:20 +03:00
|
|
|
{this.showExtra() && <td className="p-2 w-32 font-medium" align="right" valign="top">{this.formatBounceRate(referrer)}</td> }
|
|
|
|
{this.showExtra() && <td className="p-2 w-32 font-medium" align="right" valign="top">{this.formatDuration(referrer)}</td> }
|
2020-01-16 14:39:47 +03:00
|
|
|
</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>
|
2020-07-17 11:25:20 +03:00
|
|
|
{this.showExtra() && <td className="p-2 w-32 font-medium" align="right">{this.formatBounceRate(referrer)}</td> }
|
|
|
|
{this.showExtra() && <td className="p-2 w-32 font-medium" align="right">{this.formatDuration(referrer)}</td> }
|
2020-01-16 14:39:47 +03:00
|
|
|
</tr>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-20 08:48:27 +03:00
|
|
|
renderGoalText() {
|
|
|
|
if (this.state.query.filters.goal) {
|
|
|
|
return (
|
2020-03-26 16:43:55 +03:00
|
|
|
<h1 className="text-xl font-semibold text-gray-500 leading-none">completed {this.state.query.filters.goal}</h1>
|
2019-11-20 08:48:27 +03:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-19 07:30:42 +03:00
|
|
|
renderBody() {
|
2020-03-03 17:18:02 +03:00
|
|
|
if (this.state.loading) {
|
|
|
|
return (
|
|
|
|
<div className="loading mt-32 mx-auto"><div></div></div>
|
|
|
|
)
|
|
|
|
} else if (this.state.referrers) {
|
2019-11-19 07:30:42 +03:00
|
|
|
return (
|
|
|
|
<React.Fragment>
|
2020-07-30 11:18:28 +03:00
|
|
|
<h1 className="text-xl font-bold">Referrer drilldown</h1>
|
2019-11-19 07:30:42 +03:00
|
|
|
|
2020-03-06 12:11:38 +03:00
|
|
|
<div className="my-4 border-b border-gray-300"></div>
|
2019-11-19 07:30:42 +03:00
|
|
|
<main className="modal__content mt-0">
|
2020-08-05 14:45:20 +03:00
|
|
|
<h1 className="text-xl font-semibold mb-0 leading-none">{this.state.totalVisitors} visitors from {decodeURIComponent(this.props.match.params.referrer)}<br /> {toHuman(this.state.query)}</h1>
|
2019-11-20 08:48:27 +03:00
|
|
|
{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>
|
2020-03-26 16:43:55 +03:00
|
|
|
<th className="p-2 text-xs tracking-wide font-bold text-gray-500" align="left">Referrer</th>
|
|
|
|
<th className="p-2 w-32 text-xs tracking-wide font-bold text-gray-500" align="right">Visitors</th>
|
2020-07-17 11:25:20 +03:00
|
|
|
{this.showExtra() && <th className="p-2 w-32 text-xs tracking-wide font-bold text-gray-500" align="right">Bounce rate</th>}
|
|
|
|
{this.showExtra() && <th className="p-2 w-32 text-xs tracking-wide font-bold text-gray-500" align="right">Visit duration</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 (
|
2020-03-04 18:24:18 +03:00
|
|
|
<Modal site={this.props.site}>
|
2019-11-19 07:30:42 +03:00
|
|
|
{ this.renderBody() }
|
|
|
|
</Modal>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default withRouter(ReferrerDrilldownModal)
|