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!

Create project views endpoint

Yehor Dremliuha975b5728ae2f
diff --git a/apps/production/src/project/dto/create-project-view.dto.ts b/apps/production/src/project/dto/create-project-view.dto.ts
new file mode 100644
index 000000000..05a5f29ad
--- /dev/null
+++ b/apps/production/src/project/dto/create-project-view.dto.ts
@@ -0,0 +1,52 @@
+import { ApiProperty } from '@nestjs/swagger'
+
+export class CreateProjectViewDto {
+ @ApiProperty()
+ name: string
+
+ @ApiProperty({ description: 'Page the user viewed (/hello)', nullable: true })
+ pg: string | null
+
+ @ApiProperty({ description: 'Name of the custom event (e.g., sign_up)' })
+ ev: string
+
+ @ApiProperty({
+ description: 'User device (mobile, desktop, tablet, etc.)',
+ nullable: true,
+ })
+ dv: string | null
+
+ @ApiProperty({ description: 'Browser', nullable: true })
+ br: string | null
+
+ @ApiProperty({ description: 'Operating system', nullable: true })
+ os: string | null
+
+ @ApiProperty({ description: 'Locale (en-UK, en-US)', nullable: true })
+ lc: string | null
+
+ @ApiProperty({
+ description:
+ 'Referrer (site from which the user came to the site using Swetrix)',
+ nullable: true,
+ })
+ ref: string | null
+
+ @ApiProperty({ description: 'UTM source', nullable: true })
+ so: string | null
+
+ @ApiProperty({ description: 'UTM medium', nullable: true })
+ me: string | null
+
+ @ApiProperty({ description: 'UTM campaign', nullable: true })
+ ca: string | null
+
+ @ApiProperty({ description: 'Country code', nullable: true })
+ cc: string
+
+ @ApiProperty({ description: 'Region/state (Alabama, etc.)', nullable: true })
+ rg: string | null
+
+ @ApiProperty({ description: 'City (Berlin, London, etc.)', nullable: true })
+ ct: string | null
+}
diff --git a/apps/production/src/project/entity/project-view.entity.ts b/apps/production/src/project/entity/project-view.entity.ts
index a78a0fcf2..b7b19ee4b 100644
--- a/apps/production/src/project/entity/project-view.entity.ts
+++ b/apps/production/src/project/entity/project-view.entity.ts
@@ -24,10 +24,6 @@ export class ProjectViewEntity {
@Column()
projectId: string
- @ApiProperty()
- @Column({ type: 'bigint', nullable: true })
- psid: string | null
-
@ApiProperty()
@Column('varchar')
pg: string | null
diff --git a/apps/production/src/project/project.controller.ts b/apps/production/src/project/project.controller.ts
index cf850aa11..9a104d3c9 100644
--- a/apps/production/src/project/project.controller.ts
+++ b/apps/production/src/project/project.controller.ts
@@ -103,6 +103,7 @@ import {
import { ProjectsViewsRepository } from './repositories/projects-views.repository'
import { ProjectViewEntity } from './entity/project-view.entity'
import { ProjectIdDto } from './dto/project-id.dto'
+import { CreateProjectViewDto } from './dto/create-project-view.dto'
const PROJECTS_MAXIMUM = 50
@@ -1896,6 +1897,43 @@ export class ProjectController {
}
}
+ @ApiOperation({ summary: 'Create project view' })
+ @ApiOkResponse({ type: ProjectViewEntity })
+ @ApiBearerAuth()
+ @Post(':projectId/views')
+ @UseGuards(JwtAccessTokenGuard, RolesGuard)
+ @Roles(UserType.CUSTOMER, UserType.ADMIN)
+ async createProjectView(
+ @Param() params: ProjectIdDto,
+ @Body() body: CreateProjectViewDto,
+ @CurrentUserId() userId: string,
+ ) {
+ const project = await this.projectService.findProject(params.projectId, [
+ 'admin',
+ 'share',
+ ])
+
+ if (!project) {
+ throw new NotFoundException('Project not found.')
+ }
+
+ const user = await this.userService.findUserV2(userId, ['roles'])
+
+ if (!user) {
+ throw new NotFoundException('User not found.')
+ }
+
+ this.projectService.allowedToManage(project, userId, user.roles)
+
+ const createdProjectView =
+ await this.projectsViewsRepository.createProjectView(
+ params.projectId,
+ body,
+ )
+
+ return _omit(createdProjectView, ['project'])
+ }
+
@ApiOperation({ summary: 'Get project views' })
@ApiOkResponse({ type: ProjectViewEntity })
@ApiBearerAuth()
diff --git a/apps/production/src/project/repositories/projects-views.repository.ts b/apps/production/src/project/repositories/projects-views.repository.ts
index 6be7b2e09..5e9731b17 100644
--- a/apps/production/src/project/repositories/projects-views.repository.ts
+++ b/apps/production/src/project/repositories/projects-views.repository.ts
@@ -2,6 +2,7 @@ import { Injectable } from '@nestjs/common'
import { InjectRepository } from '@nestjs/typeorm'
import { Repository } from 'typeorm'
import { ProjectViewEntity } from '../entity/project-view.entity'
+import { CreateProjectViewDto } from '../dto/create-project-view.dto'
@Injectable()
export class ProjectsViewsRepository {
@@ -13,4 +14,11 @@ export class ProjectsViewsRepository {
async findViews(projectId: string) {
return this.viewsRepository.find({ projectId })
}
+
+ async createProjectView(projectId: string, data: CreateProjectViewDto) {
+ return this.viewsRepository.save({
+ project: { id: projectId },
+ ...data,
+ })
+ }
}