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'
|
2023-01-02 18:42:57 +03:00
|
|
|
import FlipMove from 'react-flip-move'
|
|
|
|
|
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'
|
2021-12-03 14:59:32 +03:00
|
|
|
import numberFormatter from '../../util/number-formatter'
|
2020-10-28 12:09:04 +03:00
|
|
|
import * as api from '../../api'
|
2021-12-03 14:59:32 +03:00
|
|
|
import * as url from '../../util/url'
|
|
|
|
import LazyLoader from '../../components/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-11-26 17:00:34 +03:00
|
|
|
this.htmlNode = React.createRef()
|
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)
|
2023-01-02 18:42:57 +03:00
|
|
|
this.fetchConversions = this.fetchConversions.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);
|
2023-01-02 18:42:57 +03:00
|
|
|
document.removeEventListener('tick', this.fetchConversions)
|
2021-07-08 09:42:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
2023-01-02 18:42:57 +03:00
|
|
|
if (this.props.query.period === 'realtime') {
|
|
|
|
document.addEventListener('tick', this.fetchConversions)
|
|
|
|
}
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate(prevProps) {
|
|
|
|
if (this.props.query !== prevProps.query) {
|
2021-11-26 17:00:34 +03:00
|
|
|
const height = this.htmlNode.current.element.offsetHeight
|
|
|
|
this.setState({loading: true, goals: null, prevHeight: height})
|
2019-11-19 07:30:42 +03:00
|
|
|
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)
|
2021-11-26 17:00:34 +03:00
|
|
|
.then((res) => this.setState({loading: false, goals: res, prevHeight: null}))
|
2019-11-19 07:30:42 +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-05 16:18:59 +03:00
|
|
|
count={goal.unique_conversions}
|
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()}
|
2021-11-05 16:18:59 +03:00
|
|
|
plot="unique_conversions"
|
2021-07-08 09:42:30 +03:00
|
|
|
>
|
2022-07-21 14:00:42 +03:00
|
|
|
<Link to={url.setQuery('goal', goal.name)} className="block px-2 py-1.5 hover:underline relative z-9 break-all lg:truncate dark:text-gray-200">{goal.name}</Link>
|
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-05 16:18:59 +03:00
|
|
|
<span className="inline-block w-20 font-medium text-right">{numberFormatter(goal.unique_conversions)}</span>
|
|
|
|
{viewport > MOBILE_UPPER_WIDTH && <span className="inline-block w-20 font-medium text-right">{numberFormatter(goal.total_conversions)}</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>
|
2023-01-02 18:42:57 +03:00
|
|
|
<FlipMove>
|
|
|
|
{ this.state.goals.map(this.renderGoal.bind(this)) }
|
|
|
|
</FlipMove>
|
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 (
|
2021-11-26 17:00:34 +03:00
|
|
|
<LazyLoader className="w-full p-4 bg-white rounded shadow-xl dark:bg-gray-825" style={{minHeight: '132px', height: this.state.prevHeight ?? 'auto'}} onVisible={this.onVisible} ref={this.htmlNode}>
|
2021-03-25 12:55:15 +03:00
|
|
|
{ this.renderInner() }
|
|
|
|
</LazyLoader>
|
|
|
|
)
|
|
|
|
}
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|