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!

optimised custom events logic

Blue Mouse1bd1f9befd9b
diff --git a/src/analytics/analytics.controller.ts b/src/analytics/analytics.controller.ts
index 6d60172d8..5e8e58ecb 100644
--- a/src/analytics/analytics.controller.ts
+++ b/src/analytics/analytics.controller.ts
@@ -4,7 +4,6 @@ import * as _toNumber from 'lodash/toNumber'
import * as _size from 'lodash/size'
import * as _last from 'lodash/last'
import * as _map from 'lodash/map'
-import * as _countBy from 'lodash/countBy'
import * as dayjs from 'dayjs'
import * as utc from 'dayjs/plugin/utc'
import { v4 as uuidv4 } from 'uuid'
@@ -91,7 +90,7 @@ export class AnalyticsController {
// TODO: data validation
// TODO: automatic timeBucket detection based on period provided
- let queryCustoms = `SELECT ev FROM customEV WHERE pid='${pid}'`
+ let queryCustoms = `SELECT ev, count() FROM customEV WHERE pid='${pid}'`
let subQuery = `FROM analytics WHERE pid='${pid}'`
if (!_isEmpty(from) && !_isEmpty(to)) {
@@ -102,14 +101,14 @@ export class AnalyticsController {
if (dayjs.utc(to).isAfter(dayjs.utc(), 'second')) {
groupTo = dayjs.utc().format('YYYY-MM-DD HH:mm:ss')
- queryCustoms += ` AND created BETWEEN ${from} AND ${groupTo}`
+ queryCustoms += ` AND created BETWEEN ${from} AND ${groupTo} GROUP BY ev`
} else {
- queryCustoms += ` AND created BETWEEN ${from} AND ${to}`
+ queryCustoms += ` AND created BETWEEN ${from} AND ${to} GROUP BY ev`
}
} else if (!_isEmpty(period)) {
groupFrom = dayjs.utc().subtract(parseInt(period), _last(period)).format('YYYY-MM-DD')
groupTo = dayjs.utc().format('YYYY-MM-DD 23:59:59')
- queryCustoms += ` AND created BETWEEN '${groupFrom}' AND '${groupTo}'`
+ queryCustoms += ` AND created BETWEEN '${groupFrom}' AND '${groupTo}' GROUP BY ev`
subQuery += ` AND created BETWEEN '${groupFrom}' AND '${groupTo}'`
} else {
throw new BadRequestException('The timeframe (either from/to pair or period) to be provided')
@@ -117,14 +116,11 @@ export class AnalyticsController {
const result = await this.analyticsService.groupByTimeBucket(timeBucket, groupFrom, groupTo, subQuery, pid)
- const customs = await clickhouse.query(queryCustoms).toPromise()
- // TODO: VERIFY THAT CUSTOM EVENTS WORK PROPERLY
- console.log(queryCustoms, _countBy(customs, 'ev'))
- console.log(customs)
-
+ const customs = await this.analyticsService.processCustomEV(queryCustoms)
+
return {
...result,
- customs: _countBy(customs, 'ev'),
+ customs,
}
}
@@ -193,7 +189,7 @@ export class AnalyticsController {
throw new ForbiddenException('Bot traffic is ignored')
}
- await this.analyticsService.validate(eventsDTO, origin)
+ await this.analyticsService.validate(eventsDTO, origin, 'custom')
const ip = headers['cf-connecting-ip'] || headers['x-forwarded-for'] || reqIP || ''
@@ -203,7 +199,7 @@ export class AnalyticsController {
const unique = await this.analyticsService.isUnique(sessionHash)
if (!unique) {
- return
+ throw new ForbiddenException('The unique option provided, while the custom event have already been created for this session')
}
}
diff --git a/src/analytics/analytics.service.ts b/src/analytics/analytics.service.ts
index f2a7f9cda..d5f05659b 100644
--- a/src/analytics/analytics.service.ts
+++ b/src/analytics/analytics.service.ts
@@ -45,6 +45,12 @@ interface chartCHResponse {
'count()': number
}
+interface customsCHResponse {
+ ev: string
+ 'count()': number
+}
+
+// Smaller than 64 characters, must start with an English letter and contain only letters (a-z A-Z), numbers (0-9), underscores (_) and dots (.)
const customEVvalidate = /^[a-zA-Z](?:[\w\.]){0,62}$/
@Injectable()
@@ -151,13 +157,14 @@ export class AnalyticsService {
if (!isValidPID(pid)) throw new BadRequestException('The provided Project ID (pid) is incorrect')
}
- async validate(logDTO: PageviewsDTO | EventsDTO, origin: string): Promise<string | null> {
+ async validate(logDTO: PageviewsDTO | EventsDTO, origin: string, type: 'custom' | 'log' = 'log'): Promise<string | null> {
if (_isEmpty(logDTO)) throw new BadRequestException('The request cannot be empty')
const { pid } = logDTO
this.validatePID(pid)
- if (logDTO instanceof EventsDTO) {
+ if (type === 'custom') {
+ // @ts-ignore
const { ev } = logDTO
if (_isEmpty(ev)) {
@@ -366,4 +373,19 @@ export class AnalyticsService {
},
})
}
+
+ async processCustomEV(query: string): Promise<object> {
+ const result = {}
+
+ // @ts-ignore
+ const rawCustoms: Array<customsCHResponse> = await clickhouse.query(query).toPromise()
+ const size = _size(rawCustoms)
+
+ for (let i = 0; i < size; ++i) {
+ const { ev, 'count()': c } = rawCustoms[i]
+ result[ev] = c
+ }
+
+ return result
+ }
}