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!

removed duplicated checks for group/to params & added dunny GET /user-flow

Blue Mouse119322d511d7
diff --git a/src/analytics/analytics.controller.ts b/src/analytics/analytics.controller.ts
index efaade70d..eef068c38 100644
--- a/src/analytics/analytics.controller.ts
+++ b/src/analytics/analytics.controller.ts
@@ -49,6 +49,7 @@ import { RolesGuard } from '../auth/guards/roles.guard'
import { PageviewsDTO } from './dto/pageviews.dto'
import { EventsDTO } from './dto/events.dto'
import { AnalyticsGET_DTO } from './dto/getData.dto'
+import { GetUserFlowDTO } from './dto/getUserFlow.dto'
import { AppLoggerService } from '../logger/logger.service'
import { SelfhostedGuard } from '../common/guards/selfhosted.guard'
import {
@@ -284,11 +285,9 @@ export class AnalyticsController {
this.analyticsService.validateTimebucket(timeBucket)
const [filtersQuery, filtersParams, parsedFilters] =
this.analyticsService.getFiltersQuery(filters)
+ const { groupFrom, groupTo } = this.analyticsService.getGroupFromTo(from, to, timeBucket, period, timezone)
await this.analyticsService.checkProjectAccess(pid, uid)
- let groupFrom = from
- let groupTo = to
-
let queryCustoms = `SELECT ev, count() FROM customEV WHERE pid = {pid:FixedString(12)} ${filtersQuery} AND created BETWEEN {groupFrom:String} AND {groupTo:String} GROUP BY ev`
// TODO: Refactor
let subQuery = `FROM ${
@@ -311,98 +310,12 @@ export class AnalyticsController {
const paramsData = {
params: {
pid,
- groupFrom: null,
- groupTo: null,
+ groupFrom,
+ groupTo,
...filtersParams,
},
}
- if (!_isEmpty(from) && !_isEmpty(to)) {
- if (!isValidDate(from)) {
- throw new PreconditionFailedException(
- "The timeframe 'from' parameter is invalid",
- )
- }
-
- if (!isValidDate(to)) {
- throw new PreconditionFailedException(
- "The timeframe 'to' parameter is invalid",
- )
- }
-
- if (dayjs.utc(from).isAfter(dayjs.utc(to), 'second')) {
- throw new PreconditionFailedException(
- "The timeframe 'from' parameter cannot be greater than 'to'",
- )
- }
-
- checkIfTBAllowed(timeBucket, from, to)
-
- groupFrom = dayjs.tz(from, timezone).utc().format('YYYY-MM-DD HH:mm:ss')
-
- if (from === to) {
- groupTo = dayjs
- .tz(to, timezone)
- .add(1, 'day')
- .format('YYYY-MM-DD HH:mm:ss')
- } else {
- groupTo = dayjs.tz(to, timezone).format('YYYY-MM-DD HH:mm:ss')
- }
- } else if (!_isEmpty(period)) {
- if (period === 'today') {
- if (timezone !== DEFAULT_TIMEZONE && isValidTimezone(timezone)) {
- groupFrom = dayjs()
- .tz(timezone)
- .startOf('d')
- .utc()
- .format('YYYY-MM-DD HH:mm:ss')
- groupTo = dayjs().tz(timezone).utc().format('YYYY-MM-DD HH:mm:ss')
- } else {
- groupFrom = dayjs.utc().startOf('d').format('YYYY-MM-DD')
- groupTo = dayjs.utc().format('YYYY-MM-DD HH:mm:ss')
- }
- } else if (period === 'yesterday') {
- if (timezone !== DEFAULT_TIMEZONE && isValidTimezone(timezone)) {
- groupFrom = dayjs()
- .tz(timezone)
- .startOf('d')
- .subtract(1, 'day')
- .utc()
- .format('YYYY-MM-DD HH:mm:ss')
- groupTo = dayjs()
- .tz(timezone)
- .startOf('d')
- .utc()
- .format('YYYY-MM-DD HH:mm:ss')
- } else {
- groupFrom = dayjs
- .utc()
- .startOf('d')
- .subtract(1, 'day')
- .format('YYYY-MM-DD')
- groupTo = dayjs.utc().startOf('d').format('YYYY-MM-DD HH:mm:ss')
- }
- } else {
- groupFrom = dayjs
- .utc()
- .subtract(parseInt(period, 10), _last(period))
- .format('YYYY-MM-DD')
- groupTo = dayjs.utc().format('YYYY-MM-DD 23:59:59')
-
- checkIfTBAllowed(timeBucket, groupFrom, groupTo)
- }
- } else {
- throw new BadRequestException(
- 'The timeframe (either from/to pair or period) has to be provided',
- )
- }
-
- paramsData.params = {
- ...paramsData.params,
- groupFrom,
- groupTo,
- }
-
let result: object | void
if (isCaptcha) {
@@ -481,108 +394,20 @@ export class AnalyticsController {
this.analyticsService.validateTimebucket(timeBucket)
const [filtersQuery, filtersParams] =
this.analyticsService.getFiltersQuery(filters)
+ const { groupFrom, groupTo } = this.analyticsService.getGroupFromTo(from, to, timeBucket, period, timezone)
await this.analyticsService.checkProjectAccess(pid, uid)
- let groupFrom = from
- let groupTo = to
-
const subQuery = `FROM performance WHERE pid = {pid:FixedString(12)} ${filtersQuery} AND created BETWEEN {groupFrom:String} AND {groupTo:String}`
const paramsData = {
params: {
pid,
- groupFrom: null,
- groupTo: null,
+ groupFrom,
+ groupTo,
...filtersParams,
},
}
- if (!_isEmpty(from) && !_isEmpty(to)) {
- if (!isValidDate(from)) {
- throw new PreconditionFailedException(
- "The timeframe 'from' parameter is invalid",
- )
- }
-
- if (!isValidDate(to)) {
- throw new PreconditionFailedException(
- "The timeframe 'to' parameter is invalid",
- )
- }
-
- if (dayjs.utc(from).isAfter(dayjs.utc(to), 'second')) {
- throw new PreconditionFailedException(
- "The timeframe 'from' parameter cannot be greater than 'to'",
- )
- }
-
- checkIfTBAllowed(timeBucket, from, to)
-
- groupFrom = dayjs.tz(from, timezone).utc().format('YYYY-MM-DD HH:mm:ss')
-
- if (from === to) {
- groupTo = dayjs
- .tz(to, timezone)
- .add(1, 'day')
- .format('YYYY-MM-DD HH:mm:ss')
- } else {
- groupTo = dayjs.tz(to, timezone).format('YYYY-MM-DD HH:mm:ss')
- }
- } else if (!_isEmpty(period)) {
- if (period === 'today') {
- if (timezone !== DEFAULT_TIMEZONE && isValidTimezone(timezone)) {
- groupFrom = dayjs()
- .tz(timezone)
- .startOf('d')
- .utc()
- .format('YYYY-MM-DD HH:mm:ss')
- groupTo = dayjs().tz(timezone).utc().format('YYYY-MM-DD HH:mm:ss')
- } else {
- groupFrom = dayjs.utc().startOf('d').format('YYYY-MM-DD')
- groupTo = dayjs.utc().format('YYYY-MM-DD HH:mm:ss')
- }
- } else if (period === 'yesterday') {
- if (timezone !== DEFAULT_TIMEZONE && isValidTimezone(timezone)) {
- groupFrom = dayjs()
- .tz(timezone)
- .startOf('d')
- .subtract(1, 'day')
- .utc()
- .format('YYYY-MM-DD HH:mm:ss')
- groupTo = dayjs()
- .tz(timezone)
- .startOf('d')
- .utc()
- .format('YYYY-MM-DD HH:mm:ss')
- } else {
- groupFrom = dayjs
- .utc()
- .startOf('d')
- .subtract(1, 'day')
- .format('YYYY-MM-DD')
- groupTo = dayjs.utc().startOf('d').format('YYYY-MM-DD HH:mm:ss')
- }
- } else {
- groupFrom = dayjs
- .utc()
- .subtract(parseInt(period, 10), _last(period))
- .format('YYYY-MM-DD')
- groupTo = dayjs.utc().format('YYYY-MM-DD 23:59:59')
-
- checkIfTBAllowed(timeBucket, groupFrom, groupTo)
- }
- } else {
- throw new BadRequestException(
- 'The timeframe (either from/to pair or period) has to be provided',
- )
- }
-
- paramsData.params = {
- ...paramsData.params,
- groupFrom,
- groupTo,
- }
-
const result = await this.analyticsService.groupPerfByTimeBucket(
timeBucket,
groupFrom,
@@ -617,6 +442,46 @@ export class AnalyticsController {
return this.getData(data, uid, true)
}
+ @Get('/user-flow')
+ @Auth([], true, true)
+ async getUserFlow(
+ @Query() data: GetUserFlowDTO,
+ @CurrentUserId() uid: string,
+ ): Promise<any> {
+ const {
+ pid,
+ period,
+ timeBucket,
+ from,
+ to,
+ timezone = DEFAULT_TIMEZONE,
+ } = data
+ this.analyticsService.validatePID(pid)
+
+ if (!_isEmpty(period)) {
+ this.analyticsService.validatePeriod(period)
+ }
+
+ this.analyticsService.validateTimebucket(timeBucket)
+ await this.analyticsService.checkProjectAccess(pid, uid)
+
+ const { groupFrom, groupTo } = this.analyticsService.getGroupFromTo(
+ from,
+ to,
+ timeBucket,
+ period,
+ timezone,
+ )
+
+ const params = {
+ pid,
+ groupFrom,
+ groupTo,
+ }
+
+ return this.analyticsService.getUserFlow(params)
+ }
+
@Get('/birdseye')
@Auth([], true, true)
// returns overall short statistics per project
diff --git a/src/analytics/analytics.service.ts b/src/analytics/analytics.service.ts
index 0f5788228..4885b662a 100644
--- a/src/analytics/analytics.service.ts
+++ b/src/analytics/analytics.service.ts
@@ -5,6 +5,7 @@ import * as _includes from 'lodash/includes'
import * as _map from 'lodash/map'
import * as _toUpper from 'lodash/toUpper'
import * as _join from 'lodash/join'
+import * as _last from 'lodash/last'
import * as _some from 'lodash/some'
import * as _find from 'lodash/find'
import * as _now from 'lodash/now'
@@ -211,7 +212,7 @@ const generateParamsQuery = (
@Injectable()
export class AnalyticsService {
- constructor(private readonly projectService: ProjectService) {}
+ constructor(private readonly projectService: ProjectService) { }
async checkProjectAccess(pid: string, uid: string | null): Promise<void> {
if (!isSelfhosted) {
@@ -341,6 +342,100 @@ export class AnalyticsService {
return null
}
+ getGroupFromTo(from: string, to: string, timeBucket: TimeBucketType, period: string, timezone: string): { groupFrom: string, groupTo: string } {
+ let groupFrom
+ let groupTo
+
+ if (!_isEmpty(from) && !_isEmpty(to)) {
+ if (!isValidDate(from)) {
+ throw new PreconditionFailedException(
+ "The timeframe 'from' parameter is invalid",
+ )
+ }
+
+ if (!isValidDate(to)) {
+ throw new PreconditionFailedException(
+ "The timeframe 'to' parameter is invalid",
+ )
+ }
+
+ if (dayjs.utc(from).isAfter(dayjs.utc(to), 'second')) {
+ throw new PreconditionFailedException(
+ "The timeframe 'from' parameter cannot be greater than 'to'",
+ )
+ }
+
+ checkIfTBAllowed(timeBucket, from, to)
+
+ groupFrom = dayjs.tz(from, timezone).utc().format('YYYY-MM-DD HH:mm:ss')
+
+ if (from === to) {
+ groupTo = dayjs
+ .tz(to, timezone)
+ .add(1, 'day')
+ .format('YYYY-MM-DD HH:mm:ss')
+ } else {
+ groupTo = dayjs.tz(to, timezone).format('YYYY-MM-DD HH:mm:ss')
+ }
+ } else if (!_isEmpty(period)) {
+ if (period === 'today') {
+ if (timezone !== DEFAULT_TIMEZONE && isValidTimezone(timezone)) {
+ groupFrom = dayjs()
+ .tz(timezone)
+ .startOf('d')
+ .utc()
+ .format('YYYY-MM-DD HH:mm:ss')
+ groupTo = dayjs().tz(timezone).utc().format('YYYY-MM-DD HH:mm:ss')
+ } else {
+ groupFrom = dayjs.utc().startOf('d').format('YYYY-MM-DD')
+ groupTo = dayjs.utc().format('YYYY-MM-DD HH:mm:ss')
+ }
+ } else if (period === 'yesterday') {
+ if (timezone !== DEFAULT_TIMEZONE && isValidTimezone(timezone)) {
+ groupFrom = dayjs()
+ .tz(timezone)
+ .startOf('d')
+ .subtract(1, 'day')
+ .utc()
+ .format('YYYY-MM-DD HH:mm:ss')
+ groupTo = dayjs()
+ .tz(timezone)
+ .startOf('d')
+ .utc()
+ .format('YYYY-MM-DD HH:mm:ss')
+ } else {
+ groupFrom = dayjs
+ .utc()
+ .startOf('d')
+ .subtract(1, 'day')
+ .format('YYYY-MM-DD')
+ groupTo = dayjs.utc().startOf('d').format('YYYY-MM-DD HH:mm:ss')
+ }
+ } else {
+ groupFrom = dayjs
+ .utc()
+ .subtract(parseInt(period, 10), _last(period))
+ .format('YYYY-MM-DD')
+ groupTo = dayjs.utc().format('YYYY-MM-DD 23:59:59')
+
+ checkIfTBAllowed(timeBucket, groupFrom, groupTo)
+ }
+ } else {
+ throw new BadRequestException(
+ 'The timeframe (either from/to pair or period) has to be provided',
+ )
+ }
+
+ return {
+ groupFrom,
+ groupTo,
+ }
+ }
+
+ async getUserFlow(params: any): Promise<any> {
+ // TODO
+ }
+
async getSessionHash(
pid: string,
userAgent: string,
@@ -426,9 +521,8 @@ export class AnalyticsService {
...params,
[colFilter]: filter,
}
- query += ` ${
- isExclusive ? 'AND NOT' : 'AND'
- } ${column}={${colFilter}:String}`
+ query += ` ${isExclusive ? 'AND NOT' : 'AND'
+ } ${column}={${colFilter}:String}`
}
return [query, params, parsed]
@@ -666,11 +760,9 @@ export class AnalyticsService {
query += ' UNION ALL '
}
- query += `select ${i} index, count() from customEV where ${
- paramsData.params.ev_exclusive ? 'NOT' : ''
- } ev = {ev:String} AND pid = {pid:FixedString(12)} and created between '${
- xM[i]
- }' and '${xM[1 + i]}' ${filtersQuery}`
+ query += `select ${i} index, count() from customEV where ${paramsData.params.ev_exclusive ? 'NOT' : ''
+ } ev = {ev:String} AND pid = {pid:FixedString(12)} and created between '${xM[i]
+ }' and '${xM[1 + i]}' ${filtersQuery}`
}
// @ts-ignore
@@ -711,9 +803,8 @@ export class AnalyticsService {
query += ' UNION ALL '
}
- query += `select ${i} index, unique, count(), avg(sdur) from analytics where pid = {pid:FixedString(12)} and created between '${
- xM[i]
- }' and '${xM[1 + i]}' ${filtersQuery} group by unique`
+ query += `select ${i} index, unique, count(), avg(sdur) from analytics where pid = {pid:FixedString(12)} and created between '${xM[i]
+ }' and '${xM[1 + i]}' ${filtersQuery} group by unique`
}
// @ts-ignore
@@ -855,9 +946,8 @@ export class AnalyticsService {
query += ' UNION ALL '
}
- query += `select ${i} index, count() from captcha where pid = {pid:FixedString(12)} and created between '${
- xM[i]
- }' and '${xM[1 + i]}' ${filtersQuery}`
+ query += `select ${i} index, count() from captcha where pid = {pid:FixedString(12)} and created between '${xM[i]
+ }' and '${xM[1 + i]}' ${filtersQuery}`
}
// @ts-ignore
@@ -964,9 +1054,8 @@ export class AnalyticsService {
query += ' UNION ALL '
}
- query += `select ${i} index, avg(dns), avg(tls), avg(conn), avg(response), avg(render), avg(domLoad), avg(ttfb) from performance where pid = {pid:FixedString(12)} and created between '${
- xM[i]
- }' and '${xM[1 + i]}' ${filtersQuery} group by pid`
+ query += `select ${i} index, avg(dns), avg(tls), avg(conn), avg(response), avg(render), avg(domLoad), avg(ttfb) from performance where pid = {pid:FixedString(12)} and created between '${xM[i]
+ }' and '${xM[1 + i]}' ${filtersQuery} group by pid`
}
// @ts-ignore
@@ -1099,9 +1188,8 @@ export class AnalyticsService {
query += ' UNION ALL '
}
- query += `select ${i} index, ev, count() from customEV where pid = {pid:FixedString(12)} and created between '${
- xM[i]
- }' and '${xM[1 + i]}' ${filtersQuery} group by pid, ev`
+ query += `select ${i} index, ev, count() from customEV where pid = {pid:FixedString(12)} and created between '${xM[i]
+ }' and '${xM[1 + i]}' ${filtersQuery} group by pid, ev`
}
// @ts-ignore
diff --git a/src/analytics/dto/getUserFlow.dto.ts b/src/analytics/dto/getUserFlow.dto.ts
new file mode 100644
index 000000000..2929bd85b
--- /dev/null
+++ b/src/analytics/dto/getUserFlow.dto.ts
@@ -0,0 +1,36 @@
+import { ApiProperty } from '@nestjs/swagger'
+import { IsNotEmpty } from 'class-validator'
+import { DEFAULT_TIMEZONE } from '../../user/entities/user.entity'
+
+export enum TimeBucketType {
+ HOUR = 'hour',
+ DAY = 'day',
+ WEEK = 'week',
+ MONTH = 'month',
+}
+
+// eslint-disable-next-line @typescript-eslint/naming-convention
+export class GetUserFlowDTO {
+ @ApiProperty()
+ @IsNotEmpty()
+ pid: string
+
+ @ApiProperty()
+ period: string
+
+ @ApiProperty({ enum: TimeBucketType })
+ @IsNotEmpty()
+ timeBucket: TimeBucketType
+
+ @ApiProperty()
+ from: string
+
+ @ApiProperty()
+ to: string
+
+ @ApiProperty({
+ description: 'Timezone to display data in',
+ default: DEFAULT_TIMEZONE,
+ })
+ timezone: string
+}