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!

Better project reset functionality

Blue Mouse76cd59edd71b
diff --git a/backend/apps/cloud/src/analytics/analytics.controller.ts b/backend/apps/cloud/src/analytics/analytics.controller.ts
index b8b858dc0..f5530b99d 100644
--- a/backend/apps/cloud/src/analytics/analytics.controller.ts
+++ b/backend/apps/cloud/src/analytics/analytics.controller.ts
@@ -22,6 +22,7 @@ import {
Patch,
Delete,
Headers,
+ HttpCode,
BadRequestException,
InternalServerErrorException,
UnprocessableEntityException,
@@ -68,6 +69,7 @@ import { clickhouse } from '../common/integrations/clickhouse'
import { checkRateLimit, getIPDetails, getIPFromHeaders } from '../common/utils'
import { GetCustomEventsDto } from './dto/get-custom-events.dto'
import { GetFiltersDto } from './dto/get-filters.dto'
+import { DataDeletionDto } from './dto/data-deletion.dto'
import { GetVersionFiltersDto } from './dto/get-version-filters.dto'
import {
IFunnel,
@@ -2879,6 +2881,48 @@ export class AnalyticsController {
return this.analyticsService.deleteSessionReplay(pid, psid, replayId)
}
+ @Post('data-deletion/preview')
+ @HttpCode(200)
+ @Auth()
+ async getDataDeletionPreview(
+ @Body() body: DataDeletionDto,
+ @CurrentUserId() uid: string,
+ ) {
+ await this.analyticsService.checkManageAccess(body.pid, uid)
+
+ return this.analyticsService.getDataDeletionPreview(
+ body.pid,
+ body.filters || '[]',
+ body.from || null,
+ body.to || null,
+ )
+ }
+
+ @Post('data-deletion')
+ @HttpCode(200)
+ @Auth()
+ async deleteData(
+ @Body() body: DataDeletionDto,
+ @CurrentUserId() uid: string,
+ ) {
+ 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 || '[]',
+ body.from || null,
+ body.to || null,
+ body.types || ['pageview', 'custom_event'],
+ )
+
+ return { deleted: true }
+ }
+
@Post('session-replay/export')
@Auth(true, true)
async startSessionReplayExport(
diff --git a/backend/apps/cloud/src/analytics/analytics.service.ts b/backend/apps/cloud/src/analytics/analytics.service.ts
index 5672d0971..913fb60b8 100644
--- a/backend/apps/cloud/src/analytics/analytics.service.ts
+++ b/backend/apps/cloud/src/analytics/analytics.service.ts
@@ -140,6 +140,29 @@ const isValidLocale = (lc: string): boolean => {
const LIVE_SESSION_THRESHOLD_SECONDS = 120
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',
+]
+// 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'
+const DELETION_MAX_DATE = '2100-01-01 00:00:00'
+const DELETION_TIMELINE_BUCKETS = 40
+const DELETION_TIMELINE_MAX_POINTS = 60
+
+export interface DataDeletionPreview {
+ // count of matching rows, keyed by event type
+ counts: Record<string, number>
+ total: number
+ // gap-filled, uniform buckets for the preview sparkline
+ timeline: { x: string[]; counts: number[] }
+}
const SESSION_REPLAY_RETENTION_VALUES = [30, 90, 365, 1825] as const
const MAX_SESSION_REPLAY_EVENTS_PER_CHUNK = 1000
const MAX_SESSION_REPLAY_EVENT_BYTES = 5 * 1024 * 1024
@@ -1619,6 +1642,210 @@ export class AnalyticsService {
]
}
+ // Counts the rows a data-deletion request would remove and returns a uniform
+ // timeline for the preview sparkline. Mirrors the WHERE clause of deleteData
+ // exactly, so the preview is an honest dry run of the destructive query.
+ async getDataDeletionPreview(
+ pid: string,
+ filters: string,
+ from: string | null,
+ to: string | null,
+ ): Promise<DataDeletionPreview> {
+ // The preview always counts every deletable type so the UI can show a
+ // per-type breakdown; which types actually get deleted is chosen at delete
+ // time and applied in deleteData.
+ const [filtersQuery, filtersParams] = this.getFiltersQuery(
+ filters,
+ 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 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,
+ }
+
+ 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 { data: countRows } = await clickhouse
+ .query({
+ query: `
+ SELECT type, count() AS c, min(created) AS mn, max(created) AS mx
+ FROM events
+ WHERE ${whereClause}
+ GROUP BY type
+ `,
+ query_params: params,
+ })
+ .then((resultSet) =>
+ resultSet.json<{ type: string; c: string; mn: string; mx: string }>(),
+ )
+
+ const counts: Record<string, number> = {}
+ let total = 0
+ let minCreated: string | null = null
+ let maxCreated: string | null = null
+
+ for (const row of countRows) {
+ const count = Number(row.c) || 0
+ if (count === 0) {
+ continue
+ }
+ counts[row.type] = count
+ total += count
+ if (!minCreated || row.mn < minCreated) {
+ minCreated = row.mn
+ }
+ if (!maxCreated || row.mx > maxCreated) {
+ maxCreated = row.mx
+ }
+ }
+
+ if (total === 0) {
+ return { counts: {}, total: 0, timeline: { x: [], counts: [] } }
+ }
+
+ const timeline = await this.buildDeletionTimeline(
+ whereClause,
+ params,
+ hasRange ? from : minCreated,
+ hasRange ? to : maxCreated,
+ )
+
+ return { counts, total, timeline }
+ }
+
+ private async buildDeletionTimeline(
+ whereClause: string,
+ baseParams: Record<string, unknown>,
+ windowFrom: string,
+ windowTo: string,
+ ): Promise<{ x: string[]; counts: number[] }> {
+ const fromMs = dayjs.utc(windowFrom).valueOf()
+ const toMs = dayjs.utc(windowTo).valueOf()
+ const spanSeconds = Math.max(1, Math.round((toMs - fromMs) / 1000))
+ const bucketSeconds = Math.max(
+ 1,
+ Math.ceil(spanSeconds / DELETION_TIMELINE_BUCKETS),
+ )
+
+ const { data: rows } = await clickhouse
+ .query({
+ query: `
+ SELECT
+ toStartOfInterval(created, INTERVAL {bucketSeconds:UInt32} SECOND) AS t,
+ count() AS c
+ FROM events
+ WHERE ${whereClause}
+ GROUP BY t
+ ORDER BY t
+ `,
+ query_params: { ...baseParams, bucketSeconds },
+ })
+ .then((resultSet) => resultSet.json<{ t: string; c: string }>())
+
+ const bucketed = new Map<string, number>()
+ for (const row of rows) {
+ bucketed.set(row.t, Number(row.c) || 0)
+ }
+
+ // toStartOfInterval aligns buckets to multiples of the interval since the
+ // unix epoch, so align our JS walk the same way and fill empty buckets.
+ const bucketMs = bucketSeconds * 1000
+ const startMs = Math.floor(fromMs / bucketMs) * bucketMs
+ const x: string[] = []
+ const counts: number[] = []
+
+ for (
+ let ms = startMs;
+ ms <= toMs && x.length < DELETION_TIMELINE_MAX_POINTS;
+ ms += bucketMs
+ ) {
+ const key = dayjs.utc(ms).format('YYYY-MM-DD HH:mm:ss')
+ x.push(key)
+ counts.push(bucketed.get(key) ?? 0)
+ }
+
+ return { x, counts }
+ }
+
+ // Permanently removes matching analytics rows. The WHERE clause is identical
+ // to getDataDeletionPreview's, so what the user previews is what gets deleted.
+ async deleteData(
+ pid: string,
+ filters: string,
+ from: string | null,
+ to: string | null,
+ types: string[],
+ ): Promise<void> {
+ const safeTypes = _filter(types, (type) =>
+ _includes(DELETABLE_EVENT_TYPES, type),
+ )
+
+ if (_isEmpty(safeTypes)) {
+ throw new BadRequestException('No event types selected for deletion')
+ }
+
+ const [filtersQuery, filtersParams] = this.getFiltersQuery(
+ filters,
+ 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 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}'
+ }
+
+ 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_params: params,
+ }),
+ ]
+
+ // error_statuses carries no traffic columns, so it can only be cleared when
+ // no field filters are applied (a full reset or a plain date-range reset).
+ 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 },
+ }),
+ )
+ }
+
+ await Promise.all(commands)
+ }
+
derivePsidFromInputs(
pid: string,
userAgent: string,
diff --git a/backend/apps/cloud/src/analytics/dto/data-deletion.dto.ts b/backend/apps/cloud/src/analytics/dto/data-deletion.dto.ts
new file mode 100644
index 000000000..75ef20e7b
--- /dev/null
+++ b/backend/apps/cloud/src/analytics/dto/data-deletion.dto.ts
@@ -0,0 +1,58 @@
+import { ApiProperty } from '@nestjs/swagger'
+import {
+ IsNotEmpty,
+ IsOptional,
+ IsString,
+ IsArray,
+ Matches,
+} from 'class-validator'
+import { PID_REGEX } from '../../common/constants'
+
+export class DataDeletionDto {
+ @ApiProperty({
+ example: 'aUn1quEid-3',
+ required: true,
+ description: 'The project ID',
+ })
+ @IsNotEmpty()
+ @Matches(PID_REGEX, { message: 'The provided Project ID (pid) is incorrect' })
+ pid: string
+
+ @ApiProperty({
+ required: false,
+ description:
+ 'JSON-encoded array of filters, identical to the dashboard filter format. ' +
+ 'Example: [{"column":"cc","filter":"BG","isExclusive":false}]',
+ })
+ @IsOptional()
+ @IsString()
+ filters?: string
+
+ @ApiProperty({
+ required: false,
+ description:
+ 'Start of the date range (inclusive). ISO string or YYYY-MM-DD.',
+ })
+ @IsOptional()
+ @IsString()
+ from?: string
+
+ @ApiProperty({
+ required: false,
+ description: 'End of the date range (inclusive). ISO string or YYYY-MM-DD.',
+ })
+ @IsOptional()
+ @IsString()
+ to?: string
+
+ @ApiProperty({
+ required: false,
+ type: [String],
+ description:
+ "Event types to delete. Any of: 'pageview', 'custom_event', 'error', 'performance', 'captcha'.",
+ })
+ @IsOptional()
+ @IsArray()
+ @IsString({ each: true })
+ types?: string[]
+}
diff --git a/backend/apps/community/src/analytics/analytics.controller.ts b/backend/apps/community/src/analytics/analytics.controller.ts
index bd9e631c1..fb8efdca4 100644
--- a/backend/apps/community/src/analytics/analytics.controller.ts
+++ b/backend/apps/community/src/analytics/analytics.controller.ts
@@ -17,6 +17,7 @@ import {
Post,
Patch,
Headers,
+ HttpCode,
BadRequestException,
InternalServerErrorException,
UnprocessableEntityException,
@@ -42,6 +43,7 @@ import { PageviewsDto } from './dto/pageviews.dto'
import { EventsDto } from './dto/events.dto'
import { GetDataDto, ChartRenderMode, TimeBucketType } from './dto/getData.dto'
import { GetFiltersDto } from './dto/get-filters.dto'
+import { DataDeletionDto } from './dto/data-deletion.dto'
import { GetVersionFiltersDto } from './dto/get-version-filters.dto'
import { GetCustomEventMetadata } from './dto/get-custom-event-meta.dto'
import { GetPagePropertyMetaDto } from './dto/get-page-property-meta.dto'
@@ -1125,6 +1127,43 @@ export class AnalyticsController {
return data
}
+ @Post('data-deletion/preview')
+ @HttpCode(200)
+ @Auth()
+ async getDataDeletionPreview(
+ @Body() body: DataDeletionDto,
+ @CurrentUserId() uid: string,
+ ) {
+ await this.analyticsService.checkManageAccess(body.pid, uid)
+
+ return this.analyticsService.getDataDeletionPreview(
+ body.pid,
+ body.filters || '[]',
+ body.from || null,
+ body.to || null,
+ )
+ }
+
+ @Post('data-deletion')
+ @HttpCode(200)
+ @Auth()
+ async deleteData(
+ @Body() body: DataDeletionDto,
+ @CurrentUserId() uid: string,
+ ) {
+ await this.analyticsService.checkManageAccess(body.pid, uid)
+
+ await this.analyticsService.deleteData(
+ body.pid,
+ body.filters || '[]',
+ body.from || null,
+ body.to || null,
+ body.types || ['pageview', 'custom_event'],
+ )
+
+ return { deleted: true }
+ }
+
@Post('custom')
@Public()
async logCustom(
diff --git a/backend/apps/community/src/analytics/analytics.service.ts b/backend/apps/community/src/analytics/analytics.service.ts
index 4e4a14200..f373b9d90 100644
--- a/backend/apps/community/src/analytics/analytics.service.ts
+++ b/backend/apps/community/src/analytics/analytics.service.ts
@@ -124,6 +124,29 @@ const LIVE_SESSION_THRESHOLD_SECONDS = 120
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',
+]
+// 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'
+const DELETION_MAX_DATE = '2100-01-01 00:00:00'
+const DELETION_TIMELINE_BUCKETS = 40
+const DELETION_TIMELINE_MAX_POINTS = 60
+
+export interface DataDeletionPreview {
+ // count of matching rows, keyed by event type
+ counts: Record<string, number>
+ total: number
+ // gap-filled, uniform buckets for the preview sparkline
+ timeline: { x: string[]; counts: number[] }
+}
+
const SOFTWARE_WITH_PATCH_VERSION = ['GameVault']
type FunnelStepDetails = Record<
@@ -503,6 +526,11 @@ export class AnalyticsService {
this.projectService.allowedToView(project, uid, password)
}
+ async checkManageAccess(pid: string, uid: string | null): Promise<void> {
+ const project = await this.projectService.getRedisProject(pid)
+ this.projectService.allowedToManage(project, uid)
+ }
+
async isBot(pid: string, userAgent: string) {
const project = await this.projectService.getRedisProject(pid)
@@ -1426,6 +1454,210 @@ export class AnalyticsService {
]
}
+ // Counts the rows a data-deletion request would remove and returns a uniform
+ // timeline for the preview sparkline. Mirrors the WHERE clause of deleteData
+ // exactly, so the preview is an honest dry run of the destructive query.
+ async getDataDeletionPreview(
+ pid: string,
+ filters: string,
+ from: string | null,
+ to: string | null,
+ ): Promise<DataDeletionPreview> {
+ // The preview always counts every deletable type so the UI can show a
+ // per-type breakdown; which types actually get deleted is chosen at delete
+ // time and applied in deleteData.
+ const [filtersQuery, filtersParams] = this.getFiltersQuery(
+ filters,
+ 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 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,
+ }
+
+ 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 { data: countRows } = await clickhouse
+ .query({
+ query: `
+ SELECT type, count() AS c, min(created) AS mn, max(created) AS mx
+ FROM events
+ WHERE ${whereClause}
+ GROUP BY type
+ `,
+ query_params: params,
+ })
+ .then((resultSet) =>
+ resultSet.json<{ type: string; c: string; mn: string; mx: string }>(),
+ )
+
+ const counts: Record<string, number> = {}
+ let total = 0
+ let minCreated: string | null = null
+ let maxCreated: string | null = null
+
+ for (const row of countRows) {
+ const count = Number(row.c) || 0
+ if (count === 0) {
+ continue
+ }
+ counts[row.type] = count
+ total += count
+ if (!minCreated || row.mn < minCreated) {
+ minCreated = row.mn
+ }
+ if (!maxCreated || row.mx > maxCreated) {
+ maxCreated = row.mx
+ }
+ }
+
+ if (total === 0) {
+ return { counts: {}, total: 0, timeline: { x: [], counts: [] } }
+ }
+
+ const timeline = await this.buildDeletionTimeline(
+ whereClause,
+ params,
+ hasRange ? from : minCreated,
+ hasRange ? to : maxCreated,
+ )
+
+ return { counts, total, timeline }
+ }
+
+ private async buildDeletionTimeline(
+ whereClause: string,
+ baseParams: Record<string, unknown>,
+ windowFrom: string,
+ windowTo: string,
+ ): Promise<{ x: string[]; counts: number[] }> {
+ const fromMs = dayjs.utc(windowFrom).valueOf()
+ const toMs = dayjs.utc(windowTo).valueOf()
+ const spanSeconds = Math.max(1, Math.round((toMs - fromMs) / 1000))
+ const bucketSeconds = Math.max(
+ 1,
+ Math.ceil(spanSeconds / DELETION_TIMELINE_BUCKETS),
+ )
+
+ const { data: rows } = await clickhouse
+ .query({
+ query: `
+ SELECT
+ toStartOfInterval(created, INTERVAL {bucketSeconds:UInt32} SECOND) AS t,
+ count() AS c
+ FROM events
+ WHERE ${whereClause}
+ GROUP BY t
+ ORDER BY t
+ `,
+ query_params: { ...baseParams, bucketSeconds },
+ })
+ .then((resultSet) => resultSet.json<{ t: string; c: string }>())
+
+ const bucketed = new Map<string, number>()
+ for (const row of rows) {
+ bucketed.set(row.t, Number(row.c) || 0)
+ }
+
+ // toStartOfInterval aligns buckets to multiples of the interval since the
+ // unix epoch, so align our JS walk the same way and fill empty buckets.
+ const bucketMs = bucketSeconds * 1000
+ const startMs = Math.floor(fromMs / bucketMs) * bucketMs
+ const x: string[] = []
+ const counts: number[] = []
+
+ for (
+ let ms = startMs;
+ ms <= toMs && x.length < DELETION_TIMELINE_MAX_POINTS;
+ ms += bucketMs
+ ) {
+ const key = dayjs.utc(ms).format('YYYY-MM-DD HH:mm:ss')
+ x.push(key)
+ counts.push(bucketed.get(key) ?? 0)
+ }
+
+ return { x, counts }
+ }
+
+ // Permanently removes matching analytics rows. The WHERE clause is identical
+ // to getDataDeletionPreview's, so what the user previews is what gets deleted.
+ async deleteData(
+ pid: string,
+ filters: string,
+ from: string | null,
+ to: string | null,
+ types: string[],
+ ): Promise<void> {
+ const safeTypes = _filter(types, (type) =>
+ _includes(DELETABLE_EVENT_TYPES, type),
+ )
+
+ if (_isEmpty(safeTypes)) {
+ throw new BadRequestException('No event types selected for deletion')
+ }
+
+ const [filtersQuery, filtersParams] = this.getFiltersQuery(
+ filters,
+ 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 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}'
+ }
+
+ 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_params: params,
+ }),
+ ]
+
+ // error_statuses carries no traffic columns, so it can only be cleared when
+ // no field filters are applied (a full reset or a plain date-range reset).
+ 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 },
+ }),
+ )
+ }
+
+ await Promise.all(commands)
+ }
+
derivePsidFromInputs(
pid: string,
userAgent: string,
diff --git a/backend/apps/community/src/analytics/dto/data-deletion.dto.ts b/backend/apps/community/src/analytics/dto/data-deletion.dto.ts
new file mode 100644
index 000000000..75ef20e7b
--- /dev/null
+++ b/backend/apps/community/src/analytics/dto/data-deletion.dto.ts
@@ -0,0 +1,58 @@
+import { ApiProperty } from '@nestjs/swagger'
+import {
+ IsNotEmpty,
+ IsOptional,
+ IsString,
+ IsArray,
+ Matches,
+} from 'class-validator'
+import { PID_REGEX } from '../../common/constants'
+
+export class DataDeletionDto {
+ @ApiProperty({
+ example: 'aUn1quEid-3',
+ required: true,
+ description: 'The project ID',
+ })
+ @IsNotEmpty()
+ @Matches(PID_REGEX, { message: 'The provided Project ID (pid) is incorrect' })
+ pid: string
+
+ @ApiProperty({
+ required: false,
+ description:
+ 'JSON-encoded array of filters, identical to the dashboard filter format. ' +
+ 'Example: [{"column":"cc","filter":"BG","isExclusive":false}]',
+ })
+ @IsOptional()
+ @IsString()
+ filters?: string
+
+ @ApiProperty({
+ required: false,
+ description:
+ 'Start of the date range (inclusive). ISO string or YYYY-MM-DD.',
+ })
+ @IsOptional()
+ @IsString()
+ from?: string
+
+ @ApiProperty({
+ required: false,
+ description: 'End of the date range (inclusive). ISO string or YYYY-MM-DD.',
+ })
+ @IsOptional()
+ @IsString()
+ to?: string
+
+ @ApiProperty({
+ required: false,
+ type: [String],
+ description:
+ "Event types to delete. Any of: 'pageview', 'custom_event', 'error', 'performance', 'captcha'.",
+ })
+ @IsOptional()
+ @IsArray()
+ @IsString({ each: true })
+ types?: string[]
+}
diff --git a/web/app/api/api.server.ts b/web/app/api/api.server.ts
index b055741fd..c4ba053b8 100644
--- a/web/app/api/api.server.ts
+++ b/web/app/api/api.server.ts
@@ -2743,6 +2743,38 @@ export async function getFiltersServer(
})
}
+export interface DataDeletionPreview {
+ // count of matching rows, keyed by event type (pageview, custom_event, ...)
+ counts: Record<string, number>
+ total: number
+ // gap-filled, uniform buckets for the preview sparkline
+ timeline: { x: string[]; counts: number[] }
+}
+
+export async function getDataDeletionPreviewServer(
+ request: Request,
+ pid: string,
+ options: {
+ filters?: AnalyticsFilter[]
+ from?: string
+ to?: string
+ },
+): Promise<ServerFetchResult<DataDeletionPreview>> {
+ return serverFetch<DataDeletionPreview>(
+ request,
+ 'log/data-deletion/preview',
+ {
+ method: 'POST',
+ body: {
+ pid,
+ filters: JSON.stringify(options.filters || []),
+ from: options.from,
+ to: options.to,
+ },
+ },
+ )
+}
+
export async function getErrorsFiltersServer(
request: Request,
pid: string,
diff --git a/web/app/hooks/useAnalyticsProxy.ts b/web/app/hooks/useAnalyticsProxy.ts
index 75cc1b35a..72580d098 100644
--- a/web/app/hooks/useAnalyticsProxy.ts
+++ b/web/app/hooks/useAnalyticsProxy.ts
@@ -34,6 +34,8 @@ import type {
RevenueStatus,
RevenueDataResponse,
OverallObject,
+ AnalyticsFilter,
+ DataDeletionPreview,
AnalyticsParams as ServerAnalyticsParams,
} from '~/api/api.server'
@@ -798,6 +800,33 @@ export function useFiltersProxy() {
return { fetchFilters, fetchErrorsFilters, fetchVersionFilters }
}
+export function useDataDeletionPreviewProxy() {
+ const fetchPreview = useCallback(
+ async (
+ projectId: string,
+ options: {
+ filters?: AnalyticsFilter[]
+ from?: string
+ to?: string
+ },
+ ): Promise<DataDeletionPreview | null> => {
+ const result = await postAnalytics<DataDeletionPreview>({
+ action: 'getDataDeletionPreview',
+ projectId,
+ params: {
+ filters: options.filters,
+ from: options.from,
+ to: options.to,
+ },
+ })
+ return result.data
+ },
+ [],
+ )
+
+ return { fetchPreview }
+}
+
export function useCustomEventsMetadataProxy() {
const fetchMetadata = useCallback(
async (
diff --git a/web/app/pages/Project/Settings/ProjectSettings.tsx b/web/app/pages/Project/Settings/ProjectSettings.tsx
index b14ed8080..eccef8efc 100644
--- a/web/app/pages/Project/Settings/ProjectSettings.tsx
+++ b/web/app/pages/Project/Settings/ProjectSettings.tsx
@@ -1,6 +1,3 @@
-import cx from 'clsx'
-import _filter from 'lodash/filter'
-import _includes from 'lodash/includes'
import _isEmpty from 'lodash/isEmpty'
import _isString from 'lodash/isString'
import _join from 'lodash/join'
@@ -8,7 +5,6 @@ import _keys from 'lodash/keys'
import _map from 'lodash/map'
import _replace from 'lodash/replace'
import _size from 'lodash/size'
-import _toUpper from 'lodash/toUpper'
import { Link } from '~/ui/Link'
import {
SlidersHorizontalIcon,
@@ -37,33 +33,25 @@ import {
} from 'react-router'
import { toast } from 'sonner'
-import { useFiltersProxy } from '~/hooks/useAnalyticsProxy'
import { useDeduplicateFetcherResponse } from '~/hooks/useDeduplicateFetcherResponse'
import { useRequiredParams } from '~/hooks/useRequiredParams'
-import { isSelfhosted, FILTERS_PANELS_ORDER, isBrowser } from '~/lib/constants'
+import { isSelfhosted, isBrowser } from '~/lib/constants'
import { Project } from '~/lib/models/Project'
import { useAuth } from '~/providers/AuthProvider'
import type { ProjectSettingsActionData } from '~/routes/projects.settings.$id'
import Button from '~/ui/Button'
-import DatePicker from '~/ui/Datepicker'
-import Dropdown from '~/ui/Dropdown'
import GoogleGSVG from '~/ui/icons/GoogleG'
import GoogleSearchConsoleSVG from '~/ui/icons/GoogleSearchConsole'
import HoldToConfirmButton from '~/ui/HoldToConfirmButton'
import Input from '~/ui/Input'
import Loader from '~/ui/Loader'
import Modal from '~/ui/Modal'
-import MultiSelect from '~/ui/MultiSelect'
import Select from '~/ui/Select'
import { TabHeader } from '~/ui/TabHeader'
import { Text } from '~/ui/Text'
-// Select is used inside tab components
-import countries from '~/utils/isoCountries'
import routes from '~/utils/routes'
import { isValidHttpUrl } from '~/utils/url'
-import CCRow from '../View/components/CCRow'
-
import Annotations from './Annotations'
import Emails from './Emails'
import People from './People'
@@ -76,6 +64,7 @@ import Shields from './tabs/Shields'
import ProjectAlerts from './Alerts/ProjectAlertsView'
import NotificationChannels from '~/components/NotificationChannels/NotificationChannels'
import BotProtectionReport from './components/BotProtectionReport'
+import DeleteDataModal from './components/DeleteDataModal'
import DataImportTab from './components/DataImportTab'
import ProxyDomainsTab from './components/ProxyDomainsTab'
import SettingsSidebar, { SettingsTabConfig } from './SettingsSidebar'
@@ -87,241 +76,6 @@ const AUTOSAVE_DEBOUNCE_MS = 700
const CAPTCHA_CLIENT_DOCS_URL =
'https://swetrix.com/docs/captcha/client-side-usage'
-const DELETE_DATA_MODAL_TABS = [
- {
- name: 'all',
- title: 'project.settings.reseted.all',
- },
- {
- name: 'partially',
- title: 'project.settings.reseted.partially',
- },
- {
- name: 'viaFilters',
- title: 'project.settings.reseted.viaFilters',
- },
-]
-
-interface ModalMessageProps {
- dateRange: Date[]
- setDateRange: (a: Date[]) => void
- setTab: (i: string) => void
- tab: string
- pid: string
- activeFilter: string[]
- setActiveFilter: any
- filterType: string
- setFilterType: (a: string) => void
- fetchFilters: (
- projectId: string,
- filterType: string,
- ) => Promise<string[] | null>
-}
-
-const ModalMessage = ({
- dateRange,
- setDateRange,
- setTab,
- tab,
- pid,
- activeFilter,
- setActiveFilter,
- filterType,
- setFilterType,
- fetchFilters,
-}: ModalMessageProps) => {
- const {
- t,
- i18n: { language },
- } = useTranslation('common')
- const [filterList, setFilterList] = useState<string[]>([])
- const [searchList, setSearchList] = useState<string[]>([])
-
- const getFiltersList = async () => {
- if (!_isEmpty(filterType)) {
- const res = await fetchFilters(pid, filterType)
- setFilterList(res || [])
- setSearchList(res || [])
- if (!_isEmpty(activeFilter)) {
- setActiveFilter([])
- }
- }
- }
-
- useEffect(() => {
- getFiltersList()
- // eslint-disable-next-line react-hooks/exhaustive-deps
- }, [filterType])
-
- return (
- <>
- <Text
- as='p'
- size='sm'
- className='mt-1 mb-4 text-gray-500 italic dark:text-gray-300'
- >
- {t('project.settings.resetHint')}
- </Text>
- <div className='mt-6'>
- <nav className='-mb-px flex space-x-6'>
- {_map(DELETE_DATA_MODAL_TABS, (tabDelete) => (
- <button
- key={tabDelete.name}
- type='button'
- onClick={() => setTab(tabDelete.name)}
- className={cx(
- 'text-md border-b-2 px-1 pb-2 font-medium whitespace-nowrap',
- {
- 'border-indigo-500 text-indigo-600 dark:border-gray-50 dark:text-gray-50':
- tabDelete.name === tab,
- 'border-transparent text-gray-500 hover:border-gray-300 hover:text-gray-700 dark:text-gray-400 dark:hover:border-gray-300 dark:hover:text-gray-300':
- tab !== tabDelete.name,
- },
- )}
- >
- {t(tabDelete.title)}
- </button>
- ))}
- </nav>
- </div>
- {tab === DELETE_DATA_MODAL_TABS[1].name ? (
- <>
- <Text
- as='p'
- size='sm'
- className='mt-4 mb-2 text-gray-500 dark:text-gray-300'
- >
- {t('project.settings.reseted.partiallyDesc')}
- </Text>
- <Text
- as='p'
- size='sm'
- className='mt-1 mb-2 text-gray-500 italic dark:text-gray-300'
- >
- {t('project.settings.reseted.partiallyHint')}
- </Text>
- <input
- type='text'
- aria-label={t('ariaLabels.dateRange')}
- className='m-0 h-0 w-0 border-0 p-0 focus:border-transparent focus:text-transparent focus:shadow-none focus:ring-transparent'
- />
- <DatePicker
- className='!mx-0 w-0'
- onChange={(date) => setDateRange(date)}
- options={{
- altInputClass:
- ' focus:ring-slate-900 dark:focus:ring-slate-300 block w-full sm:text-sm border-gray-300 dark:text-gray-50 dark:placeholder-gray-400 dark:border-gray-800 dark:bg-slate-900 rounded-md',
- }}
- value={dateRange}
- />
- </>
- ) : null}
- {tab === DELETE_DATA_MODAL_TABS[0].name ? (
- <Text
- as='p'
- size='sm'
- className='mt-4 mb-4 text-gray-500 italic dark:text-gray-300'
- >
- {t('project.settings.reseted.allHint')}
- </Text>
- ) : null}
- {tab === DELETE_DATA_MODAL_TABS[2].name ? (
- <div className='min-h-[410px]'>
- <Text
- as='p'
- size='sm'
- className='mt-4 mb-4 text-gray-500 italic dark:text-gray-300'
- >
- {t('project.settings.reseted.viaFiltersHint')}
- </Text>
- <div>
- <Dropdown
- className='min-w-[160px]'
- title={
- !_isEmpty(filterType)
- ? t(`project.mapping.${filterType}`)
- : t('project.settings.reseted.selectFilters')
- }
- items={FILTERS_PANELS_ORDER}
- labelExtractor={(item) => t(`project.mapping.${item}`)}
- keyExtractor={(item) => item}
- onSelect={(item) => setFilterType(item)}
- />
- <div className='h-2' />
- {filterType && !_isEmpty(filterList) ? (
- <MultiSelect
- className='max-w-96'
- items={searchList}
- labelExtractor={(item) => {
- if (filterType === 'cc') {
- return <CCRow cc={item} language={language} />
- }
-
- return item
- }}
- itemExtractor={(item) => {
- if (filterType === 'cc') {
- return <CCRow cc={item} language={language} />
- }
-
- return item
- }}
- keyExtractor={(item) => item}
- label={activeFilter}
- onSearch={(search: string) => {
- if (search.length > 0) {
- if (filterType === 'cc') {
- setSearchList(
- _filter(filterList, (item) =>
- _includes(
- _toUpper(countries.getName(item, language)),
- _toUpper(search),
- ),
- ),
- )
- return
- }
-
- setSearchList(
- _filter(filterList, (item) =>
- _includes(_toUpper(item), _toUpper(search)),
- ),
- )
- } else {
- setSearchList(filterList)
- }
- }}
- placeholder={t('project.settings.reseted.filtersPlaceholder')}
- onSelect={(item: string) =>
- setActiveFilter((oldItems: string[]) => {
- if (_includes(oldItems, item)) {
- return _filter(oldItems, (i) => i !== item)
- }
- return [...oldItems, item]
- })
- }
- onRemove={(item: string) =>
- setActiveFilter((oldItems: string[]) =>
- _filter(oldItems, (i) => i !== item),
- )
- }
- />
- ) : (
- <Text
- as='p'
- size='sm'
- className='mt-4 mb-4 text-gray-500 italic dark:text-gray-300'
- >
- {t('project.settings.reseted.noFilters')}
- </Text>
- )}
- </div>
- </div>
- ) : null}
- </>
- )
-}
-
interface Form extends Partial<Omit<Project, 'brandKeywords'>> {
origins: string | null
ipBlacklist: string | null
@@ -426,7 +180,6 @@ const ProjectSettings = () => {
const fetcher = useFetcher<ProjectSettingsActionData>()
const autosaveFetcher = useFetcher<ProjectSettingsActionData>()
const gscFetcher = useFetcher<ProjectSettingsActionData>()
- const { fetchFilters } = useFiltersProxy()
const [project, setProject] = useState<Project>(initialProject)
const [form, setForm] = useState<Form>(() =>
@@ -447,11 +200,8 @@ const ProjectSettings = () => {
const [showDelete, setShowDelete] = useState(false)
const [showReset, setShowReset] = useState(false)
const [isDeleting, setIsDeleting] = useState(false)
- const [isResetting, setIsResetting] = useState(false)
const [showTransfer, setShowTransfer] = useState(false)
const [transferEmail, setTransferEmail] = useState('')
- const [dateRange, setDateRange] = useState<Date[]>([])
- const [tab, setTab] = useState(DELETE_DATA_MODAL_TABS[0].name)
const [showProtected, setShowProtected] = useState(false)
const lastSavedForm = useRef<Form>(getFormFromProject(initialProject))
const lastHandledAutosaveData = useRef<ProjectSettingsActionData | null>(null)
@@ -698,10 +448,6 @@ const ProjectSettings = () => {
)
}, [captchaDifficulty, captchaDifficultyItems, captchaDifficultyMode])
- // for reset data via filters
- const [activeFilter, setActiveFilter] = useState<string[]>([])
- const [filterType, setFilterType] = useState('')
-
const botsProtectionLevels = useMemo(() => {
return [
{
@@ -890,13 +636,6 @@ const ProjectSettings = () => {
} else if (intent === 'delete-project') {
toast.success(t('project.settings.deleted'))
navigate(routes.dashboard)
- } else if (
- intent === 'reset-project' ||
- intent === 'delete-partially' ||
- intent === 'reset-filters'
- ) {
- toast.success(t('project.settings.resetSuccess'))
- setShowReset(false)
} else if (intent === 'transfer-project') {
toast.success(t('apiNotifications.transferRequestSent'))
setShowTransfer(false)
@@ -918,15 +657,12 @@ const ProjectSettings = () => {
}
setIsDeleting(false)
- setIsResetting(false)
} else if (fetcher.data?.fieldErrors) {
setErrors(fetcher.data.fieldErrors)
setIsDeleting(false)
- setIsResetting(false)
} else if (fetcher.data?.error) {
toast.error(fetcher.data.error)
setIsDeleting(false)
- setIsResetting(false)
}
}, [fetcher.data, t, navigate, shouldHandleFetcherData])
@@ -941,37 +677,6 @@ const ProjectSettings = () => {
fetcher.submit(formData, { method: 'post' })
}
- const onReset = (
- resetTab: string,
- dateRange?: Date[],
- filterType?: string,
- filterValue?: string[],
- ) => {
- if (fetcher.state === 'submitting') return
-
- setIsResetting(true)
- const formData = new FormData()
-
- if (resetTab === 'all') {
- formData.set('intent', 'reset-project')
- } else if (resetTab === 'partially' && dateRange) {
- formData.set('intent', 'delete-partially')
- formData.set('from', dateRange[0]?.toISOString() || '')
- formData.set('to', dateRange[1]?.toISOString() || '')
- } else if (
- resetTab === 'viaFilters' &&
- filterType &&
- filterValue &&
- filterValue.length > 0
- ) {
- formData.set('intent', 'reset-filters')
- formData.set('type', filterType)
- formData.set('value', JSON.stringify(filterValue))
- }
-
- fetcher.submit(formData, { method: 'post' })
- }
-
const onTransfer = () => {
if (fetcher.state === 'submitting') return
@@ -990,29 +695,6 @@ const ProjectSettings = () => {
fetcher.submit(formData, { method: 'post' })
}
- const handleReset = () => {
- if (fetcher.state === 'submitting') return
-
- if (tab === DELETE_DATA_MODAL_TABS[1].name) {
- if (_isEmpty(dateRange) || !dateRange[0] || !dateRange[1]) {
- toast.error(t('project.settings.noDateRange'))
- return
- }
- setShowReset(false)
- onReset('partially', dateRange)
- } else if (tab === DELETE_DATA_MODAL_TABS[2].name) {
- if (_isEmpty(activeFilter) || _isEmpty(filterType)) {
- toast.error(t('project.settings.noFilters'))
- return
- }
- setShowReset(false)
- onReset('viaFilters', undefined, filterType, activeFilter)
- } else if (tab === DELETE_DATA_MODAL_TABS[0].name) {
- setShowReset(false)
- onReset('all')
- }
- }
-
const getValidationErrors = useCallback(
(data: Form) => {
const allErrors: {
@@ -1861,7 +1543,7 @@ const ProjectSettings = () => {
setShowReset={setShowReset}
setShowDelete={setShowDelete}
isDeleting={isDeleting}
- setResetting={isResetting}
+ setResetting={false}
/>
</form>
) : null}
@@ -1884,35 +1566,10 @@ const ProjectSettings = () => {
type='error'
isOpened={showDelete}
/>
- <Modal
+ <DeleteDataModal
+ pid={id}
+ isOpen={showReset}
onClose={() => setShowReset(false)}
- customButtons={
- <HoldToConfirmButton
- onConfirm={handleReset}
- className='w-full justify-center sm:w-auto'
- >
- {t('common.holdToReset')}
- </HoldToConfirmButton>
- }
- size='large'
- closeText={t('common.close')}
- title={t('project.settings.qReset')}
- message={
- <ModalMessage
- setDateRange={setDateRange}
- dateRange={dateRange}
- setTab={setTab}
- tab={tab}
- pid={id}
- activeFilter={activeFilter}
- setActiveFilter={setActiveFilter}
- filterType={filterType}
- setFilterType={setFilterType}
- fetchFilters={fetchFilters}
- />
- }
- type='error'
- isOpened={showReset}
/>
<Modal
onClose={() => {
diff --git a/web/app/pages/Project/Settings/components/DeleteDataModal.tsx b/web/app/pages/Project/Settings/components/DeleteDataModal.tsx
new file mode 100644
index 000000000..42e455844
--- /dev/null
+++ b/web/app/pages/Project/Settings/components/DeleteDataModal.tsx
@@ -0,0 +1,454 @@
+import dayjs from 'dayjs'
+import { useEffect, useMemo, useRef, useState } from 'react'
+import { useTranslation } from 'react-i18next'
+import { useFetcher } from 'react-router'
+import { toast } from 'sonner'
+
+import type { DataDeletionPreview } from '~/api/api.server'
+import { useDataDeletionPreviewProxy } from '~/hooks/useAnalyticsProxy'
+import type { ProjectSettingsActionData } from '~/routes/projects.settings.$id'
+import DatePicker from '~/ui/Datepicker'
+import HoldToConfirmButton from '~/ui/HoldToConfirmButton'
+import Modal from '~/ui/Modal'
+import { cn } from '~/utils/generic'
+
+import FilterRowsEditor from '../../View/components/FilterRowsEditor'
+import type { Filter } from '../../View/interfaces/traffic'
+
+const EMPTY_TN_MAPPING: Record<string, string> = {}
+
+type PeriodKey = 'all' | '24h' | '7d' | '30d' | 'custom'
+
+const PERIODS: { key: PeriodKey; labelKey: string }[] = [
+ { key: 'all', labelKey: 'project.settings.deleteData.periods.all' },
+ { key: '24h', labelKey: 'project.settings.deleteData.periods.24h' },
+ { key: '7d', labelKey: 'project.settings.deleteData.periods.7d' },
+ { key: '30d', labelKey: 'project.settings.deleteData.periods.30d' },
+ { key: 'custom', labelKey: 'project.settings.deleteData.periods.custom' },
+]
+
+// Order mirrors how prominent each type is for a typical cleanup.
+const EVENT_TYPES: { key: string; labelKey: string }[] = [
+ { key: 'pageview', labelKey: 'project.settings.deleteData.types.pageview' },
+ {
+ key: 'custom_event',
+ labelKey: 'project.settings.deleteData.types.custom_event',
+ },
+ { key: 'error', labelKey: 'project.settings.deleteData.types.error' },
+ {
+ key: 'performance',
+ labelKey: 'project.settings.deleteData.types.performance',
+ },
+ { key: 'captcha', labelKey: 'project.settings.deleteData.types.captcha' },
+]
+
+const DEFAULT_TYPES = ['pageview', 'custom_event']
+
+interface DateRange {
+ from: string | null
+ to: string | null
+ ready: boolean
+}
+
+const computeRange = (period: PeriodKey, customRange: Date[]): DateRange => {
+ if (period === 'all') {
+ return { from: null, to: null, ready: true }
+ }
+
+ if (period === 'custom') {
+ if (customRange.length === 2 && customRange[0] && customRange[1]) {
+ return {
+ from: dayjs(customRange[0]).startOf('day').toISOString(),
+ to: dayjs(customRange[1]).endOf('day').toISOString(),
+ ready: true,
+ }
+ }
+ return { from: null, to: null, ready: false }
+ }
+
+ const days = period === '24h' ? 1 : period === '7d' ? 7 : 30
+ return {
+ from: dayjs().subtract(days, 'day').toISOString(),
+ to: dayjs().toISOString(),
+ ready: true,
+ }
+}
+
+// Lightweight, dependency-free sparkline of the matching events over time.
+// Bars read as "the data you're about to delete", so they're tinted red.
+const MatchingTimeline = ({
+ timeline,
+ loading,
+ language,
+}: {
+ timeline: DataDeletionPreview['timeline'] | undefined
+ loading: boolean
+ language: string
+}) => {
+ const counts = timeline?.counts ?? []
+ const max = counts.length ? Math.max(...counts) : 0
+
+ return (
+ <div
+ className={cn(
+ 'flex h-16 items-end gap-px border-b border-gray-200 transition-opacity duration-200 dark:border-slate-800',
+ loading && counts.length ? 'opacity-40' : 'opacity-100',
+ )}
+ >
+ {counts.length === 0 ? (
+ <div className='h-full w-full' />
+ ) : (
+ counts.map((count, index) => {
+ const ratio = max > 0 ? count / max : 0
+ const label =
+ timeline?.x[index] &&
+ `${dayjs(timeline.x[index]).format('MMM D, HH:mm')} · ${count.toLocaleString(language)}`
+
+ return (
+ <div
+ key={timeline?.x[index] ?? index}
+ className='relative h-full flex-1'
+ title={label || undefined}
+ >
+ <div
+ className='absolute inset-x-0 bottom-0 rounded-sm bg-red-400/80 transition-[height] duration-200 ease-out dark:bg-red-500/70'
+ style={{
+ height: count > 0 ? `max(2px, ${ratio * 100}%)` : '0px',
+ }}
+ />
+ </div>
+ )
+ })
+ )}
+ </div>
+ )
+}
+
+interface DeleteDataModalProps {
+ pid: string
+ isOpen: boolean
+ onClose: () => void
+}
+
+const DeleteDataModal = ({ pid, isOpen, onClose }: DeleteDataModalProps) => {
+ const {
+ t,
+ i18n: { language },
+ } = useTranslation('common')
+
+ const [filters, setFilters] = useState<Filter[]>([])
+ const [period, setPeriod] = useState<PeriodKey>('all')
+ const [customRange, setCustomRange] = useState<Date[]>([])
+ // null = follow the auto default (every type that has matching rows)
+ const [explicitTypes, setExplicitTypes] = useState<Set<string> | null>(null)
+
+ const [preview, setPreview] = useState<DataDeletionPreview | null>(null)
+ const [previewLoading, setPreviewLoading] = useState(false)
+ const [previewError, setPreviewError] = useState(false)
+
+ const { fetchPreview } = useDataDeletionPreviewProxy()
+ const fetcher = useFetcher<ProjectSettingsActionData>()
+ const submitting = fetcher.state !== 'idle'
+ const handledFetcher = useRef<ProjectSettingsActionData | null>(null)
+
+ const range = useMemo(
+ () => computeRange(period, customRange),
+ [period, customRange],
+ )
+
+ // Reset transient state every time the modal is opened.
+ useEffect(() => {
+ if (isOpen) {
+ setPeriod('all')
+ setCustomRange([])
+ setExplicitTypes(null)
+ setPreview(null)
+ setPreviewError(false)
+ }
+ }, [isOpen])
+
+ // Debounced live preview that mirrors the deletion query exactly.
+ useEffect(() => {
+ if (!isOpen || !range.ready) {
+ return
+ }
+
+ let cancelled = false
+ setPreviewLoading(true)
+
+ const handle = setTimeout(() => {
+ fetchPreview(pid, {
+ filters,
+ from: range.from ?? undefined,
+ to: range.to ?? undefined,
+ })
+ .then((result) => {
+ if (cancelled) return
+ setPreview(result)
+ setPreviewError(false)
+ })
+ .catch(() => {
+ if (cancelled) return
+ setPreview(null)
+ setPreviewError(true)
+ })
+ .finally(() => {
+ if (!cancelled) setPreviewLoading(false)
+ })
+ }, 400)
+
+ return () => {
+ cancelled = true
+ clearTimeout(handle)
+ }
+ }, [isOpen, pid, filters, range.ready, range.from, range.to, fetchPreview])
+
+ // Success / error handling for the destructive submit.
+ useEffect(() => {
+ if (!fetcher.data || fetcher.data === handledFetcher.current) {
+ return
+ }
+
+ if (fetcher.data.intent !== 'delete-data') {
+ return
+ }
+
+ handledFetcher.current = fetcher.data
+
+ if (fetcher.data.success) {
+ toast.success(t('project.settings.deleteData.started'))
+ onClose()
+ } else if (fetcher.data.error) {
+ toast.error(t('project.settings.deleteData.error'))
+ }
+ }, [fetcher.data, onClose, t])
+
+ const countFor = (type: string) => preview?.counts?.[type] ?? 0
+
+ // Until the user touches a chip, default to every type that actually matches.
+ const autoTypes = useMemo(() => {
+ const matching = EVENT_TYPES.filter((type) => countFor(type.key) > 0).map(
+ (type) => type.key,
+ )
+ return new Set(matching.length ? matching : DEFAULT_TYPES)
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ }, [preview])
+
+ const selectedTypes = explicitTypes ?? autoTypes
+
+ const toggleType = (type: string) => {
+ setExplicitTypes((prev) => {
+ const next = new Set(prev ?? autoTypes)
+ if (next.has(type)) {
+ next.delete(type)
+ } else {
+ next.add(type)
+ }
+ return next
+ })
+ }
+
+ const selectedTotal = EVENT_TYPES.reduce(
+ (sum, type) =>
+ selectedTypes.has(type.key) ? sum + countFor(type.key) : sum,
+ 0,
+ )
+
+ const hasAnyMatch = (preview?.total ?? 0) > 0
+ const canDelete =
+ range.ready && !submitting && selectedTotal > 0 && !previewError
+
+ const handleConfirm = () => {
+ if (!canDelete) {
+ if (!range.ready) {
+ toast.error(t('project.settings.deleteData.customRangePrompt'))
+ } else if (selectedTotal === 0) {
+ toast.error(t('project.settings.deleteData.selectType'))
+ }
+ return
+ }
+
+ const formData = new FormData()
+ formData.set('intent', 'delete-data')
+ formData.set('filters', JSON.stringify(filters))
+ formData.set('types', JSON.stringify([...selectedTypes]))
+ if (range.from) formData.set('from', range.from)
+ if (range.to) formData.set('to', range.to)
+
+ fetcher.submit(formData, { method: 'post' })
+ }
+
+ const rangeSummary = useMemo(() => {
+ if (period === 'all') {
+ return t('project.settings.deleteData.summaryAllTime')
+ }
+ if (range.from && range.to) {
+ return t('project.settings.deleteData.summaryRange', {
+ from: dayjs(range.from).format('MMM D, YYYY'),
+ to: dayjs(range.to).format('MMM D, YYYY'),
+ })
+ }
+ return ''
+ }, [period, range.from, range.to, t])
+
+ return (
+ <Modal
+ type='error'
+ size='medium'
+ overflowVisible
+ isOpened={isOpen}
+ onClose={onClose}
+ closeText={t('common.close')}
+ title={t('project.settings.deleteData.title')}
+ customButtons={
+ <HoldToConfirmButton
+ onConfirm={handleConfirm}
+ disabled={!canDelete}
+ className='w-full justify-center sm:w-auto'
+ >
+ {selectedTotal > 0
+ ? t('project.settings.deleteData.holdToDeleteCount', {
+ total: selectedTotal.toLocaleString(language),
+ })
+ : t('common.holdToDelete')}
+ </HoldToConfirmButton>
+ }
+ message={
+ <div className='space-y-5'>
+ <p className='text-sm text-gray-500 dark:text-gray-400'>
+ {t('project.settings.deleteData.intro')}
+ </p>
+
+ {/* Date range */}
+ <div className='space-y-2'>
+ <p className='text-xs font-medium tracking-wide text-gray-500 uppercase dark:text-gray-400'>
+ {t('project.settings.deleteData.dateRange')}
+ </p>
+ <div className='inline-flex flex-wrap gap-1 rounded-lg bg-gray-100 p-1 dark:bg-slate-900'>
+ {PERIODS.map(({ key, labelKey }) => (
+ <button
+ key={key}
+ type='button'
+ onClick={() => setPeriod(key)}
+ className={cn(
+ 'rounded-md px-3 py-1.5 text-sm font-medium transition-colors active:scale-[0.98]',
+ period === key
+ ? 'bg-white text-gray-900 shadow-sm dark:bg-slate-700 dark:text-gray-50'
+ : 'text-gray-600 hover:text-gray-900 dark:text-gray-400 dark:hover:text-gray-200',
+ )}
+ >
+ {t(labelKey)}
+ </button>
+ ))}
+ </div>
+ {period === 'custom' ? (
+ <div className='pt-1'>
+ <DatePicker
+ mode='range'
+ onChange={(dates) => setCustomRange(dates)}
+ value={customRange}
+ />
+ </div>
+ ) : null}
+ </div>
+
+ {/* Filters */}
+ <div className='space-y-2'>
+ <p className='text-xs font-medium tracking-wide text-gray-500 uppercase dark:text-gray-400'>
+ {t('project.settings.deleteData.matching')}
+ </p>
+ <FilterRowsEditor
+ active={isOpen}
+ projectId={pid}
+ type='traffic'
+ tnMapping={EMPTY_TN_MAPPING}
+ onChange={setFilters}
+ />
+ <p className='text-xs text-gray-400 dark:text-gray-500'>
+ {t('project.settings.deleteData.filtersHint')}
+ </p>
+ </div>
+
+ {/* Preview */}
+ <div className='rounded-lg border border-gray-200 bg-gray-50/60 p-4 dark:border-slate-800 dark:bg-slate-900/40'>
+ <MatchingTimeline
+ timeline={preview?.timeline}
+ loading={previewLoading}
+ language={language}
+ />
+
+ <div className='mt-3 flex items-baseline gap-2'>
+ {previewError ? (
+ <span className='text-sm text-red-600 dark:text-red-400'>
+ {t('project.settings.deleteData.previewError')}
+ </span>
+ ) : !range.ready ? (
+ <span className='text-sm text-gray-500 dark:text-gray-400'>
+ {t('project.settings.deleteData.customRangePrompt')}
+ </span>
+ ) : preview && !hasAnyMatch ? (
+ <span className='text-sm text-gray-500 dark:text-gray-400'>
+ {t('project.settings.deleteData.noMatch')}
+ </span>
+ ) : !preview ? (
+ <span className='text-sm text-gray-400 dark:text-gray-500'>
+ {t('project.settings.deleteData.calculating')}
+ </span>
+ ) : (
+ <>
+ <span
+ className={cn(
+ 'text-2xl font-bold text-red-600 tabular-nums transition-opacity dark:text-red-400',
+ previewLoading && !preview ? 'opacity-40' : 'opacity-100',
+ )}
+ >
+ {selectedTotal.toLocaleString(language)}
+ </span>
+ <span className='text-sm text-gray-500 dark:text-gray-400'>
+ {t('project.settings.deleteData.willDelete')}
+ {rangeSummary ? ` · ${rangeSummary}` : ''}
+ </span>
+ </>
+ )}
+ </div>
+
+ {/* Per-type breakdown / toggles */}
+ {preview && hasAnyMatch ? (
+ <div className='mt-3 flex flex-wrap gap-1.5'>
+ {EVENT_TYPES.map((type) => {
+ const count = countFor(type.key)
+ const isSelected = selectedTypes.has(type.key)
+ const isEmpty = count === 0
+
+ return (
+ <button
+ key={type.key}
+ type='button'
+ onClick={() => toggleType(type.key)}
+ disabled={isEmpty}
+ className={cn(
+ 'inline-flex items-center gap-1.5 rounded-full border px-2.5 py-1 text-xs font-medium transition-colors active:scale-[0.97]',
+ isEmpty
+ ? 'cursor-not-allowed border-gray-200 text-gray-300 dark:border-slate-800 dark:text-slate-600'
+ : isSelected
+ ? 'border-red-300 bg-red-50 text-red-700 dark:border-red-500/40 dark:bg-red-500/10 dark:text-red-300'
+ : 'border-gray-200 text-gray-500 hover:border-gray-300 dark:border-slate-700 dark:text-gray-400 dark:hover:border-slate-600',
+ )}
+ >
+ {t(type.labelKey)}
+ <span className='tabular-nums opacity-70'>
+ {count.toLocaleString(language)}
+ </span>
+ </button>
+ )
+ })}
+ </div>
+ ) : null}
+ </div>
+ </div>
+ }
+ />
+ )
+}
+
+export default DeleteDataModal
diff --git a/web/app/pages/Project/View/components/FilterRowsEditor.tsx b/web/app/pages/Project/View/components/FilterRowsEditor.tsx
index c1b8208b9..8169bf07f 100644
--- a/web/app/pages/Project/View/components/FilterRowsEditor.tsx
+++ b/web/app/pages/Project/View/components/FilterRowsEditor.tsx
@@ -23,7 +23,7 @@ import {
FILTERS_PANELS_ORDER,
ERRORS_FILTERS_PANELS_ORDER,
} from '~/lib/constants'
-import { useCurrentProject } from '~/providers/CurrentProjectProvider'
+import { useOptionalCurrentProject } from '~/providers/CurrentProjectProvider'
import { useTheme } from '~/providers/ThemeProvider'
import FilterValueInput, {
filterCategoryIcons,
@@ -56,6 +56,9 @@ interface FilterRowsEditorProps {
resetKey?: string
prefetchOnOpen?: boolean
showCurrentFilters?: boolean
+ // Explicit project id for use outside CurrentProjectProvider (e.g. Settings).
+ // Falls back to the surrounding provider's project when omitted.
+ projectId?: string
}
const EMPTY_FILTERS: FilterType[] = []
@@ -293,8 +296,10 @@ const FilterRowsEditor = ({
resetKey,
prefetchOnOpen,
showCurrentFilters,
+ projectId,
}: FilterRowsEditorProps) => {
- const { id } = useCurrentProject()
+ const currentProject = useOptionalCurrentProject()
+ const id = projectId ?? currentProject?.id ?? ''
const { theme } = useTheme()
const {
t,
diff --git a/web/app/providers/CurrentProjectProvider.tsx b/web/app/providers/CurrentProjectProvider.tsx
index fbfaecb40..47a4a3c47 100644
--- a/web/app/providers/CurrentProjectProvider.tsx
+++ b/web/app/providers/CurrentProjectProvider.tsx
@@ -362,3 +362,8 @@ export const useCurrentProject = () => {
return context
}
+
+// Non-throwing variant for components that may render outside the provider
+// (e.g. reused inside the project Settings page). Returns undefined when there
+// is no surrounding CurrentProjectProvider.
+export const useOptionalCurrentProject = () => useContext(CurrentProjectContext)
diff --git a/web/app/routes/api.analytics.ts b/web/app/routes/api.analytics.ts
index cb7cfe956..d24bd1b62 100644
--- a/web/app/routes/api.analytics.ts
+++ b/web/app/routes/api.analytics.ts
@@ -27,6 +27,7 @@ import {
getProfileSessionsServer,
getProjectServer,
getFiltersServer,
+ getDataDeletionPreviewServer,
getErrorsFiltersServer,
getVersionFiltersServer,
getCustomEventsMetadataServer,
@@ -82,6 +83,7 @@ import {
type RevenueStatus,
type RevenueDataResponse,
type OverallObject,
+ type DataDeletionPreview,
} from '~/api/api.server'
import { getProjectPasswordCookie } from '~/utils/session.server'
@@ -122,6 +124,7 @@ interface ProxyRequest {
| 'getProfileSessions'
| 'getProject'
| 'getFilters'
+ | 'getDataDeletionPreview'
| 'getErrorsFilters'
| 'getVersionFilters'
| 'getCustomEventsMetadata'
@@ -708,6 +711,22 @@ export async function action({ request }: ActionFunctionArgs) {
})
}
+ case 'getDataDeletionPreview': {
+ const result = await getDataDeletionPreviewServer(request, projectId, {
+ filters: params.filters || [],
+ from: formatDateForBackend(params.from),
+ to: formatDateForBackend(params.to),
+ })
+ return data<ProxyResponse<DataDeletionPreview>>({
+ data: result.data,
+ error: result.error
+ ? Array.isArray(result.error)
+ ? result.error.join(', ')
+ : result.error
+ : null,
+ })
+ }
+
case 'getErrorsFilters': {
if (!body.filterType) {
return data<ProxyResponse<null>>(
diff --git a/web/app/routes/projects.settings.$id.tsx b/web/app/routes/projects.settings.$id.tsx
index a7ff556aa..67f4f3757 100644
--- a/web/app/routes/projects.settings.$id.tsx
+++ b/web/app/routes/projects.settings.$id.tsx
@@ -237,31 +237,24 @@ export async function action({ request, params }: ActionFunctionArgs) {
)
}
- case 'reset-project': {
- const result = await serverFetch(request, `project/reset/${id}`, {
- method: 'DELETE',
- })
-
- if (result.error) {
- return data<ProjectSettingsActionData>(
- { intent, error: result.error as string },
- { status: 400 },
- )
+ case 'delete-data': {
+ // Unified analytics-data deletion: optional filters (dashboard format,
+ // JSON-encoded), optional date range, and the event types to remove.
+ const filters = formData.get('filters')?.toString() || '[]'
+ const from = formData.get('from')?.toString() || undefined
+ const to = formData.get('to')?.toString() || undefined
+ const typesRaw = formData.get('types')?.toString()
+
+ let types: string[] | undefined
+ try {
+ types = typesRaw ? (JSON.parse(typesRaw) as string[]) : undefined
+ } catch {
+ types = undefined
}
- return data<ProjectSettingsActionData>(
- { intent, success: true },
- { headers: createHeadersWithCookies(result.cookies) },
- )
- }
-
- case 'delete-partially': {
- const from = formData.get('from')?.toString()
- const to = formData.get('to')?.toString()
-
- const result = await serverFetch(request, `project/partially/${id}`, {
- method: 'DELETE',
- body: { from, to },
+ const result = await serverFetch(request, 'log/data-deletion', {
+ method: 'POST',
+ body: { pid: id, filters, from, to, types },
})
if (result.error) {
@@ -277,31 +270,6 @@ export async function action({ request, params }: ActionFunctionArgs) {
)
}
- case 'reset-filters': {
- const type = formData.get('type')?.toString()
- const filters = formData.get('value')?.toString()
-
- const result = await serverFetch(
- request,
- `project/reset-filters/${id}?type=${encodeURIComponent(type || '')}&filters=${encodeURIComponent(filters || '')}`,
- {
- method: 'DELETE',
- },
- )
-
- if (result.error) {
- return data<ProjectSettingsActionData>(
- { intent, error: result.error as string },
- { status: 400 },
- )
- }
-
- return data<ProjectSettingsActionData>(
- { intent, success: true },
- { headers: createHeadersWithCookies(result.cookies) },
- )
- }
-
case 'transfer-project': {
const email = formData.get('email')?.toString() || ''
diff --git a/web/public/locales/en.json b/web/public/locales/en.json
index 6abfa0fe9..fb6984fc5 100644
--- a/web/public/locales/en.json
+++ b/web/public/locales/en.json
@@ -89,7 +89,6 @@
"continue": "Continue",
"confirm": "Confirm",
"holdToDelete": "Hold to delete",
- "holdToReset": "Hold to reset",
"amount": "Amount",
"date": "Date",
"info": "Info",
@@ -2253,7 +2252,6 @@
"blockedTraffic": "Blocked traffic",
"blockedTrafficHint": "Insights into the bot requests we filtered out before they reached your dashboard.",
"access": "Access settings",
- "resetSuccess": "The project has been reset",
"gsc": {
"connect": "Connect Google Search Console to see which search queries bring people to your website.",
"disconnected": "Google Search Console has been disconnected from this project.",
@@ -2572,9 +2570,7 @@
"delete": "Delete project",
"reset": "Reset project",
"qDelete": "Delete this project?",
- "qReset": "Reset this project?",
"deleteHint": "Deleting this project is permanent. We will delete the project and all related data from our servers. No project data will be kept, and this cannot be undone.",
- "resetHint": "Resetting this project is permanent. We will delete its pageviews and custom events from our servers. The project ID and settings will be kept.",
"deleted": "Project deleted",
"projectReset": "Project reset",
"noPeople": "No members have been added to this project yet.",
@@ -2618,17 +2614,37 @@
"transferTo": "Transfer ownership",
"transferHint": "Transfer '{{name}}' to another Swetrix user. They will become the project owner and can manage settings, members, and invitations. You will stay on the project as an administrator, but the new owner can remove you later.",
"transfereeEmail": "New owner email",
- "reseted": {
- "selectFilters": "Select filters",
- "all": "Reset all analytics data",
- "allHint": "This permanently deletes all analytics data for this project from our servers. The project itself, its settings, and Project ID will remain.",
- "partially": "Reset by date range",
- "partiallyDesc": "Delete analytics data from a specific date range.",
- "partiallyHint": "Choose the start and end dates, then confirm the reset. Analytics data in this date range will be permanently deleted and cannot be recovered.",
- "viaFilters": "Reset by filters",
- "viaFiltersHint": "Delete only the analytics data that matches your selected filters. Matching data will be permanently deleted and cannot be recovered.",
- "filtersPlaceholder": "Select filters",
- "noFilters": "No filters selected, or no data found for this filter type"
+ "deleteData": {
+ "title": "Delete analytics data",
+ "intro": "Permanently remove analytics data from this project. Filter to exactly what you want gone — a spam spike from a handful of countries, a single bad referrer — and see precisely what matches before you delete. This can't be undone.",
+ "dateRange": "Date range",
+ "periods": {
+ "all": "All time",
+ "24h": "Last 24 hours",
+ "7d": "Last 7 days",
+ "30d": "Last 30 days",
+ "custom": "Custom"
+ },
+ "matching": "Matching data",
+ "filtersHint": "Leave the filters empty to match everything in the selected range. Add filters to target specific traffic — for example country is one of the spam sources, or referrer contains a spam domain.",
+ "types": {
+ "pageview": "Pageviews",
+ "custom_event": "Custom events",
+ "error": "Errors",
+ "performance": "Performance",
+ "captcha": "CAPTCHA"
+ },
+ "willDelete": "will be permanently deleted",
+ "holdToDeleteCount": "Hold to delete {{total}}",
+ "summaryAllTime": "across all time",
+ "summaryRange": "{{from}} – {{to}}",
+ "noMatch": "Nothing matches your filters and date range.",
+ "calculating": "Calculating…",
+ "previewError": "Couldn't calculate the preview. Please try again.",
+ "customRangePrompt": "Pick a start and end date.",
+ "selectType": "Select at least one event type to delete.",
+ "started": "Deletion started. It can take a few minutes for the data to disappear.",
+ "error": "Something went wrong while deleting your data."
},
"protected": "Password protected",
"protectedHint": "You can protect your project with a password. Only the people who know the password can access it through a shareable link.",
@@ -2657,7 +2673,6 @@
"title": "User flow",
"noData": "User flow is not available yet due to lack of data, or you disabled this feature in the tracking script parameters"
},
- "noDateRange": "Please select a date range",
"compareDateRangeError": "This date range cannot be compared, please select a other date range",
"previousPeriod": "Previous period",
"disableCompare": "Disable comparison",