2019-12-18 06:15:31 +03:00
|
|
|
import {formatDay, formatMonthYYYY, nowInOffset, parseUTCDate} from './date'
|
2019-11-19 07:30:42 +03:00
|
|
|
|
2020-03-04 18:24:18 +03:00
|
|
|
const PERIODS = ['day', 'month', '7d', '30d', '60d', '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-03-04 18:24:18 +03:00
|
|
|
if (period !== 'custom') 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,
|
2019-12-18 06:15:31 +03:00
|
|
|
date: q.get('date') ? parseUTCDate(q.get('date')) : nowInOffset(site.offset),
|
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,
|
|
|
|
filters: {'goal': q.get('goal')}
|
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:23:03 +03:00
|
|
|
} else if (query.period === '60d') {
|
|
|
|
return 'in the last 60 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'
|
|
|
|
}
|
|
|
|
}
|