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!

Optional bot validation

Blue Mouse85bb02b7ef9f
diff --git a/backend/apps/production/src/analytics/analytics.controller.ts b/backend/apps/production/src/analytics/analytics.controller.ts
index f64d85dda..0964de713 100644
--- a/backend/apps/production/src/analytics/analytics.controller.ts
+++ b/backend/apps/production/src/analytics/analytics.controller.ts
@@ -31,7 +31,6 @@ import {
Header,
} from '@nestjs/common'
import UAParser from 'ua-parser-js'
-import { isbot } from 'isbot'
import { OptionalJwtAccessTokenGuard } from '../auth/guards'
import { Auth, Public } from '../auth/decorators'
@@ -67,8 +66,6 @@ import {
getGeoDetails,
getIPFromHeaders,
} from '../common/utils'
-import { BotDetection } from '../common/decorators/bot-detection.decorator'
-import { BotDetectionGuard } from '../common/guards/bot-detection.guard'
import { GetCustomEventsDto } from './dto/get-custom-events.dto'
import { GetFiltersDto } from './dto/get-filters.dto'
import {
@@ -1121,10 +1118,7 @@ export class AnalyticsController {
return data
}
- // Log error event
@Post('error')
- @UseGuards(BotDetectionGuard)
- @BotDetection()
@Public()
async logError(
@Body() errorDTO: ErrorDTO,
@@ -1135,6 +1129,8 @@ export class AnalyticsController {
const ip = getIPFromHeaders(headers, true) || reqIP || ''
+ await this.analyticsService.throwIfBot(errorDTO.pid, userAgent)
+
await this.analyticsService.validate(errorDTO, origin, 'error', ip)
const { city, region, country } = getGeoDetails(ip, errorDTO.tz)
@@ -1206,10 +1202,7 @@ export class AnalyticsController {
return this.analyticsService.updateEIDStatus(eids, status, pid)
}
- // Log custom event
@Post('custom')
- @UseGuards(BotDetectionGuard)
- @BotDetection()
@Public()
async logCustom(
@Body() eventsDTO: EventsDTO,
@@ -1221,6 +1214,8 @@ export class AnalyticsController {
const ip = getIPFromHeaders(headers, true) || reqIP || ''
this.analyticsService.validateCustomEVMeta(eventsDTO.meta)
+
+ await this.analyticsService.throwIfBot(eventsDTO.pid, userAgent)
await this.analyticsService.validate(eventsDTO, origin, 'custom', ip)
if (eventsDTO.unique) {
@@ -1289,8 +1284,6 @@ export class AnalyticsController {
}
@Post('hb')
- @UseGuards(BotDetectionGuard)
- @BotDetection()
@Auth([], true, true)
async heartbeat(
@Body() logDTO: PageviewsDTO,
@@ -1301,6 +1294,8 @@ export class AnalyticsController {
const { pid } = logDTO
const ip = getIPFromHeaders(headers, true) || reqIP || ''
+ await this.analyticsService.throwIfBot(logDTO.pid, userAgent)
+
await this.analyticsService.validateHeartbeat(logDTO, origin, ip)
const [, psid] = await this.analyticsService.isUnique(pid, userAgent, ip)
@@ -1316,8 +1311,6 @@ export class AnalyticsController {
// Log pageview event
@Post()
- @UseGuards(BotDetectionGuard)
- @BotDetection()
@Public()
async log(
@Body() logDTO: PageviewsDTO,
@@ -1328,6 +1321,8 @@ export class AnalyticsController {
const ip = getIPFromHeaders(headers, true) || reqIP || ''
+ await this.analyticsService.throwIfBot(logDTO.pid, userAgent)
+
await this.analyticsService.validate(logDTO, origin, 'log', ip)
const [unique, psid] = await this.analyticsService.isUnique(
@@ -1448,8 +1443,9 @@ export class AnalyticsController {
const { 'user-agent': userAgent, origin } = headers
const { pid } = data
- // todo: create a decorator for bot traffic detection
- if (isbot(userAgent)) {
+ try {
+ await this.analyticsService.throwIfBot(pid, userAgent)
+ } catch {
res.writeHead(200, { 'Content-Type': 'image/gif' })
return res.end(TRANSPARENT_GIF_BUFFER, 'binary')
}
diff --git a/backend/apps/production/src/analytics/analytics.service.ts b/backend/apps/production/src/analytics/analytics.service.ts
index c9a1863d6..e46ffa731 100644
--- a/backend/apps/production/src/analytics/analytics.service.ts
+++ b/backend/apps/production/src/analytics/analytics.service.ts
@@ -38,6 +38,7 @@ import {
PreconditionFailedException,
ForbiddenException,
} from '@nestjs/common'
+import { isbot } from 'isbot'
import { DEFAULT_TIMEZONE } from '../user/entities/user.entity'
import {
@@ -69,7 +70,7 @@ import { PageviewsDTO } from './dto/pageviews.dto'
import { EventsDTO } from './dto/events.dto'
import { ProjectService } from '../project/project.service'
import { UserService } from '../user/user.service'
-import { Project } from '../project/entity/project.entity'
+import { BotsProtectionLevel, Project } from '../project/entity/project.entity'
import { ChartRenderMode, TimeBucketType } from './dto/getData.dto'
import { GetCustomEventMetadata } from './dto/get-custom-event-meta.dto'
import {
@@ -391,6 +392,17 @@ export class AnalyticsService {
}
}
+ async throwIfBot(pid: string, userAgent: string) {
+ const project = await this.projectService.getRedisProject(pid)
+ if (project.botsProtectionLevel === BotsProtectionLevel.OFF) {
+ return
+ }
+
+ if (isbot(userAgent)) {
+ throw new BadRequestException('Bot traffic detected')
+ }
+ }
+
async checkUptimeAccess(pid: string, monitorId: number) {
const monitor = await this.projectService.findMonitorById(
monitorId as unknown as string,