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'
|
2021-12-03 14:59:32 +03:00
|
|
|
import numberFormatter, {durationFormatter} from '../../util/number-formatter'
|
2019-11-19 07:30:42 +03:00
|
|
|
import {parseQuery} from '../../query'
|
|
|
|
|
2020-09-28 11:29:24 +03:00
|
|
|
const TITLES = {
|
2021-08-18 15:21:47 +03:00
|
|
|
sources: 'Top Sources',
|
2020-09-28 11:29:24 +03:00
|
|
|
utm_mediums: 'Top UTM mediums',
|
|
|
|
utm_sources: 'Top UTM sources',
|
|
|
|
utm_campaigns: 'Top UTM campaigns'
|
|
|
|
}
|
|
|
|
|
|
|
|
class SourcesModal extends React.Component {
|
2019-11-19 07:30:42 +03:00
|
|
|
constructor(props) {
|
|
|
|
super(props)
|
2020-01-06 16:51:43 +03:00
|
|
|
this.state = {
|
|
|
|
loading: true,
|
2020-09-28 11:29:24 +03:00
|
|
|
sources: [],
|
2020-09-10 10:42:43 +03:00
|
|
|
query: parseQuery(props.location.search, props.site),
|
|
|
|
page: 1,
|
|
|
|
moreResultsAvailable: false
|
2020-01-06 16:51:43 +03:00
|
|
|
}
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|
|
|
|
|
2020-09-28 11:29:24 +03:00
|
|
|
loadSources() {
|
|
|
|
const {site} = this.props
|
|
|
|
const {query, page, sources} = this.state
|
2020-09-10 10:42:43 +03:00
|
|
|
|
2021-05-18 15:14:33 +03:00
|
|
|
const detailed = this.showExtra()
|
|
|
|
api.get(`/api/stats/${encodeURIComponent(site.domain)}/${this.currentFilter()}`, query, {limit: 100, page, detailed, show_noref: true})
|
2020-09-28 11:29:24 +03:00
|
|
|
.then((res) => this.setState({loading: false, sources: sources.concat(res), moreResultsAvailable: res.length === 100}))
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
this.loadSources()
|
|
|
|
}
|
2019-11-19 07:30:42 +03:00
|
|
|
|
2020-09-28 11:29:24 +03:00
|
|
|
componentDidUpdate(prevProps) {
|
|
|
|
if (this.props.location.pathname !== prevProps.location.pathname) {
|
|
|
|
this.setState({sources: [], loading: true}, this.loadSources.bind(this))
|
2020-01-16 16:40:06 +03:00
|
|
|
}
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|
|
|
|
|
2020-09-28 11:29:24 +03:00
|
|
|
currentFilter() {
|
|
|
|
const urlparts = this.props.location.pathname.split('/')
|
|
|
|
return urlparts[urlparts.length - 1]
|
2020-09-10 10:42:43 +03:00
|
|
|
}
|
|
|
|
|
2020-07-17 11:09:54 +03:00
|
|
|
showExtra() {
|
2020-07-14 16:52:26 +03:00
|
|
|
return this.state.query.period !== 'realtime' && !this.state.query.filters.goal
|
2020-01-06 16:51:43 +03:00
|
|
|
}
|
|
|
|
|
2021-09-20 17:17:11 +03:00
|
|
|
showConversionRate() {
|
|
|
|
return !!this.state.query.filters.goal
|
|
|
|
}
|
|
|
|
|
2020-09-10 10:42:43 +03:00
|
|
|
loadMore() {
|
2020-09-28 11:29:24 +03:00
|
|
|
this.setState({loading: true, page: this.state.page + 1}, this.loadSources.bind(this))
|
2020-09-10 10:42:43 +03:00
|
|
|
}
|
|
|
|
|
2020-01-06 16:51:43 +03:00
|
|
|
formatBounceRate(page) {
|
2020-01-29 16:49:46 +03:00
|
|
|
if (typeof(page.bounce_rate) === 'number') {
|
2020-01-06 16:51:43 +03:00
|
|
|
return page.bounce_rate + '%'
|
|
|
|
} else {
|
|
|
|
return '-'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-28 11:29:24 +03:00
|
|
|
formatDuration(source) {
|
|
|
|
if (typeof(source.visit_duration) === 'number') {
|
|
|
|
return durationFormatter(source.visit_duration)
|
2020-07-17 11:09:54 +03:00
|
|
|
} else {
|
|
|
|
return '-'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-28 11:29:24 +03:00
|
|
|
renderSource(source) {
|
2020-07-30 11:18:28 +03:00
|
|
|
const query = new URLSearchParams(window.location.search)
|
2020-09-28 11:29:24 +03:00
|
|
|
const filter = this.currentFilter()
|
|
|
|
if (filter === 'sources') query.set('source', source.name)
|
|
|
|
if (filter === 'utm_mediums') query.set('utm_medium', source.name)
|
|
|
|
if (filter === 'utm_sources') query.set('utm_source', source.name)
|
|
|
|
if (filter === 'utm_campaigns') query.set('utm_campaign', source.name)
|
2020-07-30 11:18:28 +03:00
|
|
|
|
2021-09-20 17:17:11 +03:00
|
|
|
console.log(source)
|
|
|
|
|
2019-11-19 07:30:42 +03:00
|
|
|
return (
|
2020-12-16 12:57:28 +03:00
|
|
|
<tr className="text-sm dark:text-gray-200" key={source.name}>
|
2020-01-06 16:51:43 +03:00
|
|
|
<td className="p-2">
|
2021-08-18 15:21:47 +03:00
|
|
|
<img
|
|
|
|
src={`/favicon/sources/${encodeURIComponent(source.name)}`}
|
|
|
|
className="h-4 w-4 mr-2 align-middle inline"
|
|
|
|
/>
|
2020-09-28 11:29:24 +03:00
|
|
|
<Link className="hover:underline" to={{search: query.toString(), pathname: '/' + encodeURIComponent(this.props.site.domain)}}>{ source.name }</Link>
|
2020-01-06 16:51:43 +03:00
|
|
|
</td>
|
2021-09-29 14:28:29 +03:00
|
|
|
{this.showConversionRate() && <td className="p-2 w-32 font-medium" align="right">{numberFormatter(source.total_visitors)}</td> }
|
2021-11-04 15:20:39 +03:00
|
|
|
<td className="p-2 w-32 font-medium" align="right">{numberFormatter(source.visitors)}</td>
|
2020-09-28 11:29:24 +03:00
|
|
|
{this.showExtra() && <td className="p-2 w-32 font-medium" align="right">{this.formatBounceRate(source)}</td> }
|
|
|
|
{this.showExtra() && <td className="p-2 w-32 font-medium" align="right">{this.formatDuration(source)}</td> }
|
2021-09-20 17:17:11 +03:00
|
|
|
{this.showConversionRate() && <td className="p-2 w-32 font-medium" align="right">{source.conversion_rate}%</td> }
|
2020-01-06 16:51:43 +03:00
|
|
|
</tr>
|
2019-11-19 07:30:42 +03:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-07-14 16:52:26 +03:00
|
|
|
label() {
|
2021-09-29 14:28:29 +03:00
|
|
|
if (this.state.query.period === 'realtime') {
|
|
|
|
return 'Current visitors'
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.showConversionRate()) {
|
|
|
|
return 'Conversions'
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'Visitors'
|
2020-07-14 16:52:26 +03:00
|
|
|
}
|
|
|
|
|
2020-09-10 10:42:43 +03:00
|
|
|
renderLoading() {
|
2020-03-03 17:18:02 +03:00
|
|
|
if (this.state.loading) {
|
2020-09-10 10:42:43 +03:00
|
|
|
return <div className="loading my-16 mx-auto"><div></div></div>
|
|
|
|
} else if (this.state.moreResultsAvailable) {
|
2020-03-03 17:18:02 +03:00
|
|
|
return (
|
2020-09-10 10:42:43 +03:00
|
|
|
<div className="w-full text-center my-4">
|
2020-11-19 15:24:08 +03:00
|
|
|
<button onClick={this.loadMore.bind(this)} type="button" className="inline-flex items-center px-3 py-2 border border-transparent text-sm leading-4 font-medium rounded-md text-white bg-indigo-600 hover:bg-indigo-500 focus:outline-none focus:border-indigo-700 focus:ring active:bg-indigo-700 transition ease-in-out duration-150">
|
2020-09-10 10:42:43 +03:00
|
|
|
Load more
|
|
|
|
</button>
|
|
|
|
</div>
|
2019-11-19 07:30:42 +03:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-28 11:29:24 +03:00
|
|
|
title() {
|
|
|
|
return TITLES[this.currentFilter()]
|
|
|
|
}
|
|
|
|
|
2019-11-19 07:30:42 +03:00
|
|
|
render() {
|
|
|
|
return (
|
2020-03-03 17:18:02 +03:00
|
|
|
<Modal site={this.props.site}>
|
2020-12-16 12:57:28 +03:00
|
|
|
<h1 className="text-xl font-bold dark:text-gray-100">{this.title()}</h1>
|
2020-09-10 10:42:43 +03:00
|
|
|
|
2020-12-16 12:57:28 +03:00
|
|
|
<div className="my-4 border-b border-gray-300 dark:border-gray-500"></div>
|
2020-09-10 10:42:43 +03:00
|
|
|
|
|
|
|
<main className="modal__content">
|
2021-08-13 11:00:42 +03:00
|
|
|
<table className="w-max overflow-x-auto md:w-full table-striped table-fixed">
|
2020-09-10 10:42:43 +03:00
|
|
|
<thead>
|
|
|
|
<tr>
|
2021-08-13 11:00:42 +03:00
|
|
|
<th className="p-2 w-48 md:w-56 lg:w-1/3 text-xs tracking-wide font-bold text-gray-500 dark:text-gray-400" align="left">Source</th>
|
2021-09-29 14:28:29 +03:00
|
|
|
{this.showConversionRate() && <th className="p-2 w-32 text-xs tracking-wide font-bold text-gray-500 dark:text-gray-400" align="right">Total visitors</th>}
|
2020-12-16 12:57:28 +03:00
|
|
|
<th className="p-2 w-32 text-xs tracking-wide font-bold text-gray-500 dark:text-gray-400" align="right">{this.label()}</th>
|
|
|
|
{this.showExtra() && <th className="p-2 w-32 text-xs tracking-wide font-bold text-gray-500 dark:text-gray-400" align="right">Bounce rate</th>}
|
|
|
|
{this.showExtra() && <th className="p-2 w-32 text-xs tracking-wide font-bold text-gray-500 dark:text-gray-400" align="right">Visit duration</th>}
|
2021-09-20 17:17:11 +03:00
|
|
|
{this.showConversionRate() && <th className="p-2 w-32 text-xs tracking-wide font-bold text-gray-500 dark:text-gray-400" align="right">CR</th>}
|
2020-09-10 10:42:43 +03:00
|
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
2020-09-28 11:29:24 +03:00
|
|
|
{ this.state.sources.map(this.renderSource.bind(this)) }
|
2020-09-10 10:42:43 +03:00
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
</main>
|
|
|
|
|
|
|
|
{ this.renderLoading() }
|
2019-11-19 07:30:42 +03:00
|
|
|
</Modal>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-28 11:29:24 +03:00
|
|
|
export default withRouter(SourcesModal)
|