2019-11-19 07:30:42 +03:00
|
|
|
import React from 'react';
|
|
|
|
import { withRouter } from 'react-router-dom'
|
2019-11-19 07:43:28 +03:00
|
|
|
import Chart from 'chart.js'
|
2020-03-03 12:13:08 +03:00
|
|
|
import FadeIn from '../fade-in'
|
2019-11-20 08:48:27 +03:00
|
|
|
import { eventName } from '../query'
|
2019-11-19 07:30:42 +03:00
|
|
|
import numberFormatter from '../number-formatter'
|
|
|
|
import * as api from '../api'
|
|
|
|
|
2019-11-25 06:37:50 +03:00
|
|
|
function mainSet(plot, present_index, ctx) {
|
2019-11-19 07:30:42 +03:00
|
|
|
var gradient = ctx.createLinearGradient(0, 0, 0, 300);
|
|
|
|
gradient.addColorStop(0, 'rgba(101,116,205, 0.2)');
|
|
|
|
gradient.addColorStop(1, 'rgba(101,116,205, 0)');
|
|
|
|
|
2019-11-25 06:37:50 +03:00
|
|
|
if (present_index) {
|
|
|
|
var dashedPart = plot.slice(present_index - 1);
|
|
|
|
var dashedPlot = (new Array(plot.length - dashedPart.length)).concat(dashedPart)
|
|
|
|
for(var i = present_index; i < plot.length; i++) {
|
|
|
|
plot[i] = undefined
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return [{
|
|
|
|
label: 'Visitors',
|
2019-11-25 06:37:50 +03:00
|
|
|
data: plot,
|
2019-11-19 07:30:42 +03:00
|
|
|
borderWidth: 3,
|
|
|
|
borderColor: 'rgba(101,116,205)',
|
|
|
|
pointBackgroundColor: 'rgba(101,116,205)',
|
|
|
|
backgroundColor: gradient,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'Visitors',
|
|
|
|
data: dashedPlot,
|
|
|
|
borderWidth: 3,
|
|
|
|
borderDash: [5, 10],
|
|
|
|
borderColor: 'rgba(101,116,205)',
|
|
|
|
pointBackgroundColor: 'rgba(101,116,205)',
|
|
|
|
backgroundColor: gradient,
|
|
|
|
}]
|
|
|
|
} else {
|
|
|
|
return [{
|
|
|
|
label: 'Visitors',
|
2019-11-25 06:37:50 +03:00
|
|
|
data: plot,
|
2019-11-19 07:30:42 +03:00
|
|
|
borderWidth: 3,
|
|
|
|
borderColor: 'rgba(101,116,205)',
|
|
|
|
pointBackgroundColor: 'rgba(101,116,205)',
|
|
|
|
backgroundColor: gradient,
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-25 06:37:50 +03:00
|
|
|
function compareSet(plot, present_index, ctx) {
|
|
|
|
var gradient = ctx.createLinearGradient(0, 0, 0, 300);
|
|
|
|
gradient.addColorStop(0, 'rgba(255, 68, 87, .2)');
|
|
|
|
gradient.addColorStop(1, 'rgba(255, 68, 87, 0)');
|
|
|
|
|
|
|
|
if (present_index) {
|
|
|
|
var dashedPart = plot.slice(present_index - 1);
|
|
|
|
var dashedPlot = (new Array(plot.length - dashedPart.length)).concat(dashedPart)
|
|
|
|
for(var i = present_index; i < plot.length; i++) {
|
|
|
|
plot[i] = undefined
|
|
|
|
}
|
|
|
|
|
|
|
|
return [{
|
|
|
|
label: 'Conversions',
|
|
|
|
data: plot,
|
|
|
|
borderWidth: 3,
|
|
|
|
borderColor: 'rgba(255, 68, 87, 1)',
|
|
|
|
pointBackgroundColor: 'rgba(255, 68, 87, 1)',
|
|
|
|
backgroundColor: gradient,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'Conversions',
|
|
|
|
data: dashedPlot,
|
|
|
|
borderWidth: 3,
|
|
|
|
borderDash: [5, 10],
|
|
|
|
borderColor: 'rgba(255, 68, 87, 1)',
|
|
|
|
pointBackgroundColor: 'rgba(255, 68, 87, 1)',
|
|
|
|
backgroundColor: gradient,
|
|
|
|
}]
|
|
|
|
} else {
|
|
|
|
return [{
|
|
|
|
label: 'Conversions',
|
|
|
|
data: plot,
|
|
|
|
borderWidth: 3,
|
2019-11-26 06:48:27 +03:00
|
|
|
borderColor: 'rgba(255, 68, 87, 1)',
|
|
|
|
pointBackgroundColor: 'rgba(255, 68, 87, 1)',
|
2019-11-25 06:37:50 +03:00
|
|
|
backgroundColor: gradient,
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function dataSets(graphData, ctx) {
|
|
|
|
const dataSets = mainSet(graphData.plot, graphData.present_index, ctx)
|
|
|
|
|
|
|
|
if (graphData.compare_plot) {
|
|
|
|
return dataSets.concat(compareSet(graphData.compare_plot, graphData.present_index, ctx))
|
|
|
|
} else {
|
|
|
|
return dataSets
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-19 07:30:42 +03:00
|
|
|
const MONTHS = [
|
|
|
|
"January", "February", "March",
|
|
|
|
"April", "May", "June", "July",
|
|
|
|
"August", "September", "October",
|
|
|
|
"November", "December"
|
|
|
|
]
|
|
|
|
|
|
|
|
function dateFormatter(graphData) {
|
|
|
|
return function(isoDate) {
|
|
|
|
const date = new Date(isoDate)
|
|
|
|
|
|
|
|
if (graphData.interval === 'month') {
|
|
|
|
return MONTHS[date.getUTCMonth()];
|
|
|
|
} else if (graphData.interval === 'date') {
|
|
|
|
return date.getUTCDate() + ' ' + MONTHS[date.getUTCMonth()];
|
|
|
|
} else if (graphData.interval === 'hour') {
|
|
|
|
var hours = date.getHours(); // Not sure why getUTCHours doesn't work here
|
|
|
|
var ampm = hours >= 12 ? 'pm' : 'am';
|
|
|
|
hours = hours % 12;
|
|
|
|
hours = hours ? hours : 12; // the hour '0' should be '12'
|
|
|
|
return hours + ampm;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class LineGraph extends React.Component {
|
|
|
|
componentDidMount() {
|
|
|
|
const {graphData} = this.props
|
|
|
|
const ctx = document.getElementById("main-graph-canvas").getContext('2d');
|
|
|
|
|
|
|
|
this.chart = new Chart(ctx, {
|
|
|
|
type: 'line',
|
|
|
|
data: {
|
|
|
|
labels: graphData.labels,
|
|
|
|
datasets: dataSets(graphData, ctx)
|
|
|
|
},
|
|
|
|
options: {
|
|
|
|
animation: false,
|
|
|
|
legend: {display: false},
|
|
|
|
responsive: true,
|
2019-11-25 06:37:50 +03:00
|
|
|
elements: {line: {tension: 0}, point: {radius: 0}},
|
2019-11-19 07:30:42 +03:00
|
|
|
onClick: this.onClick.bind(this),
|
|
|
|
tooltips: {
|
|
|
|
mode: 'index',
|
|
|
|
intersect: false,
|
|
|
|
xPadding: 10,
|
|
|
|
yPadding: 10,
|
2019-11-25 06:37:50 +03:00
|
|
|
titleFontSize: 18,
|
2019-11-19 07:30:42 +03:00
|
|
|
footerFontSize: 14,
|
2019-11-25 06:37:50 +03:00
|
|
|
bodyFontSize: 14,
|
2019-11-19 07:30:42 +03:00
|
|
|
backgroundColor: 'rgba(25, 30, 56)',
|
2019-11-25 06:37:50 +03:00
|
|
|
titleMarginBottom: 8,
|
|
|
|
bodySpacing: 6,
|
|
|
|
footerMarginTop: 8,
|
|
|
|
xPadding: 16,
|
|
|
|
yPadding: 12,
|
|
|
|
multiKeyBackground: 'none',
|
2019-11-19 07:30:42 +03:00
|
|
|
callbacks: {
|
|
|
|
title: function(dataPoints) {
|
2019-11-20 12:06:28 +03:00
|
|
|
const data = dataPoints[0]
|
2019-11-25 06:37:50 +03:00
|
|
|
return dateFormatter(graphData)(data.xLabel)
|
|
|
|
},
|
|
|
|
beforeBody: function() {
|
|
|
|
this.drawnLabels = {}
|
|
|
|
},
|
|
|
|
label: function(item) {
|
|
|
|
const dataset = this._data.datasets[item.datasetIndex]
|
|
|
|
if (!this.drawnLabels[dataset.label]) {
|
|
|
|
this.drawnLabels[dataset.label] = true
|
|
|
|
return ` ${item.yLabel} ${dataset.label}`
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|
|
|
|
},
|
2019-11-25 06:37:50 +03:00
|
|
|
footer: function(dataPoints) {
|
2019-11-19 07:30:42 +03:00
|
|
|
if (graphData.interval === 'month') {
|
|
|
|
return 'Click to view month'
|
|
|
|
} else if (graphData.interval === 'date') {
|
|
|
|
return 'Click to view day'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
scales: {
|
|
|
|
yAxes: [{
|
|
|
|
ticks: {
|
|
|
|
callback: numberFormatter,
|
|
|
|
beginAtZero: true,
|
|
|
|
autoSkip: true,
|
|
|
|
maxTicksLimit: 8,
|
|
|
|
},
|
|
|
|
gridLines: {
|
|
|
|
zeroLineColor: 'transparent',
|
|
|
|
drawBorder: false,
|
|
|
|
}
|
|
|
|
}],
|
|
|
|
xAxes: [{
|
|
|
|
gridLines: {
|
|
|
|
display: false,
|
|
|
|
},
|
|
|
|
ticks: {
|
|
|
|
autoSkip: true,
|
|
|
|
maxTicksLimit: 8,
|
|
|
|
callback: dateFormatter(graphData),
|
|
|
|
}
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
onClick(e) {
|
2019-11-22 10:38:58 +03:00
|
|
|
const query = new URLSearchParams(window.location.search)
|
2019-11-19 07:30:42 +03:00
|
|
|
const element = this.chart.getElementsAtEventForMode(e, 'index', {intersect: false})[0]
|
|
|
|
const date = element._chart.config.data.labels[element._index]
|
|
|
|
if (this.props.graphData.interval === 'month') {
|
2019-11-22 10:38:58 +03:00
|
|
|
query.set('period', 'month')
|
|
|
|
query.set('date', date)
|
|
|
|
this.props.history.push({search: query.toString()})
|
2019-11-19 07:30:42 +03:00
|
|
|
} else if (this.props.graphData.interval === 'date') {
|
2019-11-22 10:38:58 +03:00
|
|
|
query.set('period', 'day')
|
|
|
|
query.set('date', date)
|
|
|
|
this.props.history.push({search: query.toString()})
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-20 06:22:37 +03:00
|
|
|
renderComparison(name, comparison) {
|
2019-11-19 07:30:42 +03:00
|
|
|
const formattedComparison = numberFormatter(Math.abs(comparison))
|
|
|
|
|
|
|
|
if (comparison > 0) {
|
2020-03-06 12:11:38 +03:00
|
|
|
const color = name === 'Bounce rate' ? 'text-red-400' : 'text-green-500'
|
2020-02-10 16:17:00 +03:00
|
|
|
return <span className="text-xs"><span className={color + ' font-bold'}>↑</span> {formattedComparison}%</span>
|
2019-11-19 07:30:42 +03:00
|
|
|
} else if (comparison < 0) {
|
2020-03-06 12:11:38 +03:00
|
|
|
const color = name === 'Bounce rate' ? 'text-green-500' : 'text-red-400'
|
2020-02-10 16:17:00 +03:00
|
|
|
return <span className="text-xs"><span className={color + ' font-bold'}>↓</span> {formattedComparison}%</span>
|
2019-11-19 07:30:42 +03:00
|
|
|
} else if (comparison === 0) {
|
2020-03-06 12:11:38 +03:00
|
|
|
return <span className="text-xs text-gray-700">〰 N/A</span>
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-25 12:17:18 +03:00
|
|
|
renderTopStats() {
|
|
|
|
const {graphData} = this.props
|
|
|
|
return this.props.graphData.top_stats.map((stat, index) => {
|
2020-03-06 12:11:38 +03:00
|
|
|
let border = index > 0 ? 'lg:border-l border-gray-300' : ''
|
2019-12-20 06:22:37 +03:00
|
|
|
border = index % 2 === 0 ? border + ' border-r lg:border-r-0' : border
|
2019-11-25 12:17:18 +03:00
|
|
|
|
2019-11-22 11:37:44 +03:00
|
|
|
return (
|
2020-02-10 16:17:00 +03:00
|
|
|
<div className={`px-8 w-1/2 my-4 lg:w-auto ${border}`} key={stat.name}>
|
2020-03-26 16:43:55 +03:00
|
|
|
<div className="text-gray-500 text-xs font-bold tracking-wide uppercase">{stat.name}</div>
|
2020-02-10 16:17:00 +03:00
|
|
|
<div className="my-1 flex justify-between items-center">
|
|
|
|
<b className="text-2xl mr-4">{ typeof(stat.count) == 'number' ? numberFormatter(stat.count) : stat.percentage + '%' }</b>
|
|
|
|
{this.renderComparison(stat.name, stat.change)}
|
2019-11-22 11:37:44 +03:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)
|
2019-11-25 12:17:18 +03:00
|
|
|
})
|
2019-11-22 11:37:44 +03:00
|
|
|
}
|
|
|
|
|
2020-01-13 16:16:35 +03:00
|
|
|
downloadLink() {
|
2020-02-04 16:44:13 +03:00
|
|
|
const endpoint = `/${encodeURIComponent(this.props.site.domain)}/visitors.csv${api.serializeQuery(this.props.query)}`
|
2020-01-13 16:16:35 +03:00
|
|
|
|
|
|
|
return (
|
|
|
|
<a href={endpoint} download>
|
2020-03-06 12:11:38 +03:00
|
|
|
<svg className="w-4 h-5 absolute text-gray-700" style={{right: '2rem', top: '-2rem'}}>
|
2020-01-13 16:16:35 +03:00
|
|
|
<use xlinkHref="#feather-download" />
|
|
|
|
</svg>
|
|
|
|
</a>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-11-19 07:30:42 +03:00
|
|
|
render() {
|
2019-11-25 12:17:18 +03:00
|
|
|
const extraClass = this.props.graphData.interval === 'hour' ? '' : 'cursor-pointer'
|
2019-11-19 07:30:42 +03:00
|
|
|
|
|
|
|
return (
|
|
|
|
<React.Fragment>
|
2020-01-13 16:16:35 +03:00
|
|
|
<div className="flex flex-wrap">
|
2019-11-25 12:17:18 +03:00
|
|
|
{ this.renderTopStats() }
|
2019-11-19 07:30:42 +03:00
|
|
|
</div>
|
2020-01-13 16:16:35 +03:00
|
|
|
<div className="px-2 relative">
|
|
|
|
{ this.downloadLink() }
|
2019-11-19 07:30:42 +03:00
|
|
|
<canvas id="main-graph-canvas" className={'mt-4 ' + extraClass} width="1054" height="342"></canvas>
|
|
|
|
</div>
|
|
|
|
</React.Fragment>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LineGraph = withRouter(LineGraph)
|
|
|
|
|
|
|
|
export default class VisitorGraph extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props)
|
|
|
|
this.state = {loading: true}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
this.fetchGraphData()
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate(prevProps) {
|
|
|
|
if (this.props.query !== prevProps.query) {
|
|
|
|
this.setState({loading: true, graphData: null})
|
|
|
|
this.fetchGraphData()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fetchGraphData() {
|
2020-02-04 16:44:13 +03:00
|
|
|
api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/main-graph`, this.props.query)
|
2019-11-19 07:30:42 +03:00
|
|
|
.then((res) => {
|
|
|
|
this.setState({loading: false, graphData: res})
|
|
|
|
return res
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
renderInner() {
|
2020-03-03 12:13:08 +03:00
|
|
|
if (this.state.graphData) {
|
2019-11-19 07:30:42 +03:00
|
|
|
return (
|
2020-03-03 12:13:08 +03:00
|
|
|
<LineGraph graphData={this.state.graphData} site={this.props.site} query={this.props.query} />
|
2019-11-19 07:30:42 +03:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
2020-02-10 16:17:00 +03:00
|
|
|
<div className="w-full bg-white shadow-xl rounded mt-6 main-graph">
|
2020-03-03 12:13:08 +03:00
|
|
|
{ this.state.loading && <div className="loading pt-24 sm:pt-32 md:pt-48 mx-auto"><div></div></div> }
|
|
|
|
<FadeIn show={!this.state.loading}>
|
|
|
|
{ this.renderInner() }
|
|
|
|
</FadeIn>
|
2019-11-19 07:30:42 +03:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|