2020-11-13 03:26:53 +03:00
|
|
|
import React from 'react'
|
2020-11-13 03:33:01 +03:00
|
|
|
import { Link, withRouter } from 'react-router-dom'
|
2020-09-09 11:13:55 +03:00
|
|
|
import {formatDay, formatMonthYYYY, nowForSite, parseUTCDate} from './date'
|
2019-11-19 07:30:42 +03:00
|
|
|
|
2020-09-09 11:13:55 +03:00
|
|
|
const PERIODS = ['realtime', 'day', 'month', '7d', '30d', '6mo', '12mo', 'custom']
|
2019-11-19 07:30:42 +03:00
|
|
|
|
|
|
|
export function parseQuery(querystring, site) {
|
2019-11-20 08:48:27 +03:00
|
|
|
const q = new URLSearchParams(querystring)
|
|
|
|
let period = q.get('period')
|
2019-11-19 07:30:42 +03:00
|
|
|
const periodKey = 'period__' + site.domain
|
|
|
|
|
|
|
|
if (PERIODS.includes(period)) {
|
2020-07-14 16:52:26 +03:00
|
|
|
if (period !== 'custom' && period !== 'realtime') window.localStorage[periodKey] = period
|
2019-11-19 07:30:42 +03:00
|
|
|
} else {
|
|
|
|
if (window.localStorage[periodKey]) {
|
|
|
|
period = window.localStorage[periodKey]
|
|
|
|
} else {
|
2019-11-19 07:44:54 +03:00
|
|
|
period = '30d'
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
period: period,
|
2020-09-09 11:13:55 +03:00
|
|
|
date: q.get('date') ? parseUTCDate(q.get('date')) : nowForSite(site),
|
2020-03-04 18:24:18 +03:00
|
|
|
from: q.get('from') ? parseUTCDate(q.get('from')) : undefined,
|
|
|
|
to: q.get('to') ? parseUTCDate(q.get('to')) : undefined,
|
2020-07-30 11:18:28 +03:00
|
|
|
filters: {
|
|
|
|
'goal': q.get('goal'),
|
2020-10-30 11:49:41 +03:00
|
|
|
'props': JSON.parse(q.get('props')),
|
2020-07-30 11:18:28 +03:00
|
|
|
'source': q.get('source'),
|
2020-09-28 11:29:24 +03:00
|
|
|
'utm_medium': q.get('utm_medium'),
|
|
|
|
'utm_source': q.get('utm_source'),
|
|
|
|
'utm_campaign': q.get('utm_campaign'),
|
2020-08-10 16:16:12 +03:00
|
|
|
'referrer': q.get('referrer'),
|
2020-10-13 12:03:42 +03:00
|
|
|
'screen': q.get('screen'),
|
|
|
|
'browser': q.get('browser'),
|
2020-11-10 16:18:59 +03:00
|
|
|
'browser_version': q.get('browser_version'),
|
2020-10-13 12:03:42 +03:00
|
|
|
'os': q.get('os'),
|
2020-11-10 16:18:59 +03:00
|
|
|
'os_version': q.get('os_version'),
|
2020-10-13 12:03:42 +03:00
|
|
|
'country': q.get('country'),
|
2020-08-10 16:16:12 +03:00
|
|
|
'page': q.get('page')
|
2020-07-30 11:18:28 +03:00
|
|
|
}
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-13 03:26:53 +03:00
|
|
|
function generateQueryString(data) {
|
|
|
|
const query = new URLSearchParams(window.location.search)
|
2020-12-15 13:29:12 +03:00
|
|
|
query.delete("date");
|
2020-11-13 03:26:53 +03:00
|
|
|
Object.keys(data).forEach(key => {
|
|
|
|
if (!data[key]) {
|
|
|
|
query.delete(key)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
query.set(key, data[key])
|
|
|
|
})
|
|
|
|
return query.toString()
|
|
|
|
}
|
|
|
|
|
2020-11-11 13:23:42 +03:00
|
|
|
export function navigateToQuery(history, queryFrom, newData) {
|
|
|
|
// if we update any data that we store in localstorage, make sure going back in history will revert them
|
|
|
|
if (newData.period && newData.period !== queryFrom.period) {
|
|
|
|
const replaceQuery = new URLSearchParams(window.location.search)
|
|
|
|
replaceQuery.set('period', queryFrom.period)
|
|
|
|
history.replace({ search: replaceQuery.toString() })
|
|
|
|
}
|
|
|
|
|
|
|
|
// then push the new query to the history
|
2020-11-13 03:26:53 +03:00
|
|
|
history.push({ search: generateQueryString(newData) })
|
|
|
|
}
|
2020-11-11 13:23:42 +03:00
|
|
|
|
2020-11-13 03:33:01 +03:00
|
|
|
class QueryLink extends React.Component {
|
2020-11-13 03:26:53 +03:00
|
|
|
constructor() {
|
|
|
|
super()
|
|
|
|
this.onClick = this.onClick.bind(this)
|
|
|
|
}
|
|
|
|
|
|
|
|
onClick(e) {
|
|
|
|
e.preventDefault()
|
2020-11-13 03:35:09 +03:00
|
|
|
navigateToQuery(this.props.history, this.props.query, this.props.to)
|
2020-11-13 03:26:53 +03:00
|
|
|
if (this.props.onClick) this.props.onClick(e)
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2020-11-13 03:35:09 +03:00
|
|
|
const { history, query, to, ...props } = this.props
|
2020-11-13 03:46:48 +03:00
|
|
|
return <Link
|
|
|
|
{...props}
|
|
|
|
to={{ pathname: window.location.pathname, search: generateQueryString(to) }}
|
|
|
|
onClick={this.onClick}
|
|
|
|
/>
|
2020-11-13 03:26:53 +03:00
|
|
|
}
|
2020-11-11 13:23:42 +03:00
|
|
|
}
|
2020-11-13 03:33:01 +03:00
|
|
|
const QueryLinkWithRouter = withRouter(QueryLink)
|
|
|
|
export { QueryLinkWithRouter as QueryLink };
|
2020-11-11 13:23:42 +03:00
|
|
|
|
2019-11-19 07:30:42 +03:00
|
|
|
export function toHuman(query) {
|
|
|
|
if (query.period === 'day') {
|
|
|
|
return `on ${formatDay(query.date)}`
|
|
|
|
} else if (query.period === 'month') {
|
|
|
|
return `in ${formatMonthYYYY(query.date)}`
|
|
|
|
} else if (query.period === '7d') {
|
|
|
|
return 'in the last 7 days'
|
|
|
|
} else if (query.period === '30d') {
|
|
|
|
return 'in the last 30 days'
|
2020-01-08 12:25:46 +03:00
|
|
|
} else if (query.period === '6mo') {
|
|
|
|
return 'in the last 6 months'
|
2020-01-08 12:23:03 +03:00
|
|
|
} else if (query.period === '12mo') {
|
2020-01-08 12:31:58 +03:00
|
|
|
return 'in the last 12 months'
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|
|
|
|
}
|
2019-11-20 08:48:27 +03:00
|
|
|
|
|
|
|
export function removeQueryParam(search, parameter) {
|
|
|
|
const q = new URLSearchParams(search)
|
|
|
|
q.delete(parameter)
|
|
|
|
return q.toString()
|
|
|
|
}
|
|
|
|
|
|
|
|
export function eventName(query) {
|
|
|
|
if (query.filters.goal) {
|
|
|
|
if (query.filters.goal.startsWith('Visit ')) {
|
|
|
|
return 'pageviews'
|
|
|
|
}
|
|
|
|
return 'events'
|
|
|
|
} else {
|
|
|
|
return 'pageviews'
|
|
|
|
}
|
|
|
|
}
|