analytics/assets/js/dashboard/stats/locations/map.js
RobertJoonas 47e21121db
Realtime dashboard improvements (#2445)
* add a new realtime-update-timer module

* hook to the new 'tick' event in ListReport for auto-updates

This commit fixes the bug where all reports using the `ListReport` component did not
auto-update in realtime mode. Those reports are:

- Pages (Top / Entry / Exit)
- Locations (Countries / Regions / Cities)
- Devices (Screen Sizes / Browsers + versions / OS-s + versions)

* fetch data for ListReports only when scrolled into view

* refactor fetching data in ListReport

* refer to one source of truth for utm tags

* make the 'All' tab in Sources auto-update

* make all UTM tabs in Sources auto-update

* fetch UTM data only when scrolled into view

* auto-update Referrers with the new timer

* auto-update google search terms

* auto-update Conversions

* make countries map auto-update

* auto-update visitor-graph and top stats with new timer

* use new tick event for current visitors (in Historical)

* remove the old timer class

* update changelog

* Visual improvements to automatic realtime updates (#2532)

* minor consistency fix for text color in dark mode

* use FlipMove in goal conversions report

* use FlipMove in ListReports

* set main graph and top stats loading state correctly

* refactor isIntervalValid function

* enforce intervals are valid when set and stored

* remove duplicate data fetching on interval change

Fetching new data is handled by the `fetchGraphData` callback in `updateInterval`

* refactor updateMetric function

* make it clearer why 'metric' can be a faulty value

* extract 'query' and 'site' variables from 'this.props'

* reset interval state only when period is changed

The 'maybeRollbackInterval' function was also used to fetch data. This commit replaces
all those function calls with 'fetchGraphData' which better describes the actual behavior.
We should only worry about rolling back the interval if 'query.period' has changed.

This commit also stops the graph from flickering when it is updated in realtime.

* update names of two variables

* remove unnecessary negation

* make collapsed graph state more explicit

* consider stored invalid intervals when graph mounts

* fix not showing loading spinner regression

* remove interval state from VisitorGraph (#2540)

* Realtime prop breakdown (#2535)

* disable load more in realtime mode

* extract doFetch function

* separate fetchPropBreakdown and fetchNextPage functions

* subscribe for auto-updates in realtime

* improve readability with function name changes
2023-01-02 17:42:57 +02:00

171 lines
5.3 KiB
JavaScript

import React from 'react';
import Datamap from 'datamaps'
import { withRouter } from 'react-router-dom'
import * as d3 from "d3"
import numberFormatter from '../../util/number-formatter'
import FadeIn from '../../fade-in'
import LazyLoader from '../../components/lazy-loader'
import MoreLink from '../more-link'
import * as api from '../../api'
import { navigateToQuery } from '../../query'
class Countries extends React.Component {
constructor(props) {
super(props)
this.resizeMap = this.resizeMap.bind(this)
this.drawMap = this.drawMap.bind(this)
this.getDataset = this.getDataset.bind(this)
this.state = {
loading: true,
darkTheme: document.querySelector('html').classList.contains('dark') || false
}
this.onVisible = this.onVisible.bind(this)
this.updateCountries = this.updateCountries.bind(this)
}
componentDidUpdate(prevProps) {
if (this.props.query !== prevProps.query) {
// eslint-disable-next-line react/no-did-update-set-state
this.setState({loading: true, countries: null})
this.fetchCountries().then(this.drawMap)
}
}
componentWillUnmount() {
window.removeEventListener('resize', this.resizeMap);
document.removeEventListener('tick', this.updateCountries);
}
onVisible() {
this.fetchCountries().then(this.drawMap.bind(this))
window.addEventListener('resize', this.resizeMap);
if (this.props.query.period === 'realtime') {
document.addEventListener('tick', this.updateCountries)
}
}
getDataset() {
const dataset = {};
var onlyValues = this.state.countries.map(function(obj){ return obj.visitors });
var maxValue = Math.max.apply(null, onlyValues);
// eslint-disable-next-line no-undef
const paletteScale = d3.scale.linear()
.domain([0,maxValue])
.range([
this.state.darkTheme ? "#2e3954" : "#f3ebff",
this.state.darkTheme ? "#6366f1" : "#a779e9"
])
this.state.countries.forEach(function(item){
dataset[item.alpha_3] = {numberOfThings: item.visitors, fillColor: paletteScale(item.visitors)};
});
return dataset
}
updateCountries() {
this.fetchCountries().then(() => {
this.map.updateChoropleth(this.getDataset(), {reset: true})
})
}
fetchCountries() {
return api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/countries`, this.props.query, {limit: 300})
.then((res) => this.setState({loading: false, countries: res}))
}
resizeMap() {
this.map && this.map.resize()
}
drawMap() {
const dataset = this.getDataset();
const label = this.props.query.period === 'realtime' ? 'Current visitors' : 'Visitors'
const defaultFill = this.state.darkTheme ? '#2d3747' : '#f8fafc'
const highlightFill = this.state.darkTheme ? '#374151' : '#F5F5F5'
const borderColor = this.state.darkTheme ? '#1f2937' : '#dae1e7'
const highlightBorderColor = this.state.darkTheme ? '#4f46e5' : '#a779e9'
this.map = new Datamap({
element: document.getElementById('map-container'),
responsive: true,
projection: 'mercator',
fills: { defaultFill },
data: dataset,
geographyConfig: {
borderColor,
highlightBorderWidth: 2,
highlightFillColor: (geo) => geo.fillColor || highlightFill,
highlightBorderColor,
popupTemplate: (geo, data) => {
if (!data) { return null; }
const pluralizedLabel = data.numberOfThings === 1 ? label.slice(0, -1) : label
return ['<div class="hoverinfo dark:bg-gray-800 dark:shadow-gray-850 dark:border-gray-850 dark:text-gray-200">',
'<strong>', geo.properties.name, ' </strong>',
'<br><strong class="dark:text-indigo-400">', numberFormatter(data.numberOfThings), '</strong> ', pluralizedLabel,
'</div>'].join('');
}
},
done: (datamap) => {
datamap.svg.selectAll('.datamaps-subunit').on('click', (geography) => {
const country = this.state.countries.find(c => c.alpha_3 === geography.id)
if (country) {
this.props.onClick()
navigateToQuery(
this.props.history,
this.props.query,
{
country: country.code,
country_name: country.name
}
)
}
})
}
});
}
geolocationDbNotice() {
if (this.props.site.isDbip) {
return (
<span className="text-xs text-gray-500 absolute bottom-4 right-3">IP Geolocation by <a target="_blank" href="https://db-ip.com" rel="noreferrer" className="text-indigo-600">DB-IP</a></span>
)
}
return null
}
renderBody() {
if (this.state.countries) {
return (
<>
<div className="mx-auto mt-4" style={{width: '100%', maxWidth: '475px', height: '335px'}} id="map-container"></div>
<MoreLink site={this.props.site} list={this.state.countries} endpoint="countries" />
{ this.geolocationDbNotice() }
</>
)
}
return null
}
render() {
return (
<LazyLoader onVisible={this.onVisible}>
{ this.state.loading && <div className="mx-auto my-32 loading"><div></div></div> }
<FadeIn show={!this.state.loading}>
{ this.renderBody() }
</FadeIn>
</LazyLoader>
)
}
}
export default withRouter(Countries)