📈 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!
TypeScript chartsclickhousedockergdprgoogle-analyticsmetricsmonitoringmysqlnestjsprivacyredisself-hostedselfhostedstatisticstailwindcssweb-analyticswebanalyticswebsite
Don't throw 400 for detected bot requests
Blue Mousedd303fd5ab22
diff --git a/backend/apps/cloud/src/analytics/analytics.controller.ts b/backend/apps/cloud/src/analytics/analytics.controller.tsindex 306181fb7..7bb00db4e 100644--- a/backend/apps/cloud/src/analytics/analytics.controller.ts+++ b/backend/apps/cloud/src/analytics/analytics.controller.ts@@ -97,6 +97,12 @@ dayjs.extend(dayjsTimezone)export const DEFAULT_MEASURE = 'median'+// Silent 200 response for bots+// https://github.com/Swetrix/swetrix/issues/371+const BOT_RESPONSE = {+ message: 'Bot traffic detected, request is ignored',+}+// Performance object validator: none of the values cannot be bigger than 1000 * 60 * 5 (5 minutes) and are >= 0const MAX_PERFORMANCE_VALUE = 1000 * 60 * 5const isPerformanceValid = (perf: any) => {@@ -137,7 +143,7 @@ const getPIDsArray = (pids, pid) => {try {pids = JSON.parse(pids)- } catch (e) {+ } catch (reason) {throw new UnprocessableEntityException("Cannot process the provided array of Project ID's",)@@ -1106,7 +1112,11 @@ export class AnalyticsController {const ip = getIPFromHeaders(headers, true) || reqIP || ''- await this.analyticsService.throwIfBot(errorDTO.pid, userAgent)+ const isBot = await this.analyticsService.isBot(errorDTO.pid, userAgent)++ if (isBot) {+ return BOT_RESPONSE+ }await this.analyticsService.validate(errorDTO, origin, ip)@@ -1154,12 +1164,14 @@ export class AnalyticsController {async_insert: 1,},})- } catch (e) {- this.logger.error(e)+ } catch (reason) {+ this.logger.error(reason)throw new InternalServerErrorException('Error occured while saving the custom event',)}++ return {}}// Update error(s) status@@ -1199,7 +1211,12 @@ export class AnalyticsController {this.analyticsService.validateCustomEVMeta(eventsDTO.meta)- await this.analyticsService.throwIfBot(eventsDTO.pid, userAgent)+ const isBot = await this.analyticsService.isBot(eventsDTO.pid, userAgent)++ if (isBot) {+ return BOT_RESPONSE+ }+await this.analyticsService.validate(eventsDTO, origin, ip)if (eventsDTO.unique) {@@ -1267,12 +1284,14 @@ export class AnalyticsController {async_insert: 1,},})- } catch (e) {- this.logger.error(e)+ } catch (reason) {+ this.logger.error(reason)throw new InternalServerErrorException('Error occured while saving the custom event',)}++ return {}}@Post('hb')@@ -1286,7 +1305,11 @@ export class AnalyticsController {const { pid } = logDTOconst ip = getIPFromHeaders(headers, true) || reqIP || ''- await this.analyticsService.throwIfBot(logDTO.pid, userAgent)+ const isBot = await this.analyticsService.isBot(logDTO.pid, userAgent)++ if (isBot) {+ return BOT_RESPONSE+ }await this.analyticsService.validateHeartbeat(logDTO, origin, ip)@@ -1302,6 +1325,8 @@ export class AnalyticsController {await redis.set(sessionHash, psid, 'EX', UNIQUE_SESSION_LIFE_TIME)await this.analyticsService.processInteractionSD(psid, pid)++ return {}}// Log pageview event@@ -1312,7 +1337,11 @@ export class AnalyticsController {const ip = getIPFromHeaders(headers, true) || reqIP || ''- await this.analyticsService.throwIfBot(logDTO.pid, userAgent)+ const isBot = await this.analyticsService.isBot(logDTO.pid, userAgent)++ if (isBot) {+ return BOT_RESPONSE+ }await this.analyticsService.validate(logDTO, origin, ip)@@ -1420,12 +1449,14 @@ export class AnalyticsController {},})}- } catch (e) {- this.logger.error(e)+ } catch (reason) {+ this.logger.error(reason)throw new InternalServerErrorException('Error occured while saving the log data',)}++ return {}}// Fallback for logging pageviews for users with JavaScript disabled@@ -1442,9 +1473,9 @@ export class AnalyticsController {const { 'user-agent': userAgent, origin } = headersconst { pid } = data- try {- await this.analyticsService.throwIfBot(pid, userAgent)- } catch {+ const isBot = await this.analyticsService.isBot(pid, userAgent)++ if (isBot) {res.writeHead(200, { 'Content-Type': 'image/gif' })return res.end(TRANSPARENT_GIF_BUFFER, 'binary')}@@ -1508,8 +1539,8 @@ export class AnalyticsController {async_insert: 1,},})- } catch (e) {- this.logger.error(e)+ } catch (reason) {+ this.logger.error(reason)}res.writeHead(200, { 'Content-Type': 'image/gif' })diff --git a/backend/apps/cloud/src/analytics/analytics.service.ts b/backend/apps/cloud/src/analytics/analytics.service.tsindex 049b6d16c..bec7c6fc6 100644--- a/backend/apps/cloud/src/analytics/analytics.service.ts+++ b/backend/apps/cloud/src/analytics/analytics.service.ts@@ -361,16 +361,14 @@ export class AnalyticsService {}}- async throwIfBot(pid: string, userAgent: string) {+ async isBot(pid: string, userAgent: string) {const project = await this.projectService.getRedisProject(pid)if (project.botsProtectionLevel === BotsProtectionLevel.OFF) {- return+ return false}- if (isbot(userAgent)) {- throw new BadRequestException('Bot traffic detected')- }+ return isbot(userAgent)}checkOrigin(project: Project, origin: string): void {diff --git a/backend/apps/community/src/analytics/analytics.controller.ts b/backend/apps/community/src/analytics/analytics.controller.tsindex b1544a32b..52b7f5c9c 100644--- a/backend/apps/community/src/analytics/analytics.controller.ts+++ b/backend/apps/community/src/analytics/analytics.controller.ts@@ -84,6 +84,12 @@ dayjs.extend(dayjsTimezone)export const DEFAULT_MEASURE = 'median'+// Silent 200 response for bots+// https://github.com/Swetrix/swetrix/issues/371+const BOT_RESPONSE = {+ message: 'Bot traffic detected, opinion rejected :D',+}+// Performance object validator: none of the values cannot be bigger than 1000 * 60 * 5 (5 minutes) and are >= 0const MAX_PERFORMANCE_VALUE = 1000 * 60 * 5const isPerformanceValid = (perf: any) => {@@ -907,7 +913,12 @@ export class AnalyticsController {this.analyticsService.validateCustomEVMeta(eventsDTO.meta)- await this.analyticsService.throwIfBot(eventsDTO.pid, userAgent)+ const isBot = await this.analyticsService.isBot(eventsDTO.pid, userAgent)++ if (isBot) {+ return BOT_RESPONSE+ }+await this.analyticsService.validate(eventsDTO, origin, ip)if (eventsDTO.unique) {@@ -981,6 +992,8 @@ export class AnalyticsController {'Error occured while saving the custom event',)}++ return {}}@Post('hb')@@ -994,7 +1007,11 @@ export class AnalyticsController {const { pid } = logDTOconst ip = getIPFromHeaders(headers) || reqIP || ''- await this.analyticsService.throwIfBot(logDTO.pid, userAgent)+ const isBot = await this.analyticsService.isBot(logDTO.pid, userAgent)++ if (isBot) {+ return BOT_RESPONSE+ }await this.analyticsService.validateHeartbeat(logDTO, origin, ip)@@ -1010,6 +1027,8 @@ export class AnalyticsController {await redis.set(sessionHash, psid, 'EX', UNIQUE_SESSION_LIFE_TIME)await this.analyticsService.processInteractionSD(psid, pid)++ return {}}@Post()@@ -1019,7 +1038,11 @@ export class AnalyticsController {const ip = getIPFromHeaders(headers) || reqIP || ''- await this.analyticsService.throwIfBot(logDTO.pid, userAgent)+ const isBot = await this.analyticsService.isBot(logDTO.pid, userAgent)++ if (isBot) {+ return BOT_RESPONSE+ }await this.analyticsService.validate(logDTO, origin, ip)@@ -1134,6 +1157,8 @@ export class AnalyticsController {'Error occured while saving the log data',)}++ return {}}// Fallback for logging pageviews for users with JavaScript disabled@@ -1149,9 +1174,9 @@ export class AnalyticsController {const { 'user-agent': userAgent, origin } = headersconst { pid } = data- try {- await this.analyticsService.throwIfBot(pid, userAgent)- } catch {+ const isBot = await this.analyticsService.isBot(pid, userAgent)++ if (isBot) {res.writeHead(200, { 'Content-Type': 'image/gif' })return res.end(TRANSPARENT_GIF_BUFFER, 'binary')}@@ -1459,7 +1484,11 @@ export class AnalyticsController {const ip = getIPFromHeaders(headers) || reqIP || ''- await this.analyticsService.throwIfBot(errorDTO.pid, userAgent)+ const isBot = await this.analyticsService.isBot(errorDTO.pid, userAgent)++ if (isBot) {+ return BOT_RESPONSE+ }await this.analyticsService.validate(errorDTO, origin, ip)@@ -1513,6 +1542,8 @@ export class AnalyticsController {'Error occured while saving the custom event',)}++ return {}}// Update error(s) statusdiff --git a/backend/apps/community/src/analytics/analytics.service.ts b/backend/apps/community/src/analytics/analytics.service.tsindex 4b0b36697..47923c7df 100644--- a/backend/apps/community/src/analytics/analytics.service.ts+++ b/backend/apps/community/src/analytics/analytics.service.ts@@ -329,16 +329,14 @@ export class AnalyticsService {this.projectService.allowedToView(project, uid, password)}- async throwIfBot(pid: string, userAgent: string) {+ async isBot(pid: string, userAgent: string) {const project = await this.projectService.getRedisProject(pid)if (project.botsProtectionLevel === BotsProtectionLevel.OFF) {- return+ return false}- if (isbot(userAgent)) {- throw new BadRequestException('Bot traffic detected')- }+ return isbot(userAgent)}checkOrigin(project: Project, origin: string): void {@@ -785,7 +783,7 @@ export class AnalyticsService {}return pages- } catch (e) {+ } catch (reason) {throw new UnprocessableEntityException('Cannot process the provided array of pages',)@@ -892,7 +890,7 @@ export class AnalyticsService {try {parsed = JSON.parse(filters)- } catch (e) {+ } catch {console.error(`Cannot parse the filters array: ${filters}`)return [query, params, parsed, customEVFilterApplied]}@@ -3390,7 +3388,7 @@ export class AnalyticsService {try {parsedOptions = JSON.parse(options)- } catch (reason) {+ } catch {console.error('[getErrorsList] Failed to parse options:', options)}