2019-11-19 07:30:42 +03:00
|
|
|
import React from 'react';
|
2019-11-20 08:48:27 +03:00
|
|
|
import { Link } from 'react-router-dom'
|
2019-11-19 07:30:42 +03:00
|
|
|
|
2020-10-28 12:09:04 +03:00
|
|
|
import Bar from '../bar'
|
2020-10-30 11:49:41 +03:00
|
|
|
import PropBreakdown from './prop-breakdown'
|
2020-10-28 12:09:04 +03:00
|
|
|
import numberFormatter from '../../number-formatter'
|
|
|
|
import * as api from '../../api'
|
2021-09-21 11:44:51 +03:00
|
|
|
import * as url from '../../url'
|
2021-03-25 12:55:15 +03:00
|
|
|
import LazyLoader from '../../lazy-loader'
|
2019-11-19 07:30:42 +03:00
|
|
|
|
2021-07-08 09:42:30 +03:00
|
|
|
const MOBILE_UPPER_WIDTH = 767
|
|
|
|
const DEFAULT_WIDTH = 1080
|
|
|
|
|
2019-11-19 07:30:42 +03:00
|
|
|
export default class Conversions extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props)
|
2021-07-08 09:42:30 +03:00
|
|
|
this.state = {
|
|
|
|
loading: true,
|
|
|
|
viewport: DEFAULT_WIDTH,
|
|
|
|
}
|
2021-03-25 12:55:15 +03:00
|
|
|
this.onVisible = this.onVisible.bind(this)
|
2021-07-08 09:42:30 +03:00
|
|
|
|
|
|
|
this.handleResize = this.handleResize.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
window.addEventListener('resize', this.handleResize, false);
|
|
|
|
this.handleResize();
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
window.removeEventListener('resize', this.handleResize, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleResize() {
|
|
|
|
this.setState({ viewport: window.innerWidth });
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|
|
|
|
|
2021-03-25 12:55:15 +03:00
|
|
|
onVisible() {
|
2019-11-19 07:30:42 +03:00
|
|
|
this.fetchConversions()
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate(prevProps) {
|
|
|
|
if (this.props.query !== prevProps.query) {
|
|
|
|
this.setState({loading: true, goals: null})
|
|
|
|
this.fetchConversions()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-08 09:42:30 +03:00
|
|
|
getBarMaxWidth() {
|
|
|
|
const { viewport } = this.state;
|
|
|
|
return viewport > MOBILE_UPPER_WIDTH ? "16rem" : "10rem";
|
|
|
|
}
|
|
|
|
|
2019-11-19 07:30:42 +03:00
|
|
|
fetchConversions() {
|
2020-02-04 16:44:13 +03:00
|
|
|
api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/conversions`, this.props.query)
|
2019-11-19 07:30:42 +03:00
|
|
|
.then((res) => this.setState({loading: false, goals: res}))
|
|
|
|
}
|
|
|
|
|
2020-07-14 16:52:26 +03:00
|
|
|
renderGoalText(goalName) {
|
2020-10-20 11:24:20 +03:00
|
|
|
if (this.props.query.period === 'realtime') {
|
2021-07-29 09:54:52 +03:00
|
|
|
return <span className="block px-2 py-1.5 relative z-9 md:truncate break-all dark:text-gray-200">{goalName}</span>
|
2020-07-14 16:52:26 +03:00
|
|
|
} else {
|
|
|
|
return (
|
2021-09-21 11:44:51 +03:00
|
|
|
<Link to={url.setQuery('goal', goalName)} className="block px-2 py-1.5 hover:underline relative z-9 break-all lg:truncate dark:text-gray-200">
|
2021-07-29 09:54:52 +03:00
|
|
|
{goalName}
|
2020-07-14 16:52:26 +03:00
|
|
|
</Link>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-21 11:44:51 +03:00
|
|
|
|
2021-07-08 09:42:30 +03:00
|
|
|
|
2020-07-14 16:52:26 +03:00
|
|
|
renderGoal(goal) {
|
2021-07-08 09:42:30 +03:00
|
|
|
const { viewport } = this.state;
|
2020-10-30 11:49:41 +03:00
|
|
|
const renderProps = this.props.query.filters['goal'] == goal.name && goal.prop_names
|
2020-10-28 12:09:04 +03:00
|
|
|
|
2019-11-19 07:30:42 +03:00
|
|
|
return (
|
2020-10-28 12:09:04 +03:00
|
|
|
<div className="my-2 text-sm" key={goal.name}>
|
|
|
|
<div className="flex items-center justify-between my-2">
|
2021-07-08 09:42:30 +03:00
|
|
|
<Bar
|
2021-11-04 15:20:39 +03:00
|
|
|
count={goal.visitors}
|
2021-07-08 09:42:30 +03:00
|
|
|
all={this.state.goals}
|
|
|
|
bg="bg-red-50 dark:bg-gray-500 dark:bg-opacity-15"
|
|
|
|
maxWidthDeduction={this.getBarMaxWidth()}
|
|
|
|
>
|
2020-10-28 12:09:04 +03:00
|
|
|
{this.renderGoalText(goal.name)}
|
2021-07-08 09:42:30 +03:00
|
|
|
</Bar>
|
2020-12-16 12:57:28 +03:00
|
|
|
<div className="dark:text-gray-200">
|
2021-11-04 15:20:39 +03:00
|
|
|
<span className="inline-block w-20 font-medium text-right">{numberFormatter(goal.visitors)}</span>
|
2021-07-08 09:42:30 +03:00
|
|
|
{viewport > MOBILE_UPPER_WIDTH && <span className="inline-block w-20 font-medium text-right">{numberFormatter(goal.total_count)}</span>}
|
2021-03-25 12:55:15 +03:00
|
|
|
<span className="inline-block w-20 font-medium text-right">{goal.conversion_rate}%</span>
|
2020-10-28 12:09:04 +03:00
|
|
|
</div>
|
2020-08-20 14:57:49 +03:00
|
|
|
</div>
|
2020-10-30 11:49:41 +03:00
|
|
|
{ renderProps && <PropBreakdown site={this.props.site} query={this.props.query} goal={goal} /> }
|
2020-02-10 16:17:00 +03:00
|
|
|
</div>
|
2019-11-19 07:30:42 +03:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-03-25 12:55:15 +03:00
|
|
|
renderInner() {
|
2021-07-08 09:42:30 +03:00
|
|
|
const { viewport } = this.state;
|
2019-11-19 07:30:42 +03:00
|
|
|
if (this.state.loading) {
|
2021-03-25 12:55:15 +03:00
|
|
|
return <div className="mx-auto my-2 loading"><div></div></div>
|
2019-11-19 07:30:42 +03:00
|
|
|
} else if (this.state.goals) {
|
|
|
|
return (
|
2021-03-25 12:55:15 +03:00
|
|
|
<React.Fragment>
|
2020-12-16 12:57:28 +03:00
|
|
|
<h3 className="font-bold dark:text-gray-100">{this.props.title || "Goal Conversions"}</h3>
|
2021-03-25 12:55:15 +03:00
|
|
|
<div className="flex items-center justify-between mt-3 mb-2 text-xs font-bold tracking-wide text-gray-500 dark:text-gray-400">
|
2020-02-10 16:17:00 +03:00
|
|
|
<span>Goal</span>
|
2020-08-20 14:57:49 +03:00
|
|
|
<div className="text-right">
|
|
|
|
<span className="inline-block w-20">Uniques</span>
|
2021-07-08 09:42:30 +03:00
|
|
|
{viewport > MOBILE_UPPER_WIDTH && <span className="inline-block w-20">Total</span>}
|
2020-10-30 12:26:16 +03:00
|
|
|
<span className="inline-block w-20">CR</span>
|
2020-08-20 14:57:49 +03:00
|
|
|
</div>
|
2019-11-19 07:30:42 +03:00
|
|
|
</div>
|
|
|
|
|
2020-02-10 16:17:00 +03:00
|
|
|
{ this.state.goals.map(this.renderGoal.bind(this)) }
|
2021-03-25 12:55:15 +03:00
|
|
|
</React.Fragment>
|
2019-11-19 07:30:42 +03:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2021-03-25 12:55:15 +03:00
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<LazyLoader className="w-full p-4 bg-white rounded shadow-xl dark:bg-gray-825" style={{minHeight: '94px'}} onVisible={this.onVisible}>
|
|
|
|
{ this.renderInner() }
|
|
|
|
</LazyLoader>
|
|
|
|
)
|
|
|
|
}
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|