Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

(add) new period alltime and thishour (#341)

Maksim Mrugf59d3e5120b1
diff --git a/app/api/index.ts b/app/api/index.ts
index c20ad5f28..4d33a19e5 100644
--- a/app/api/index.ts
+++ b/app/api/index.ts
@@ -370,7 +370,7 @@ export const resetCaptchaProject = (id: string) =>
export const getProjectData = (
pid: string,
tb: string = 'hour',
- period: string = '3d',
+ period: string = '1d',
filters: string[] = [],
from: string = '',
to: string = '',
diff --git a/app/pages/Captcha/View/ViewCaptcha.tsx b/app/pages/Captcha/View/ViewCaptcha.tsx
index 070e04155..cc926286d 100644
--- a/app/pages/Captcha/View/ViewCaptcha.tsx
+++ b/app/pages/Captcha/View/ViewCaptcha.tsx
@@ -32,7 +32,7 @@ import { getItem, setItem } from 'utils/localstorage'
import EventsRunningOutBanner from 'components/EventsRunningOutBanner'
import {
tbPeriodPairs, getProjectCaptchaCacheKey, timeBucketToDays, getProjectCacheCustomKey, roleAdmin,
- MAX_MONTHS_IN_PAST, TimeFormat, chartTypes, TITLE_SUFFIX,
+ MAX_MONTHS_IN_PAST, TimeFormat, chartTypes, TITLE_SUFFIX, KEY_FOR_ALL_TIME,
} from 'redux/constants'
import { ICaptchaProject, IProject, ILiveStats } from 'redux/models/IProject'
import { IUser } from 'redux/models/IUser'
@@ -211,18 +211,39 @@ const ViewProject = ({
}
const {
- chart, params, customs, appliedFilters,
+ chart, params, customs, appliedFilters, timeBucket: timeBucketFromResponse,
} = data
+ let newTimebucket = timeBucket
+
if (!_isEmpty(appliedFilters)) {
setFilters(appliedFilters)
}
+ if (period === KEY_FOR_ALL_TIME && !_isEmpty(timeBucketFromResponse)) {
+ // eslint-disable-next-line prefer-destructuring
+ newTimebucket = _includes(timeBucketFromResponse, timeBucket) ? timeBucket : timeBucketFromResponse[0]
+ setPeriodPairs((prev) => {
+ // find in prev state period === KEY_FOR_ALL_TIME and change tbs
+ const newPeriodPairs = _map(prev, (item) => {
+ if (item.period === KEY_FOR_ALL_TIME) {
+ return {
+ ...item,
+ tbs: timeBucketFromResponse.length > 2 ? [timeBucketFromResponse[0], timeBucketFromResponse[1]] : timeBucketFromResponse,
+ }
+ }
+ return item
+ })
+ return newPeriodPairs
+ })
+ setTimebucket(newTimebucket)
+ }
+
if (_isEmpty(params)) {
setIsPanelsDataEmpty(true)
} else {
const applyRegions = !_includes(noRegionPeriods, activePeriod.period)
- const bbSettings: any = getSettings(chart, timeBucket, activeChartMetrics, applyRegions, timeFormat, rotateXAxias, chartType)
+ const bbSettings: any = getSettings(chart, newTimebucket, activeChartMetrics, applyRegions, timeFormat, rotateXAxias, chartType)
setChartData(chart)
setPanelsData({
@@ -248,11 +269,6 @@ const ViewProject = ({
}
}
- useEffect(() => {
- loadAnalytics()
- // eslint-disable-next-line react-hooks/exhaustive-deps
- }, [chartType])
-
// this funtion is used for requesting the data from the API when the filter is changed
const filterHandler = (column: any, filter: any, isExclusive: boolean = false) => {
let newFilters
@@ -417,10 +433,25 @@ const ViewProject = ({
}
useEffect(() => {
+ if (period !== KEY_FOR_ALL_TIME) {
+ return
+ }
+
+ if (areFiltersParsed && areTimeBucketParsed && arePeriodParsed) {
+ loadAnalytics()
+ }
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ }, [project, period, chartType, filters, arePeriodParsed])
+
+ useEffect(() => {
+ if (period === KEY_FOR_ALL_TIME) {
+ return
+ }
+
if (areFiltersParsed && areTimeBucketParsed && arePeriodParsed) {
loadAnalytics()
}
- }, [project, period, timeBucket, periodPairs, areFiltersParsed, areTimeBucketParsed, arePeriodParsed, t]) // eslint-disable-line
+ }, [project, period, chartType, timeBucket, periodPairs, areFiltersParsed, areTimeBucketParsed, arePeriodParsed, t]) // eslint-disable-line
useEffect(() => {
if (dateRange && arePeriodParsed) {
diff --git a/app/pages/Project/View/ViewProject.tsx b/app/pages/Project/View/ViewProject.tsx
index 72d9b7f8d..6d3929300 100644
--- a/app/pages/Project/View/ViewProject.tsx
+++ b/app/pages/Project/View/ViewProject.tsx
@@ -48,7 +48,7 @@ import {
tbPeriodPairs, getProjectCacheKey, LIVE_VISITORS_UPDATE_INTERVAL, DEFAULT_TIMEZONE, CDN_URL, isDevelopment,
timeBucketToDays, getProjectCacheCustomKey, MAX_MONTHS_IN_PAST, PROJECT_TABS, TimeFormat, getProjectForcastCacheKey, chartTypes, roleAdmin,
TRAFFIC_PANELS_ORDER, PERFORMANCE_PANELS_ORDER, isSelfhosted, tbPeriodPairsCompare, PERIOD_PAIRS_COMPARE, filtersPeriodPairs, IS_ACTIVE_COMPARE,
- PROJECTS_PROTECTED, getProjectCacheCustomKeyPerf, isBrowser, TITLE_SUFFIX, FILTERS_PANELS_ORDER,
+ PROJECTS_PROTECTED, getProjectCacheCustomKeyPerf, isBrowser, TITLE_SUFFIX, FILTERS_PANELS_ORDER, KEY_FOR_ALL_TIME,
} from 'redux/constants'
import { IUser } from 'redux/models/IUser'
import { IProject, ILiveStats } from 'redux/models/IProject'
@@ -660,7 +660,7 @@ const ViewProject = ({
setDataLoading(true)
try {
- let data
+ let data: { timeBucket?: any; chart?: any; params?: any; customs?: any; appliedFilters?: any; avgSdur?: any }
let dataCompare
let key = ''
let keyCompare = ''
@@ -776,9 +776,29 @@ const ViewProject = ({
const {
chart, params, customs, appliedFilters, avgSdur,
} = data
+ let newTimebucket = timeBucket
sdkInstance?._emitEvent('load', sdkData)
const processedSdur = getTimeFromSeconds(avgSdur)
+ if (period === KEY_FOR_ALL_TIME && !_isEmpty(data.timeBucket)) {
+ // eslint-disable-next-line prefer-destructuring
+ newTimebucket = _includes(data.timeBucket, timeBucket) ? timeBucket : data.timeBucket[0]
+ setPeriodPairs((prev) => {
+ // find in prev state period === KEY_FOR_ALL_TIME and change tbs
+ const newPeriodPairs = _map(prev, (item) => {
+ if (item.period === KEY_FOR_ALL_TIME) {
+ return {
+ ...item,
+ tbs: data.timeBucket.length > 2 ? [data.timeBucket[0], data.timeBucket[1]] : data.timeBucket,
+ }
+ }
+ return item
+ })
+ return newPeriodPairs
+ })
+ setTimebucket(newTimebucket)
+ }
+
setSessionDurationAVG(getStringFromTime(processedSdur))
if (!_isEmpty(appliedFilters)) {
@@ -793,7 +813,7 @@ const ViewProject = ({
setIsPanelsDataEmpty(true)
} else {
const applyRegions = !_includes(noRegionPeriods, activePeriod?.period)
- const bbSettings = getSettings(chart, timeBucket, activeChartMetrics, applyRegions, timeFormat, forecasedChartData, rotateXAxias, chartType, customEventsChart, dataCompare?.chart)
+ const bbSettings = getSettings(chart, newTimebucket, activeChartMetrics, applyRegions, timeFormat, forecasedChartData, rotateXAxias, chartType, customEventsChart, dataCompare?.chart)
setChartData(chart)
setPanelsData({
@@ -833,7 +853,7 @@ const ViewProject = ({
setDataLoading(true)
try {
- let dataPerf
+ let dataPerf: { timeBucket?: any; params?: any; appliedFilters?: any; chart?: any }
let key
let from
let to
@@ -927,6 +947,27 @@ const ViewProject = ({
return
}
+ let newTimebucket = timeBucket
+
+ if (period === KEY_FOR_ALL_TIME && !_isEmpty(dataPerf.timeBucket)) {
+ // eslint-disable-next-line prefer-destructuring
+ newTimebucket = _includes(dataPerf.timeBucket, timeBucket) ? timeBucket : dataPerf.timeBucket[0]
+ setPeriodPairs((prev) => {
+ // find in prev state period === KEY_FOR_ALL_TIME and change tbs
+ const newPeriodPairs = _map(prev, (item) => {
+ if (item.period === KEY_FOR_ALL_TIME) {
+ return {
+ ...item,
+ tbs: dataPerf.timeBucket.length > 2 ? [dataPerf.timeBucket[0], dataPerf.timeBucket[1]] : dataPerf.timeBucket,
+ }
+ }
+ return item
+ })
+ return newPeriodPairs
+ })
+ setTimebucket(newTimebucket)
+ }
+
if (!_isEmpty(dataCompare) && !_isEmpty(dataCompare?.chart)) {
setDataChartPerfCompare(dataCompare.chart)
}
@@ -1534,6 +1575,10 @@ const ViewProject = ({
// useEffect using for call loadAnalytics or loadAnalyticsPerf when smth dependencies changed
useEffect(() => {
+ if (period === KEY_FOR_ALL_TIME) {
+ return
+ }
+
if (areFiltersParsed && areTimeBucketParsed && arePeriodParsed) {
if (activeTab === PROJECT_TABS.traffic) {
loadAnalytics()
@@ -1546,6 +1591,24 @@ const ViewProject = ({
}
}, [project, period, chartType, filters, forecasedChartData, timeBucket, periodPairs, areFiltersParsed, areTimeBucketParsed, arePeriodParsed, t, activeTab, areFiltersPerfParsed]) // eslint-disable-line
+ useEffect(() => {
+ if (period !== KEY_FOR_ALL_TIME) {
+ return
+ }
+
+ if (areFiltersParsed && areTimeBucketParsed && arePeriodParsed) {
+ if (activeTab === PROJECT_TABS.traffic) {
+ loadAnalytics()
+ }
+ }
+ if (areFiltersPerfParsed && areTimeBucketParsed && arePeriodParsed) {
+ if (activeTab === PROJECT_TABS.performance) {
+ loadAnalyticsPerf()
+ }
+ }
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ }, [project, period, chartType, filters, forecasedChartData, areFiltersParsed, areTimeBucketParsed, arePeriodParsed, activeTab, areFiltersPerfParsed])
+
// using this for fix some bugs with update custom events data for chart
useEffect(() => {
if (!_isEmpty(activeChartMetricsCustomEvents)) {
diff --git a/app/redux/constants/index.ts b/app/redux/constants/index.ts
index 514af3a6e..3d92b47a3 100644
--- a/app/redux/constants/index.ts
+++ b/app/redux/constants/index.ts
@@ -21,6 +21,8 @@ export const FORECAST_MAX_MAPPING: {
month: 12,
}
+export const KEY_FOR_ALL_TIME = 'all'
+
export const tbPeriodPairs = (t: Function, tbs?: string[] | null, dates?: Date[]): {
label: string
period: string
@@ -29,6 +31,10 @@ export const tbPeriodPairs = (t: Function, tbs?: string[] | null, dates?: Date[]
dropdownLabel?: string
isCustomDate?: boolean
}[] => [{
+ label: t('project.thishour'),
+ period: 'thishour',
+ tbs: ['hour'],
+}, {
label: t('project.today'),
period: 'today',
tbs: ['hour'],
@@ -65,6 +71,10 @@ export const tbPeriodPairs = (t: Function, tbs?: string[] | null, dates?: Date[]
label: t('project.lastXMonths', { amount: 24 }),
period: '24M',
tbs: ['month'],
+}, {
+ label: t('project.all'),
+ period: KEY_FOR_ALL_TIME,
+ tbs: ['month', 'year'],
}, {
label: dates ? getCustomLabel(dates, t) : t('project.custom'),
dropdownLabel: t('project.custom'),
@@ -77,7 +87,7 @@ export const tbPeriodPairs = (t: Function, tbs?: string[] | null, dates?: Date[]
tbs: tbs || ['custom'],
}]
-export const filtersPeriodPairs = ['1d', '7d', '4w', '3M', '12M', 'custom', 'compare']
+export const filtersPeriodPairs = ['thishour', '1d', '7d', '4w', '3M', '12M', 'custom', 'compare']
export const tbPeriodPairsCompare = (t: Function, dates?: Date[]): {
label: string
@@ -132,24 +142,28 @@ export const tbsFormatMapper: IStringObject = {
hour: '%I %p',
day: '%d %b',
month: '%b %Y',
+ year: '%Y',
}
export const tbsFormatMapperTooltip: IStringObject = {
hour: '%d %b %I %p',
day: '%d %b',
month: '%b %Y',
+ year: '%Y',
}
export const tbsFormatMapperTooltip24h: IStringObject = {
hour: '%d %b %H:%M',
day: '%d %b',
month: '%b %Y',
+ year: '%Y',
}
export const tbsFormatMapper24h: IStringObject = {
hour: '%H:%M',
day: '%d %b',
month: '%b %Y',
+ year: '%Y',
}
export const TimeFormat: IStringObject = {
diff --git a/public/locales/en.json b/public/locales/en.json
index 25d6c4c0a..1e3ca9f36 100644
--- a/public/locales/en.json
+++ b/public/locales/en.json
@@ -590,6 +590,8 @@
"unique": "Unique visitors",
"results": "Results",
"total": "Total page views",
+ "thishour": "This hour",
+ "all": "All time",
"today": "Today",
"yesterday": "Yesterday",
"last24h": "Last 24 hours",
@@ -601,6 +603,7 @@
"day": "Day",
"week": "Week",
"month": "Month",
+ "year": "Year",
"resetFilters": "Reset filters",
"exportImgError": "Exporting data as image failed",
"codeExample": "Or insert the following code into your website to track your events:",