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!

improvements

Blue Mouse3a54eda3c4f4
diff --git a/src/analytics/analytics.controller.ts b/src/analytics/analytics.controller.ts
index ff33295fe..5a1dafa9c 100644
--- a/src/analytics/analytics.controller.ts
+++ b/src/analytics/analytics.controller.ts
@@ -1,5 +1,5 @@
import { Controller, Body, Query, Param, UseGuards, Get, Post, Put, Delete, BadRequestException,
- HttpCode, NotFoundException } from '@nestjs/common'
+ HttpCode, NotFoundException, InternalServerErrorException } from '@nestjs/common'
import { ApiTags, ApiQuery, ApiResponse } from '@nestjs/swagger'
import { AnalyticsService } from './analytics.service'
@@ -8,10 +8,13 @@ import { UserType } from '../user/entities/user.entity'
import { Pagination } from '../common/pagination/pagination'
import { Analytics } from './entities/analytics.entity'
import { UserService } from '../user/user.service'
+import { Roles } from '../common/decorators/roles.decorator'
+import { RolesGuard } from 'src/common/guards/roles.guard'
import { PageviewsDTO } from './dto/pageviews.dto'
import { AppLoggerService } from '../logger/logger.service'
@ApiTags('Analytics')
+@UseGuards(RolesGuard)
@Controller('log')
export class AnalyticsController {
constructor(
@@ -20,11 +23,26 @@ export class AnalyticsController {
private readonly logger: AppLoggerService,
) {}
+ @Get('/')
+ @UseGuards(RolesGuard)
+ @Roles(UserType.CUSTOMER, UserType.ADMIN)
+ async getData(pid: string): Promise<any> {
+ this.logger.log({ pid }, 'GET /log')
+
+ return
+ }
+
@Post('/')
- async get(@Body() logDTO: PageviewsDTO): Promise<any> {
+ async log(@Body() logDTO: PageviewsDTO): Promise<any> {
this.logger.log({ logDTO }, 'POST /log')
- this.analyticsService.validate(logDTO)
+ await this.analyticsService.validate(logDTO)
- return
+ try {
+ await this.analyticsService.create(logDTO)
+ return
+ } catch(e) {
+ this.logger.log(e)
+ throw new InternalServerErrorException(`Error error while saving the data: ${e}`)
+ }
}
}
diff --git a/src/analytics/analytics.service.ts b/src/analytics/analytics.service.ts
index 894bbdcc3..400f03bbc 100644
--- a/src/analytics/analytics.service.ts
+++ b/src/analytics/analytics.service.ts
@@ -36,7 +36,7 @@ export class AnalyticsService {
return this.analyticsRepository.count()
}
- async create(project: PageviewsDTO | Analytics): Promise<Analytics> {
+ async create(project: PageviewsDTO | Analytics): Promise<PageviewsDTO | Analytics> {
return this.analyticsRepository.save(project)
}
@@ -56,10 +56,13 @@ export class AnalyticsService {
return this.analyticsRepository.findOne({ where })
}
- validate(logDTO: PageviewsDTO): string | null {
+ async validate(logDTO: PageviewsDTO): Promise<string | null> {
const errors = []
if (_isEmpty(logDTO)) errors.push('The request cannot be empty')
- if (_isEmpty(logDTO.pid)) errors.push('The Project ID has to be provided')
+ if (_isEmpty(logDTO.pid)) errors.push('The Project ID (pid) has to be provided')
+
+ const project = await this.projectService.findOne(logDTO.pid)
+ if (_isEmpty(project)) errors.push('The provided Project ID (pid) is incorrect')
if (!_isEmpty(errors)) {
throw new BadRequestException(errors)
diff --git a/src/analytics/entities/analytics.entity.ts b/src/analytics/entities/analytics.entity.ts
index ab84b9667..3eb207fdb 100644
--- a/src/analytics/entities/analytics.entity.ts
+++ b/src/analytics/entities/analytics.entity.ts
@@ -1,4 +1,7 @@
-import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm'
+import { Entity, Column, PrimaryGeneratedColumn, CreateDateColumn, ManyToOne } from 'typeorm'
+import { ApiProperty } from '@nestjs/swagger'
+
+import { Project } from '../../project/entity/project.entity'
enum EventType {
PAGEVIEWS = 'pageviews',
@@ -12,6 +15,10 @@ export class Analytics {
@Column('varchar', { length: 11 })
pid: string
+ // @ApiProperty({ type: () => Project })
+ // @ManyToOne(() => Project, proj => proj.id)
+ // pid: Project
+
@Column({
type: 'enum',
enum: EventType,
@@ -19,30 +26,56 @@ export class Analytics {
})
ev: string
- @Column('varchar', { length: 6 })
+ @Column('varchar', {
+ length: 6,
+ nullable: true,
+ })
tz: string
- @Column('varchar', { length: 500 }) // maybe it should be 2083? https://support.microsoft.com/uk-ua/topic/maximum-url-length-is-2-083-characters-in-internet-explorer-174e7c8a-6666-f4e0-6fd6-908b53c12246
+ @Column('varchar', {
+ length: 500,
+ nullable: true,
+ }) // maybe it should be 2083? https://support.microsoft.com/uk-ua/topic/maximum-url-length-is-2-083-characters-in-internet-explorer-174e7c8a-6666-f4e0-6fd6-908b53c12246
pg: string
- @Column('varchar', { length: 8 })
+ @Column('varchar', {
+ length: 8,
+ nullable: true,
+ })
lc: string
- @Column('varchar', { length: 500 }) // same question as pg
+ @Column('varchar', {
+ length: 500,
+ nullable: true,
+ }) // same question as pg
ref: string
- @Column()
+ @Column({
+ nullable: true,
+ })
sw: number
- @Column('varchar', { length: 100 })
+ @Column('varchar', {
+ length: 100,
+ nullable: true,
+ })
so: string
- @Column('varchar', { length: 100 })
+ @Column('varchar', {
+ length: 100,
+ nullable: true,
+ })
me: string
- @Column('varchar', { length: 100 })
+ @Column('varchar', {
+ length: 100,
+ nullable: true,
+ })
ca: string
- @Column()
+ @Column({ nullable: true })
lt: number
+
+ @CreateDateColumn()
+ created: Date;
}