Add funnel picker

pull/2859/head
Uku Taht 4 years ago committed by Adam Rutkowski
parent 964718dbe2
commit aaf1863fc6

@ -9,7 +9,7 @@ import Sources from './stats/sources'
import Pages from './stats/pages'
import Locations from './stats/locations';
import Devices from './stats/devices'
import Funnel from './stats/funnel'
import Behaviours from './stats/behaviours'
import ComparisonInput from './comparison-input'
import { withPinnedHeader } from './pinned-header-hoc';
@ -20,7 +20,7 @@ function Historical(props) {
if (props.site.hasGoals) {
return (
<div className="items-start justify-between block w-full mt-6 md:flex">
<Funnel site={props.site} query={props.query} / >
<Behaviours site={props.site} query={props.query} / >
</div>
)
}

@ -16,6 +16,7 @@ if (container) {
domain: container.dataset.domain,
offset: container.dataset.offset,
hasGoals: container.dataset.hasGoals === 'true',
funnels: JSON.parse(container.dataset.funnels),
statsBegin: container.dataset.statsBegin,
nativeStatsBegin: container.dataset.nativeStatsBegin,
embedded: container.dataset.embedded,

@ -8,7 +8,7 @@ import Sources from './stats/sources'
import Pages from './stats/pages'
import Locations from './stats/locations'
import Devices from './stats/devices'
import Conversions from './stats/conversions'
import Behaviours from './stats/behaviours'
import { withPinnedHeader } from './pinned-header-hoc';
class Realtime extends React.Component {
@ -16,7 +16,7 @@ class Realtime extends React.Component {
if (this.props.site.hasGoals) {
return (
<div className="items-start justify-between block w-full mt-6 md:flex">
<Conversions site={this.props.site} query={this.props.query} title="Goal Conversions (last 30 min)" />
<Behaviours site={this.props.site} query={this.props.query} title="Goal Conversions (last 30 min)" />
</div>
)
}

@ -100,7 +100,6 @@ export default class Conversions extends React.Component {
} else if (this.state.goals) {
return (
<React.Fragment>
<h3 className="font-bold dark:text-gray-100">{this.props.title || "Goal Conversions"}</h3>
<div className="flex items-center justify-between mt-3 mb-2 text-xs font-bold tracking-wide text-gray-500 dark:text-gray-400">
<span>Goal</span>
<div className="text-right">
@ -119,7 +118,7 @@ export default class Conversions 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}>
<LazyLoader style={{minHeight: '132px', height: this.state.prevHeight ?? 'auto'}} onVisible={this.onVisible} ref={this.htmlNode}>
{ this.renderInner() }
</LazyLoader>
)

@ -1,10 +1,12 @@
import React from 'react';
import Chart from 'chart.js/auto';
import ChartDataLabels from 'chartjs-plugin-datalabels';
Chart.register(ChartDataLabels);
import * as api from '../../api'
import LazyLoader from '../../components/lazy-loader'
import * as api from '../api'
import LazyLoader from '../components/lazy-loader'
const FUNNEL_ID = 5
export default class Funnel extends React.Component {
constructor(props) {
@ -19,22 +21,31 @@ export default class Funnel extends React.Component {
}
componentDidUpdate(prevProps) {
if (this.props.query !== prevProps.query) {
const queryChanged = this.props.query !== prevProps.query
const funnelChanged = this.props.funnel !== prevProps.funnel
if (queryChanged || funnelChanged) {
this.setState({loading: true})
this.fetchFunnel()
}
}
formatDataLabel(visitors) {
return `${visitors} Visitors`
}
fetchFunnel() {
api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/funnels/${FUNNEL_ID}`, this.props.query)
api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/funnels/${this.props.funnel.id}`, this.props.query)
.then((res) => this.setState({ loading: false, funnel: res }))
.then(this.initialiseChart.bind(this))
}
initialiseChart() {
console.info(this.state.funnel);
if (this.chart) {
this.chart.destroy()
}
const labels = this.state.funnel.steps.map(step => step.label);
const stepData = this.state.funnel.steps.map(step => step.visitors);
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);
@ -44,24 +55,30 @@ export default class Funnel extends React.Component {
const data = {
labels: labels,
datasets: [{
label: 'My First Dataset',
label: 'Visitors',
data: stepData,
backgroundColor: [gradient],
borderColor: ['rgba(101,116,205)'],
borderWidth: 3,
borderWidth: 1,
borderSkipped: false,
borderRadius: 4
}]
};
const config = {
plugins: [ChartDataLabels],
type: 'bar',
data: data,
options: {
responsive: true,
barThickness: 120,
plugins: {
legend: {
display: false,
},
datalabels: {
formatter: this.formatDataLabel.bind(this)
}
},
scales: {
y: { display: false, border: { display: false } },
@ -70,7 +87,7 @@ export default class Funnel extends React.Component {
},
};
new Chart(ctx, config);
this.chart = new Chart(ctx, config);
}
renderInner() {
@ -88,7 +105,7 @@ 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}>
<LazyLoader onVisible={this.onVisible} ref={this.htmlNode}>
{this.renderInner()}
</LazyLoader>
)

@ -0,0 +1,76 @@
import React, { Fragment, useState } from 'react'
import { Menu, Transition } from '@headlessui/react'
import { ChevronDownIcon } from '@heroicons/react/20/solid'
import classNames from 'classnames'
import Funnel from './funnel'
import Conversions from './conversions'
function Tabs({tab, setTab, funnelNames}) {
const activeClass = 'inline-block h-5 text-indigo-700 dark:text-indigo-500 font-bold active-prop-heading truncate text-left'
const defaultClass = 'hover:text-indigo-600 cursor-pointer truncate text-left'
return (
<div className="flex text-xs font-medium text-gray-500 dark:text-gray-400 space-x-2">
<div className={classNames({[activeClass]: tab == 'conversions', [defaultClass]: tab !== 'conversions'})} onClick={() => setTab('conversions')}>Conversions</div>
<Menu as="div" className="relative inline-block text-left">
<div>
<Menu.Button className="inline-flex justify-between focus:outline-none">
<span className={funnelNames.includes(tab) ? activeClass : defaultClass}>Funnels</span>
<ChevronDownIcon className="-mr-1 ml-1 h-4 w-4" aria-hidden="true" />
</Menu.Button>
</div>
<Transition
as={Fragment}
enter="transition ease-out duration-100"
enterFrom="transform opacity-0 scale-95"
enterTo="transform opacity-100 scale-100"
leave="transition ease-in duration-75"
leaveFrom="transform opacity-100 scale-100"
leaveTo="transform opacity-0 scale-95"
>
<Menu.Items className="text-left origin-top-right absolute right-0 mt-2 w-56 rounded-md shadow-lg bg-white dark:bg-gray-800 ring-1 ring-black ring-opacity-5 focus:outline-none z-10">
<div className="py-1">
{funnelNames.map((option) => {
return (
<Menu.Item key={option}>
{({ active }) => (
<span
onClick={() => setTab(option)}
className={classNames(
active ? 'bg-gray-100 dark:bg-gray-900 text-gray-900 dark:text-gray-200 cursor-pointer' : 'text-gray-700 dark:text-gray-200',
'block px-4 py-2 text-sm',
tab === option ? 'font-bold' : ''
)}
>
{option}
</span>
)}
</Menu.Item>
)
})}
</div>
</Menu.Items>
</Transition>
</Menu>
</div>
)
}
export default function Behaviours({ query, site }) {
const [tab, setTab] = useState('Signup funnel')
const funnelNames = site.funnels.map(({name}) => name)
const selectedFunnel = site.funnels.find(funnel => funnel.name === tab)
return (
<div className="w-full p-4 bg-white rounded shadow-xl dark:bg-gray-825">
<div className="flex justify-between w-full">
<h3 className="font-bold dark:text-gray-100">Behaviors</h3>
<Tabs tab={tab} setTab={setTab} funnelNames={funnelNames} />
</div>
{ tab == 'conversions' && <Conversions query={query} site={site} /> }
{ funnelNames.includes(tab) && <Funnel funnel={selectedFunnel} query={query} site={site} /> }
</div>
)
}

@ -25,6 +25,7 @@
"autoprefixer": "^10.2.6",
"babel-loader": "^8.2.2",
"chart.js": "^3.3.2",
"chartjs-plugin-datalabels": "^2.2.0",
"classnames": "^2.3.1",
"copy-webpack-plugin": "^9.0.0",
"css-loader": "^5.2.6",
@ -2848,6 +2849,14 @@
"version": "3.3.2",
"license": "MIT"
},
"node_modules/chartjs-plugin-datalabels": {
"version": "2.2.0",
"resolved": "https://registry.npmjs.org/chartjs-plugin-datalabels/-/chartjs-plugin-datalabels-2.2.0.tgz",
"integrity": "sha512-14ZU30lH7n89oq+A4bWaJPnAG8a7ZTk7dKf48YAzMvJjQtjrgg5Dpk9f+LbjCF6bpx3RAGTeL13IXpKQYyRvlw==",
"peerDependencies": {
"chart.js": ">=3.0.0"
}
},
"node_modules/chokidar": {
"version": "3.5.3",
"resolved": "https://registry.npmjs.org/chokidar/-/chokidar-3.5.3.tgz",
@ -10851,6 +10860,12 @@
"chart.js": {
"version": "3.3.2"
},
"chartjs-plugin-datalabels": {
"version": "2.2.0",
"resolved": "https://registry.npmjs.org/chartjs-plugin-datalabels/-/chartjs-plugin-datalabels-2.2.0.tgz",
"integrity": "sha512-14ZU30lH7n89oq+A4bWaJPnAG8a7ZTk7dKf48YAzMvJjQtjrgg5Dpk9f+LbjCF6bpx3RAGTeL13IXpKQYyRvlw==",
"requires": {}
},
"chokidar": {
"version": "3.5.3",
"resolved": "https://registry.npmjs.org/chokidar/-/chokidar-3.5.3.tgz",

@ -27,6 +27,7 @@
"autoprefixer": "^10.2.6",
"babel-loader": "^8.2.2",
"chart.js": "^3.3.2",
"chartjs-plugin-datalabels": "^2.2.0",
"classnames": "^2.3.1",
"copy-webpack-plugin": "^9.0.0",
"css-loader": "^5.2.6",

Loading…
Cancel
Save