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 analytics endpoint & minor improvements

Blue Mouse132c7a09d7cc
diff --git a/.env.example b/.env.example
index 5925673a4..c76b3ea1c 100644
--- a/.env.example
+++ b/.env.example
@@ -1,7 +1,7 @@
# Auth
JWT_SECRET=SOME_SECRET_TOKEN
-#Bcrypt Salt
+# Bcrypt Salt
PASSWORD_SALT=BCRYPT_SECRET_SALT
# Database
@@ -16,5 +16,6 @@ SMTP_PORT=1025
SMTP_USER='smtp_user_name'
SMTP_PASSWORD='smtp_password'
FROM_EMAIL='[email protected]'
+
# Use SMTP_MOCK variable for development
-SMTP_MOCK=true
\ No newline at end of file
+SMTP_MOCK=true
diff --git a/package.json b/package.json
index 8297df88d..a19387785 100644
--- a/package.json
+++ b/package.json
@@ -2,7 +2,7 @@
"name": "analytics_api",
"version": "0.0.1",
"description": "",
- "author": "",
+ "author": "Blaumaus",
"private": true,
"license": "UNLICENSED",
"scripts": {
@@ -35,6 +35,7 @@
"class-validator": "^0.13.1",
"cookie-parser": "^1.4.5",
"handlebars": "^4.7.7",
+ "lodash": "^4.17.21",
"moment": "^2.29.1",
"mysql": "^2.18.1",
"nodemailer": "^6.5.0",
diff --git a/src/analytics/analytics.controller.ts b/src/analytics/analytics.controller.ts
index 22593761c..ff33295fe 100644
--- a/src/analytics/analytics.controller.ts
+++ b/src/analytics/analytics.controller.ts
@@ -6,7 +6,7 @@ import { AnalyticsService } from './analytics.service'
import { ProjectService } from '../project/project.service'
import { UserType } from '../user/entities/user.entity'
import { Pagination } from '../common/pagination/pagination'
-import { Analytics } from './entity/analytics.entity'
+import { Analytics } from './entities/analytics.entity'
import { UserService } from '../user/user.service'
import { PageviewsDTO } from './dto/pageviews.dto'
import { AppLoggerService } from '../logger/logger.service'
@@ -20,5 +20,11 @@ export class AnalyticsController {
private readonly logger: AppLoggerService,
) {}
-
+ @Post('/')
+ async get(@Body() logDTO: PageviewsDTO): Promise<any> {
+ this.logger.log({ logDTO }, 'POST /log')
+ this.analyticsService.validate(logDTO)
+
+ return
+ }
}
diff --git a/src/analytics/analytics.module.ts b/src/analytics/analytics.module.ts
index 876237f6c..c5cf3c8ea 100644
--- a/src/analytics/analytics.module.ts
+++ b/src/analytics/analytics.module.ts
@@ -1,17 +1,19 @@
import { Module, forwardRef } from '@nestjs/common'
import { TypeOrmModule } from '@nestjs/typeorm'
-import { Analytics } from './entity/analytics.entity'
+import { Analytics } from './entities/analytics.entity'
import { AnalyticsService } from './analytics.service'
import { AnalyticsController } from './analytics.controller'
import { UserModule } from '../user/user.module'
import { AppLoggerModule } from '../logger/logger.module'
+import { ProjectModule } from '../project/project.module'
@Module({
imports: [
TypeOrmModule.forFeature([Analytics]),
forwardRef(() => UserModule),
AppLoggerModule,
+ ProjectModule,
],
providers: [AnalyticsService],
exports: [AnalyticsService],
diff --git a/src/analytics/analytics.service.ts b/src/analytics/analytics.service.ts
index 791a7791b..894bbdcc3 100644
--- a/src/analytics/analytics.service.ts
+++ b/src/analytics/analytics.service.ts
@@ -1,9 +1,10 @@
+import * as _isEmpty from 'lodash/isEmpty'
import { ForbiddenException, Injectable, BadRequestException } from '@nestjs/common'
import { InjectRepository } from '@nestjs/typeorm'
import { Repository } from 'typeorm'
import { Pagination, PaginationOptionsInterface } from '../common/pagination'
-import { Analytics } from './entity/analytics.entity'
+import { Analytics } from './entities/analytics.entity'
import { PageviewsDTO } from './dto/pageviews.dto'
import { ProjectService } from '../project/project.service'
@@ -54,4 +55,16 @@ export class AnalyticsService {
findOneWhere(where: Record<string, unknown>): Promise<Analytics> {
return this.analyticsRepository.findOne({ where })
}
+
+ validate(logDTO: PageviewsDTO): 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(errors)) {
+ throw new BadRequestException(errors)
+ }
+
+ return null
+ }
}
diff --git a/src/analytics/entity/analytics.entity.ts b/src/analytics/entities/analytics.entity.ts
similarity index 100%
rename from src/analytics/entity/analytics.entity.ts
rename to src/analytics/entities/analytics.entity.ts
diff --git a/src/app.module.ts b/src/app.module.ts
index fefc4e1e3..c877c0df4 100644
--- a/src/app.module.ts
+++ b/src/app.module.ts
@@ -5,8 +5,10 @@ import { ScheduleModule } from '@nestjs/schedule'
import { AuthModule } from './auth/auth.module'
import { UserModule } from './user/user.module'
+import { AnalyticsModule } from './analytics/analytics.module'
import { User } from './user/entities/user.entity'
import { Project } from './project/entity/project.entity'
+import { Analytics } from './analytics/entities/analytics.entity'
import { ProjectModule } from './project/project.module'
import { MailerModule } from './mailer/mailer.module'
import { ActionTokensModule } from './action-tokens/action-tokens.module'
@@ -24,7 +26,7 @@ import { TaskManagerModule } from './task-manager/task-manager.module'
password: process.env.MYSQL_ROOT_PASSWORD || '123123123',
database: process.env.MYSQL_DATABASE || 'analytics',
synchronize: true,
- entities: [User, ActionToken, Project],
+ entities: [User, ActionToken, Project, Analytics],
}),
ScheduleModule.forRoot(),
TaskManagerModule,
@@ -33,6 +35,7 @@ import { TaskManagerModule } from './task-manager/task-manager.module'
MailerModule,
ActionTokensModule,
ProjectModule,
+ AnalyticsModule,
],
})
diff --git a/src/auth/auth.service.ts b/src/auth/auth.service.ts
index 25685647a..a5fa58fcb 100644
--- a/src/auth/auth.service.ts
+++ b/src/auth/auth.service.ts
@@ -34,15 +34,15 @@ export class AuthService {
}
async login(user: User, res: Response): Promise<any> {
- const options = {
- httpOnly: true,
+ const options = {
+ httpOnly: true,
sameSite: false,
hostonly: true
- }
+ }
- const token = sign({ user_id: user.id }, process.env.JWT_SECRET, {
- expiresIn: JWT_LIFE_TIME,
- })
+ const token = sign({ user_id: user.id }, process.env.JWT_SECRET, {
+ expiresIn: JWT_LIFE_TIME,
+ })
res.cookie('token', token, options)
return { access_token: token, user }
diff --git a/src/user/entities/user.entity.ts b/src/user/entities/user.entity.ts
index 50f3d5683..fd17f3795 100644
--- a/src/user/entities/user.entity.ts
+++ b/src/user/entities/user.entity.ts
@@ -38,7 +38,7 @@ export class User {
@Column({
type: 'set',
enum: UserType,
- default: UserType.CUSTOMER
+ default: UserType.CUSTOMER,
})
roles: UserType[]