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!

Implemented algorithm to distinguish unique visits and regular page views

Blue Mouse45115371b0dc
diff --git a/package.json b/package.json
index 97ac2a3e6..0f05eb5da 100644
--- a/package.json
+++ b/package.json
@@ -31,6 +31,7 @@
"@nestjs/swagger": "^4.8.0",
"@nestjs/typeorm": "^7.1.5",
"bcrypt": "^5.0.1",
+ "blake3": "^2.1.4",
"class-transformer": "^0.4.0",
"class-validator": "^0.13.1",
"clickhouse": "^2.4.0",
diff --git a/src/analytics/analytics.controller.ts b/src/analytics/analytics.controller.ts
index 6b4cdc457..142975f93 100644
--- a/src/analytics/analytics.controller.ts
+++ b/src/analytics/analytics.controller.ts
@@ -6,12 +6,12 @@ import * as _last from 'lodash/last'
import * as dayjs from 'dayjs'
import * as utc from 'dayjs/plugin/utc'
import { v4 as uuidv4 } from 'uuid'
+import { hash } from 'blake3'
import {
- Controller, Body, Query, Param, UseGuards, Get, Post, Put, Delete, BadRequestException,
+ Controller, Body, Query, Param, UseGuards, Get, Post, Ip, Headers, Put, Delete, BadRequestException,
HttpCode, NotFoundException, InternalServerErrorException, NotAcceptableException, NotImplementedException, UnprocessableEntityException,
} from '@nestjs/common'
import { ApiTags, ApiQuery, ApiResponse } from '@nestjs/swagger'
-import { Between } from 'typeorm'
import { AnalyticsService } from './analytics.service'
import { ProjectService } from '../project/project.service'
@@ -31,8 +31,20 @@ import ct from '../common/countriesTimezones'
dayjs.extend(utc)
-// temp
-const allKeys = ['id', 'pid', 'ev', 'pg', 'lc', 'ref', 'sw', 'so', 'me', 'ca', 'lt', 'cc', 'created']
+// todo: implement random salts
+// store them in redis storage and reset every 24 hours
+const getSessionKey = (ip: string, ua: string, salt: string = '') => hash(`${ua}${ip}${salt}`).toString('hex')
+
+// todo: return string, not array
+const analyticsDTO = (pid: string, ev: string, pg: string, lc: string, ref: string, sw: number | string, so: string, me: string, ca: string, lt: number | string, tz: string, unique: number): Array<string | number> => {
+ const cc = tz === 'NULL' ? 'NULL' : ct.getCountryForTimezone(tz)?.id
+ return [uuidv4(), pid, ev, pg, lc, ref, sw, so, me, ca, lt, cc, unique, dayjs.utc().format('YYYY-MM-DD HH:mm:ss')]
+}
+
+const getElValue = (el) => {
+ if (el === undefined || el === null || el === 'NULL') return 'NULL'
+ return `'${el}'`
+}
@ApiTags('Analytics')
@UseGuards(RolesGuard)
@@ -147,35 +159,29 @@ export class AnalyticsController {
}
@Post('/')
- async log(@Body() logDTO: PageviewsDTO): Promise<any> {
- this.logger.log({ logDTO }, 'POST /log')
- await this.analyticsService.validate(logDTO)
- const dto = {
- id: uuidv4(),
- pid: logDTO.pid,
- ev: logDTO.ev,
- pg: logDTO.pg,
- lc: logDTO.lc,
- ref: logDTO.ref,
- sw: logDTO.sw,
- so: logDTO.so,
- me: logDTO.me,
- ca: logDTO.ca,
- lt: logDTO.lt,
- cc: ct.getCountryForTimezone(logDTO.tz)?.id,
- nupw: 0, // todo: increment this value on every user's new pageview in a session range
- created: dayjs.utc().format('YYYY-MM-DD HH:mm:ss'),
+ async log(@Body() logDTO: PageviewsDTO, @Ip() ip, @Headers() headers): Promise<any> {
+ this.logger.log({ logDTO, ip, headers }, 'POST /log')
+ const { 'user-agent': userAgent, origin } = headers
+ await this.analyticsService.validate(logDTO, origin)
+
+ const sessionHash = getSessionKey(ip, userAgent)
+ const unique = await this.analyticsService.isUnique(sessionHash)
+ let dto: Array<string | number>
+
+ if (unique) {
+ dto = analyticsDTO(logDTO.pid, logDTO.ev, logDTO.pg, logDTO.lc, logDTO.ref, logDTO.sw, logDTO.so, logDTO.me, logDTO.ca, logDTO.lt, logDTO.tz, 1)
+ } else {
+ dto = analyticsDTO(logDTO.pid, logDTO.ev, logDTO.pg, 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 0)
}
+ const values = dto.map(getElValue).join(',')
+
// may be vulnerable to sql injection attack
// use only validated project IDs gained from redis store and other data
// todo: refactor; put into redis cache and next upload data chunks from redis into clickhouse
- const absentKeys = allKeys.filter(el => dto[el] === undefined)
- const query = `INSERT INTO analytics (* EXCEPT(${absentKeys.join(', ')})) VALUES (${Object.values(dto).filter(el => el !== undefined).map(el => `'${el}'`).join(',')})`
-
+ const query = `INSERT INTO analytics (*) VALUES (${values})`
try {
await clickhouse.query(query).toPromise()
-
return
} catch (e) {
this.logger.log(e)
diff --git a/src/analytics/entities/analytics.entity.ts b/src/analytics/entities/analytics.entity.ts
index 4c2a56b73..64f0cd814 100644
--- a/src/analytics/entities/analytics.entity.ts
+++ b/src/analytics/entities/analytics.entity.ts
@@ -79,10 +79,10 @@ export class Analytics {
// the record itself is a unique view
// every additional pageview request coming from the user will be set as
// nupw -> non-unique page views
- @Column({
- default: 0,
- })
- nupw: number
+ // @Column({
+ // default: 0,
+ // })
+ // nupw: number
@CreateDateColumn()
created: Date;
diff --git a/src/common/constants.ts b/src/common/constants.ts
index 092462729..29c8825ba 100644
--- a/src/common/constants.ts
+++ b/src/common/constants.ts
@@ -58,7 +58,10 @@ const getRedisProjectKey = (pid: string) => `pid_${pid}`
// 3600 sec -> 1 hour
const redisProjectCacheTimeout = 3600
+// 30 minues -> the amount of time analytics requests within one session are counted as non-unique
+const UNIQUE_SESSION_LIFE_TIME = 1800
+
export {
clickhouse, JWT_LIFE_TIME, HISTORY_LIFE_TIME_DAYS, redis, isValidPID, getRedisProjectKey,
- redisProjectCacheTimeout, getPercentageChange,
+ redisProjectCacheTimeout, getPercentageChange, UNIQUE_SESSION_LIFE_TIME,
}
diff --git a/src/task-manager/task-manager.service.ts b/src/task-manager/task-manager.service.ts
index 62daed5c1..0506e9a7e 100644
--- a/src/task-manager/task-manager.service.ts
+++ b/src/task-manager/task-manager.service.ts
@@ -11,8 +11,8 @@ export class TaskManagerService {
private readonly httpService: HttpService
) {}
- @Cron(CronExpression.EVERY_6_MONTHS)
- async someTask(): Promise<void> {
+ // @Cron(CronExpression.EVERY_MINUTE)
+ // async someTask(): Promise<void> {
- }
-}
\ No newline at end of file
+ // }
+}