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!

added basic support for filters

Blue Mouse44ef91cc108a
diff --git a/src/analytics/analytics.controller.ts b/src/analytics/analytics.controller.ts
index 899dca035..66d5d1730 100644
--- a/src/analytics/analytics.controller.ts
+++ b/src/analytics/analytics.controller.ts
@@ -85,16 +85,17 @@ export class AnalyticsController {
@Get('/')
async getData(@Query() data: AnalyticsGET_DTO, @CurrentUserId() uid: string): Promise<any> {
- const { pid, period, timeBucket, from, to } = data
+ const { pid, period, timeBucket, from, to, filters } = data
this.analyticsService.validatePID(pid)
this.analyticsService.validatePeriod(period)
this.analyticsService.validateTimebucket(timeBucket)
+ const filtersQuery = this.analyticsService.getFiltersQuery(filters)
await this.analyticsService.checkProjectAccess(pid, uid)
let groupFrom = from, groupTo = to
let queryCustoms = `SELECT ev, count() FROM customEV WHERE pid='${pid}'`
- let subQuery = `FROM analytics WHERE pid='${pid}'`
+ let subQuery = `FROM analytics WHERE pid='${pid}' ${filtersQuery}`
if (!_isEmpty(from) && !_isEmpty(to)) {
throw new NotImplementedException('Filtering by from/to params is currently not available')
@@ -124,6 +125,7 @@ export class AnalyticsController {
return {
...result,
customs,
+ appliedFilters: filters,
}
}
diff --git a/src/analytics/analytics.service.ts b/src/analytics/analytics.service.ts
index be79c0b7e..d5b6bdda1 100644
--- a/src/analytics/analytics.service.ts
+++ b/src/analytics/analytics.service.ts
@@ -37,7 +37,7 @@ dayjs.extend(utc)
export const getSessionKey = (ip: string, ua: string, pid: string, salt: string = '') => `ses_${hash(`${ua}${ip}${pid}${salt}`).toString('hex')}`
-const cols = [
+export const cols = [
'cc', 'pg', 'lc', 'br', 'os', 'dv', 'ref','so', 'me', 'ca',
]
@@ -243,6 +243,28 @@ export class AnalyticsService {
}
}
+ // returns SQL filters query in a format like 'AND col=value AND ...'
+ getFiltersQuery(filters: Object): string {
+ if (_isEmpty(filters)) {
+ return ''
+ }
+
+ const cols = _keys(filters)
+ let query = ''
+
+ for (let i = 0; i < _size(cols); ++i) {
+ const column = cols[i]
+
+ if (!_includes(cols, column)) {
+ throw new UnprocessableEntityException(`The provided filter (${column}) is not supported`)
+ }
+
+ query += ` AND ${column}=${filters[column]}`
+ }
+
+ return query
+ }
+
validateTimebucket(tb: string): void {
if (!_includes(validTimebuckets, tb)) {
throw new UnprocessableEntityException('The provided timebucket is incorrect')
diff --git a/src/analytics/dto/getData.dto.ts b/src/analytics/dto/getData.dto.ts
index 0c9338b43..34eee804c 100644
--- a/src/analytics/dto/getData.dto.ts
+++ b/src/analytics/dto/getData.dto.ts
@@ -27,4 +27,10 @@ export class AnalyticsGET_DTO {
@ApiProperty()
to: string
+
+ @ApiProperty({
+ type: Object,
+ description: 'An object of properties to filter: { column: filter }',
+ })
+ filters: Object
}