Fetch funnel data from the endpoint

pull/2859/head
Adam Rutkowski 4 years ago
parent 0748a8aa2b
commit ca76a6d871

@ -10,7 +10,7 @@ const FUNNEL_ID = 1
export default class Funnel extends React.Component {
constructor(props) {
super(props)
this.state = {loading: true}
this.state = { loading: true }
this.onVisible = this.onVisible.bind(this)
this.fetchFunnel = this.fetchFunnel.bind(this)
}
@ -25,14 +25,17 @@ export default class Funnel extends React.Component {
}
}
fetchFunnel() {
api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/funnels/${FUNNEL_ID}`, this.props.query)
.then((res) => this.setState({loading: false, funnel: res}))
.then(this.initialiseChart)
.then((res) => this.setState({ loading: false, funnel: res }))
.then(this.initialiseChart.bind(this))
}
initialiseChart() {
console.info(this.state.funnel);
const labels = this.state.funnel.steps.map(step => step.label);
const stepData = this.state.funnel.steps.map(step => step.visitors);
const ctx = document.getElementById('funnel').getContext('2d')
var gradient = ctx.createLinearGradient(0, 0, 0, 300);
@ -40,10 +43,10 @@ export default class Funnel extends React.Component {
gradient.addColorStop(0, 'rgba(101,116,205, 0)');
const data = {
labels: ['1. Visit /blog/**', '2. Visit /register', '3. Signup', '4. Go to moon', '5. Turn it up to 11'],
labels: labels,
datasets: [{
label: 'My First Dataset',
data: [100, 80, 20, 4, 1],
data: stepData,
backgroundColor: [gradient],
borderColor: ['rgba(101,116,205)'],
borderWidth: 1,
@ -56,17 +59,14 @@ export default class Funnel extends React.Component {
data: data,
options: {
responsive: true,
layout: {
padding: {left: 0, right: 0}
},
plugins: {
legend: {
display: false,
},
},
scales: {
y: {display: false, border: {display: false}},
x: {display: true, border: {display: false}, grid: {drawOnChartArea: false, drawTicks: false}, ticks: {font: {weight: 'bold', size: 14}, color: 'rgb(75, 85, 99)'}}
y: { display: false, border: { display: false } },
x: { display: true, border: { display: false }, grid: { drawOnChartArea: false, drawTicks: false }, ticks: { font: { weight: 'bold', size: 14 }, color: 'rgb(75, 85, 99)' } }
}
},
};
@ -80,7 +80,7 @@ export default class Funnel extends React.Component {
} else if (this.state.funnel) {
return (
<React.Fragment>
<h3 className="font-bold dark:text-gray-100">Signup funnel</h3>
<h3 className="font-bold dark:text-gray-100">{this.state.funnel.name}</h3>
<canvas className="mt-8" id="funnel" height="100px"></canvas>
</React.Fragment>
)
@ -89,8 +89,8 @@ export default class Funnel extends React.Component {
render() {
return (
<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}>
{ this.renderInner() }
<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}>
{this.renderInner()}
</LazyLoader>
)
}

@ -3,6 +3,7 @@ defmodule PlausibleWeb.Api.FunnelsController do
def show(conn, _params) do
json(conn, %{
name: "Signup Funnel",
conversion_rate: "38%",
steps: [
%{

Loading…
Cancel
Save