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!

PR review fixes

Blue Mousecdbab366afd1
diff --git a/backend/apps/cloud/src/analytics/analytics.service.ts b/backend/apps/cloud/src/analytics/analytics.service.ts
index 913fb60b8..9c514dc74 100644
--- a/backend/apps/cloud/src/analytics/analytics.service.ts
+++ b/backend/apps/cloud/src/analytics/analytics.service.ts
@@ -115,6 +115,7 @@ import {
} from './interfaces'
import { ErrorDto } from './dto/error.dto'
import { GetPagePropertyMetaDto } from './dto/get-page-property-meta.dto'
+import { DATA_DELETION_EVENT_TYPES } from './dto/data-deletion.dto'
import { ProjectViewCustomEventMetaValueType } from '../project/entity/project-view-custom-event.entity'
import { ProjectViewCustomEventDto } from '../project/dto/create-project-view.dto'
import { UAParser } from '@ua-parser-js/pro-business'
@@ -142,13 +143,7 @@ const MAX_FILTERS = 100
const MAX_FILTER_VALUES = 100
// Event types that can be targeted by the data-deletion tool.
-const DELETABLE_EVENT_TYPES = [
- 'pageview',
- 'custom_event',
- 'error',
- 'performance',
- 'captcha',
-]
+const DELETABLE_EVENT_TYPES = [...DATA_DELETION_EVENT_TYPES]
// Sentinel bounds used when no explicit date range is given, so that
// session-scoped filters (entry/exit page, referrer) still resolve.
const DELETION_MIN_DATE = '2000-01-01 00:00:00'
@@ -1659,26 +1654,21 @@ export class AnalyticsService {
DataType.ANALYTICS,
)
- from = from ? dayjs.utc(from).format('YYYY-MM-DD HH:mm:ss') : null
- to = to ? dayjs.utc(to).format('YYYY-MM-DD HH:mm:ss') : null
+ const dateRange = this.getDataDeletionDateRange(from, to)
+ from = dateRange.from
+ to = dateRange.to
- const hasRange = Boolean(from && to)
const params: Record<string, unknown> = {
pid,
types: DELETABLE_EVENT_TYPES,
groupFrom: from || DELETION_MIN_DATE,
groupTo: to || DELETION_MAX_DATE,
...filtersParams,
+ ...(from ? { from } : {}),
+ ...(to ? { to } : {}),
}
- let dateCondition = ''
- if (hasRange) {
- params.from = from
- params.to = to
- dateCondition = 'AND created BETWEEN {from:String} AND {to:String}'
- }
-
- const whereClause = `pid = {pid:FixedString(12)} AND type IN ({types:Array(String)}) ${dateCondition} ${filtersQuery}`
+ const whereClause = `pid = {pid:FixedString(12)} AND type IN ({types:Array(String)}) ${dateRange.condition} ${filtersQuery}`
const { data: countRows } = await clickhouse
.query({
@@ -1718,16 +1708,44 @@ export class AnalyticsService {
return { counts: {}, total: 0, timeline: { x: [], counts: [] } }
}
+ const timelineFrom = from || minCreated
+ const timelineTo = to || maxCreated
+
+ if (!timelineFrom || !timelineTo) {
+ return { counts, total, timeline: { x: [], counts: [] } }
+ }
+
const timeline = await this.buildDeletionTimeline(
whereClause,
params,
- hasRange ? from : minCreated,
- hasRange ? to : maxCreated,
+ timelineFrom,
+ timelineTo,
)
return { counts, total, timeline }
}
+ private getDataDeletionDateRange(from: string | null, to: string | null) {
+ const normalizedFrom = from
+ ? dayjs.utc(from).format('YYYY-MM-DD HH:mm:ss')
+ : null
+ const normalizedTo = to ? dayjs.utc(to).format('YYYY-MM-DD HH:mm:ss') : null
+ const conditions: string[] = []
+
+ if (normalizedFrom) {
+ conditions.push('created >= {from:String}')
+ }
+ if (normalizedTo) {
+ conditions.push('created <= {to:String}')
+ }
+
+ return {
+ from: normalizedFrom,
+ to: normalizedTo,
+ condition: conditions.length ? `AND ${conditions.join(' AND ')}` : '',
+ }
+ }
+
private async buildDeletionTimeline(
whereClause: string,
baseParams: Record<string, unknown>,
@@ -1804,29 +1822,25 @@ export class AnalyticsService {
DataType.ANALYTICS,
)
- from = from ? dayjs.utc(from).format('YYYY-MM-DD HH:mm:ss') : null
- to = to ? dayjs.utc(to).format('YYYY-MM-DD HH:mm:ss') : null
+ const dateRange = this.getDataDeletionDateRange(from, to)
+ from = dateRange.from
+ to = dateRange.to
- const hasRange = Boolean(from && to)
const params: Record<string, unknown> = {
pid,
types: safeTypes,
groupFrom: from || DELETION_MIN_DATE,
groupTo: to || DELETION_MAX_DATE,
...filtersParams,
- }
-
- let dateCondition = ''
- if (hasRange) {
- params.from = from
- params.to = to
- dateCondition = 'AND created BETWEEN {from:String} AND {to:String}'
+ ...(from ? { from } : {}),
+ ...(to ? { to } : {}),
}
const commands: Promise<unknown>[] = [
clickhouse.command({
- query: `ALTER TABLE events DELETE WHERE pid = {pid:FixedString(12)} AND type IN ({types:Array(String)}) ${dateCondition} ${filtersQuery}`,
+ query: `ALTER TABLE events DELETE WHERE pid = {pid:FixedString(12)} AND type IN ({types:Array(String)}) ${dateRange.condition} ${filtersQuery}`,
query_params: params,
+ clickhouse_settings: { mutations_sync: '2' },
}),
]
@@ -1835,10 +1849,13 @@ export class AnalyticsService {
if (_includes(safeTypes, 'error') && _isEmpty(filtersQuery)) {
commands.push(
clickhouse.command({
- query: `ALTER TABLE error_statuses DELETE WHERE pid = {pid:FixedString(12)} ${
- hasRange ? 'AND created BETWEEN {from:String} AND {to:String}' : ''
- }`,
- query_params: hasRange ? { pid, from, to } : { pid },
+ query: `ALTER TABLE error_statuses DELETE WHERE pid = {pid:FixedString(12)} ${dateRange.condition}`,
+ query_params: {
+ pid,
+ ...(from ? { from } : {}),
+ ...(to ? { to } : {}),
+ },
+ clickhouse_settings: { mutations_sync: '2' },
}),
)
}
diff --git a/backend/apps/cloud/src/analytics/dto/data-deletion.dto.ts b/backend/apps/cloud/src/analytics/dto/data-deletion.dto.ts
index 75ef20e7b..ee48f8c8c 100644
--- a/backend/apps/cloud/src/analytics/dto/data-deletion.dto.ts
+++ b/backend/apps/cloud/src/analytics/dto/data-deletion.dto.ts
@@ -1,5 +1,6 @@
import { ApiProperty } from '@nestjs/swagger'
import {
+ IsIn,
IsNotEmpty,
IsOptional,
IsString,
@@ -8,6 +9,16 @@ import {
} from 'class-validator'
import { PID_REGEX } from '../../common/constants'
+export const DATA_DELETION_EVENT_TYPES = [
+ 'pageview',
+ 'custom_event',
+ 'error',
+ 'performance',
+ 'captcha',
+] as const
+
+export type DataDeletionEventType = (typeof DATA_DELETION_EVENT_TYPES)[number]
+
export class DataDeletionDto {
@ApiProperty({
example: 'aUn1quEid-3',
@@ -48,11 +59,13 @@ export class DataDeletionDto {
@ApiProperty({
required: false,
type: [String],
+ enum: DATA_DELETION_EVENT_TYPES,
description:
"Event types to delete. Any of: 'pageview', 'custom_event', 'error', 'performance', 'captcha'.",
})
@IsOptional()
@IsArray()
@IsString({ each: true })
- types?: string[]
+ @IsIn(DATA_DELETION_EVENT_TYPES as unknown as string[], { each: true })
+ types?: DataDeletionEventType[]
}
diff --git a/backend/apps/community/src/analytics/analytics.controller.ts b/backend/apps/community/src/analytics/analytics.controller.ts
index fb8efdca4..ca82cbaa2 100644
--- a/backend/apps/community/src/analytics/analytics.controller.ts
+++ b/backend/apps/community/src/analytics/analytics.controller.ts
@@ -1153,6 +1153,11 @@ export class AnalyticsController {
) {
await this.analyticsService.checkManageAccess(body.pid, uid)
+ this.logger.log(
+ { uid, pid: body.pid, types: body.types },
+ 'POST /log/data-deletion',
+ )
+
await this.analyticsService.deleteData(
body.pid,
body.filters || '[]',
diff --git a/backend/apps/community/src/analytics/analytics.service.ts b/backend/apps/community/src/analytics/analytics.service.ts
index f373b9d90..c33446a8e 100644
--- a/backend/apps/community/src/analytics/analytics.service.ts
+++ b/backend/apps/community/src/analytics/analytics.service.ts
@@ -101,6 +101,7 @@ import {
} from './interfaces'
import { ErrorDto } from './dto/error.dto'
import { GetPagePropertyMetaDto } from './dto/get-page-property-meta.dto'
+import { DATA_DELETION_EVENT_TYPES } from './dto/data-deletion.dto'
import { ProjectViewCustomEventMetaValueType } from '../project/entity/project-view-custom-event.entity'
import { ProjectViewCustomEventDto } from '../project/dto/create-project-view.dto'
import { UAParser } from '@ua-parser-js/pro-business'
@@ -125,13 +126,7 @@ const MAX_FILTERS = 100
const MAX_FILTER_VALUES = 100
// Event types that can be targeted by the data-deletion tool.
-const DELETABLE_EVENT_TYPES = [
- 'pageview',
- 'custom_event',
- 'error',
- 'performance',
- 'captcha',
-]
+const DELETABLE_EVENT_TYPES = [...DATA_DELETION_EVENT_TYPES]
// Sentinel bounds used when no explicit date range is given, so that
// session-scoped filters (entry/exit page, referrer) still resolve.
const DELETION_MIN_DATE = '2000-01-01 00:00:00'
diff --git a/backend/apps/community/src/analytics/dto/data-deletion.dto.ts b/backend/apps/community/src/analytics/dto/data-deletion.dto.ts
index 75ef20e7b..333743729 100644
--- a/backend/apps/community/src/analytics/dto/data-deletion.dto.ts
+++ b/backend/apps/community/src/analytics/dto/data-deletion.dto.ts
@@ -1,13 +1,26 @@
import { ApiProperty } from '@nestjs/swagger'
import {
+ IsDateString,
+ IsIn,
IsNotEmpty,
IsOptional,
IsString,
IsArray,
+ Length,
Matches,
} from 'class-validator'
import { PID_REGEX } from '../../common/constants'
+export const DATA_DELETION_EVENT_TYPES = [
+ 'pageview',
+ 'custom_event',
+ 'error',
+ 'performance',
+ 'captcha',
+] as const
+
+export type DataDeletionEventType = (typeof DATA_DELETION_EVENT_TYPES)[number]
+
export class DataDeletionDto {
@ApiProperty({
example: 'aUn1quEid-3',
@@ -30,29 +43,32 @@ export class DataDeletionDto {
@ApiProperty({
required: false,
- description:
- 'Start of the date range (inclusive). ISO string or YYYY-MM-DD.',
+ description: 'Start of the date range (inclusive). YYYY-MM-DD.',
})
@IsOptional()
- @IsString()
+ @IsDateString()
+ @Length(10, 10)
from?: string
@ApiProperty({
required: false,
- description: 'End of the date range (inclusive). ISO string or YYYY-MM-DD.',
+ description: 'End of the date range (inclusive). YYYY-MM-DD.',
})
@IsOptional()
- @IsString()
+ @IsDateString()
+ @Length(10, 10)
to?: string
@ApiProperty({
required: false,
type: [String],
+ enum: DATA_DELETION_EVENT_TYPES,
description:
"Event types to delete. Any of: 'pageview', 'custom_event', 'error', 'performance', 'captcha'.",
})
@IsOptional()
@IsArray()
@IsString({ each: true })
- types?: string[]
+ @IsIn(DATA_DELETION_EVENT_TYPES as unknown as string[], { each: true })
+ types?: DataDeletionEventType[]
}
diff --git a/web/app/pages/Project/Settings/ProjectSettings.tsx b/web/app/pages/Project/Settings/ProjectSettings.tsx
index eccef8efc..25e478891 100644
--- a/web/app/pages/Project/Settings/ProjectSettings.tsx
+++ b/web/app/pages/Project/Settings/ProjectSettings.tsx
@@ -1543,7 +1543,7 @@ const ProjectSettings = () => {
setShowReset={setShowReset}
setShowDelete={setShowDelete}
isDeleting={isDeleting}
- setResetting={false}
+ isResetting={false}
/>
</form>
) : null}
diff --git a/web/app/pages/Project/Settings/tabs/DangerZone.tsx b/web/app/pages/Project/Settings/tabs/DangerZone.tsx
index aa769bcf4..101e5b356 100644
--- a/web/app/pages/Project/Settings/tabs/DangerZone.tsx
+++ b/web/app/pages/Project/Settings/tabs/DangerZone.tsx
@@ -32,7 +32,7 @@ interface DangerZoneProps {
setShowReset: (show: boolean) => void
setShowDelete: (show: boolean) => void
isDeleting: boolean
- setResetting: boolean
+ isResetting: boolean
}
const DangerZone = ({
@@ -42,7 +42,7 @@ const DangerZone = ({
setShowReset,
setShowDelete,
isDeleting,
- setResetting,
+ isResetting,
}: DangerZoneProps) => {
const { t } = useTranslation('common')
@@ -89,8 +89,8 @@ const DangerZone = ({
<Button
variant='danger-outline'
type='button'
- onClick={() => !setResetting && setShowReset(true)}
- loading={setResetting}
+ onClick={() => !isResetting && setShowReset(true)}
+ loading={isResetting}
>
{t('project.settings.reset')}
</Button>
diff --git a/web/app/pages/Project/View/components/TabErrorBoundary.tsx b/web/app/pages/Project/View/components/TabErrorBoundary.tsx
index c0a9b67af..d95a913ee 100644
--- a/web/app/pages/Project/View/components/TabErrorBoundary.tsx
+++ b/web/app/pages/Project/View/components/TabErrorBoundary.tsx
@@ -1,7 +1,8 @@
-import { Component, type ReactNode } from 'react'
+import { Component, type ErrorInfo, type ReactNode } from 'react'
import { useTranslation } from 'react-i18next'
import StatusPage from '~/ui/StatusPage'
+import { trackError } from '~/utils/analytics'
import routes from '~/utils/routes'
const TabErrorFallback = ({ titleKey }: { titleKey?: string }) => {
@@ -28,10 +29,14 @@ interface TabErrorBoundaryProps {
children: ReactNode
/** Translation key for the title, e.g. `dashboard.failedToLoadTraffic`. Falls back to a generic title. */
titleKey?: string
+ resetKey?: unknown
}
interface TabErrorBoundaryState {
hasError: boolean
+ resetKey: unknown
+ titleKey?: string
+ children: ReactNode
}
/**
@@ -45,13 +50,48 @@ class TabErrorBoundary extends Component<
> {
constructor(props: TabErrorBoundaryProps) {
super(props)
- this.state = { hasError: false }
+ this.state = {
+ hasError: false,
+ resetKey: props.resetKey,
+ titleKey: props.titleKey,
+ children: props.children,
+ }
}
- static getDerivedStateFromError(): TabErrorBoundaryState {
+ static getDerivedStateFromError(): Partial<TabErrorBoundaryState> {
return { hasError: true }
}
+ static getDerivedStateFromProps(
+ props: TabErrorBoundaryProps,
+ state: TabErrorBoundaryState,
+ ): Partial<TabErrorBoundaryState> | null {
+ if (
+ props.resetKey === state.resetKey &&
+ props.titleKey === state.titleKey &&
+ props.children === state.children
+ ) {
+ return null
+ }
+
+ return {
+ hasError: false,
+ resetKey: props.resetKey,
+ titleKey: props.titleKey,
+ children: props.children,
+ }
+ }
+
+ componentDidCatch(error: Error, info: ErrorInfo) {
+ trackError({
+ name: `TabErrorBoundary: ${error.message}`,
+ message: error.message,
+ lineno: 0,
+ colno: 0,
+ stackTrace: [error.stack, info.componentStack].filter(Boolean).join('\n'),
+ })
+ }
+
render() {
if (this.state.hasError) {
return <TabErrorFallback titleKey={this.props.titleKey} />