From 9b5cdbe68ff846028b1307100b2cdfed37fc9618 Mon Sep 17 00:00:00 2001 From: Adam Rutkowski Date: Thu, 11 May 2023 11:11:42 +0200 Subject: [PATCH] Stop doing maths in react --- .../js/dashboard/stats/behaviours/funnel.js | 22 +++++-------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/assets/js/dashboard/stats/behaviours/funnel.js b/assets/js/dashboard/stats/behaviours/funnel.js index ce00ed667..a6a383d05 100644 --- a/assets/js/dashboard/stats/behaviours/funnel.js +++ b/assets/js/dashboard/stats/behaviours/funnel.js @@ -36,9 +36,8 @@ export default class Funnel extends React.Component { formatDataLabel(visitors, ctx) { if (ctx.dataset.label === 'Visitors') { - const total = this.state.funnel.steps[0].visitors - const percentage = (visitors / total) * 100 - return `${percentage}%\n${numberFormatter(visitors)} Visitors` + const conversionRate = this.state.funnel.steps[ctx.dataIndex].conversion_rate + return `${conversionRate}%\n${numberFormatter(visitors)} Visitors` } else { return null } @@ -70,16 +69,8 @@ export default class Funnel extends React.Component { } const labels = this.state.funnel.steps.map((step, i) => `${i + 1}. ${step.label}`); const stepData = this.state.funnel.steps.map(step => step.visitors) - const dropOffData = this.state.funnel.steps.map((step, i) => { - const thisVisitors = step.visitors - const prevEntry = this.state.funnel.steps[i - 1] - if (prevEntry) { - return prevEntry.visitors - thisVisitors - } else { - return 0 - } - }) + const dropOffData = this.state.funnel.steps.map((step) => step.dropoff) const ctx = document.getElementById('funnel').getContext('2d') var gradient = ctx.createLinearGradient(0, 0, 0, 300); @@ -148,6 +139,7 @@ export default class Funnel extends React.Component { } renderError() { + if (this.state.error.payload && this.state.error.payload.level === "normal") { return ( {this.header()} @@ -174,15 +166,13 @@ export default class Funnel extends React.Component { } else if (this.state.error) { return this.renderError() } else if (this.state.funnel) { - const firstStep = this.state.funnel.steps[0].visitors - const lastStep = this.state.funnel.steps[this.state.funnel.steps.length - 1].visitors - const conversionRate = (lastStep / firstStep) * 100 + const conversionRate = this.state.funnel.steps[this.state.funnel.steps.length - 1].conversion_rate return ( {this.header()}

{this.state.funnel.steps.length}-step funnel • {conversionRate}% conversion rate

- +
) }