import React from 'react'; import { Link } from 'react-router-dom' import Bar from '../bar' import PropBreakdown from './prop-breakdown' import numberFormatter from '../../number-formatter' import * as api from '../../api' import * as url from '../../url' import LazyLoader from '../../lazy-loader' const MOBILE_UPPER_WIDTH = 767 const DEFAULT_WIDTH = 1080 export default class Conversions extends React.Component { constructor(props) { super(props) this.state = { loading: true, viewport: DEFAULT_WIDTH, } this.onVisible = this.onVisible.bind(this) 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 }); } onVisible() { this.fetchConversions() } componentDidUpdate(prevProps) { if (this.props.query !== prevProps.query) { this.setState({loading: true, goals: null}) this.fetchConversions() } } getBarMaxWidth() { const { viewport } = this.state; return viewport > MOBILE_UPPER_WIDTH ? "16rem" : "10rem"; } fetchConversions() { api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/conversions`, this.props.query) .then((res) => this.setState({loading: false, goals: res})) } renderGoalText(goalName) { if (this.props.query.period === 'realtime') { return {goalName} } else { return ( {goalName} ) } } renderGoal(goal) { const { viewport } = this.state; const renderProps = this.props.query.filters['goal'] == goal.name && goal.prop_names return (
{this.renderGoalText(goal.name)}
{numberFormatter(goal.unique_conversions)} {viewport > MOBILE_UPPER_WIDTH && {numberFormatter(goal.total_conversions)}} {goal.conversion_rate}%
{ renderProps && }
) } renderInner() { const { viewport } = this.state; if (this.state.loading) { return
} else if (this.state.goals) { return (

{this.props.title || "Goal Conversions"}

Goal
Uniques {viewport > MOBILE_UPPER_WIDTH && Total} CR
{ this.state.goals.map(this.renderGoal.bind(this)) }
) } } render() { return ( { this.renderInner() } ) } }