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!

get previos & custom previos date

kruzhambus53f611c417fb
diff --git a/src/pages/Project/View/ViewProject.tsx b/src/pages/Project/View/ViewProject.tsx
index 1531de94f..5a1b0bd3c 100644
--- a/src/pages/Project/View/ViewProject.tsx
+++ b/src/pages/Project/View/ViewProject.tsx
@@ -42,7 +42,7 @@ import EventsRunningOutBanner from 'components/EventsRunningOutBanner'
import {
tbPeriodPairs, getProjectCacheKey, LIVE_VISITORS_UPDATE_INTERVAL, DEFAULT_TIMEZONE, CDN_URL, isDevelopment,
timeBucketToDays, getProjectCacheCustomKey, roleViewer, MAX_MONTHS_IN_PAST, PROJECT_TABS,
- TimeFormat, getProjectForcastCacheKey, chartTypes, roleAdmin, TRAFFIC_PANELS_ORDER, PERFORMANCE_PANELS_ORDER, isSelfhosted, tbPeriodPairsCompare, PERIOD_PAIRS_COMPARE,
+ TimeFormat, getProjectForcastCacheKey, chartTypes, roleAdmin, TRAFFIC_PANELS_ORDER, PERFORMANCE_PANELS_ORDER, isSelfhosted, tbPeriodPairsCompare, PERIOD_PAIRS_COMPARE, periodToCompareDate,
} from 'redux/constants'
import { IUser } from 'redux/models/IUser'
import { IProject, ILiveStats } from 'redux/models/IProject'
@@ -218,7 +218,6 @@ const ViewProject = ({
const [isActiveCompare, setIsActiveCompare] = useState<boolean>(false)
const [activePeriodCompare, setActivePeriodCompare] = useState<string>(periodPairsCompare[0].period)
const activeDropdownLabelCompare = useMemo(() => _find(periodPairsCompare, p => p.period === activePeriodCompare)?.label, [periodPairsCompare, activePeriodCompare])
- console.log('activeDropdownLabelCompare', activeDropdownLabelCompare)
const [dateRangeCompare, setDateRangeCompare] = useState<null | Date[]>(null)
const tabs: {
@@ -467,6 +466,21 @@ const ViewProject = ({
let to
let customEventsChart = customEventsChartData
+ if (isActiveCompare) {
+ if (dateRangeCompare && activePeriodCompare === PERIOD_PAIRS_COMPARE.CUSTOM) {
+ from = getFormatDate(dateRangeCompare[0])
+ to = getFormatDate(dateRangeCompare[1])
+ } else {
+ let date
+ if (dateRange) {
+ date = _find(periodToCompareDate, (item) => item.period === period)?.formula(dateRange)
+ } else {
+ date = _find(periodToCompareDate, (item) => item.period === period)?.formula()
+ }
+ console.log('date', date)
+ }
+ }
+
if (dateRange) {
from = getFormatDate(dateRange[0])
to = getFormatDate(dateRange[1])
@@ -1426,6 +1440,14 @@ const ViewProject = ({
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [chartType])
+ // loadAnalytics when compare period change or compare selected
+ useEffect(() => {
+ if (isActiveCompare) {
+ loadAnalytics()
+ }
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ }, [isActiveCompare, activePeriodCompare])
+
if (!isLoading) {
return (
<Title title={name}>
diff --git a/src/redux/constants/index.ts b/src/redux/constants/index.ts
index 1ed990029..6ecaa3aab 100644
--- a/src/redux/constants/index.ts
+++ b/src/redux/constants/index.ts
@@ -98,6 +98,93 @@ export const PERIOD_PAIRS_COMPARE: {
DISABLE: 'disable',
}
+export const periodToCompareDate: {
+ period: string
+ formula: (date?: Date[]) => {
+ from: Date
+ to: Date
+ }
+}[] = [{
+ period: '1d',
+ formula: () => {
+ const to = new Date()
+ to.setDate(to.getDate() - 1)
+ const from = new Date()
+ from.setDate(to.getDate() - 1)
+ return {
+ from,
+ to,
+ }
+ },
+}, {
+ period: '7d',
+ formula: () => {
+ const to = new Date()
+ to.setDate(to.getDate() - 7)
+ const from = new Date()
+ from.setDate(to.getDate() - 7)
+ return {
+ from,
+ to,
+ }
+ },
+}, {
+ period: '4w',
+ formula: () => {
+ const to = new Date()
+ to.setDate(to.getDate() - 28)
+ const from = new Date()
+ from.setDate(to.getDate() - 28)
+ return {
+ from,
+ to,
+ }
+ },
+}, {
+ period: '3M',
+ formula: () => {
+ const to = new Date()
+ to.setMonth(to.getMonth() - 3)
+ const from = new Date()
+ from.setMonth(to.getMonth() - 3)
+ return {
+ from,
+ to,
+ }
+ },
+}, {
+ period: '12M',
+ formula: () => {
+ const to = new Date()
+ to.setMonth(to.getMonth() - 12)
+ const from = new Date()
+ from.setMonth(to.getMonth() - 12)
+ return {
+ from,
+ to,
+ }
+ },
+}, {
+ period: 'custom',
+ formula: (date) => {
+ if (!date) {
+ return {
+ from: new Date(),
+ to: new Date(),
+ }
+ }
+ const days = (date[1].getTime() - date[0].getTime()) / (1000 * 3600 * 24)
+ const to = new Date()
+ to.setDate(to.getDate() - days)
+ const from = new Date()
+ from.setDate(to.getDate() - days)
+ return {
+ from,
+ to,
+ }
+ },
+}]
+
interface IStringObject {
[key: string]: string
}