📈 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!
TypeScript chartsclickhousedockergdprgoogle-analyticsmetricsmonitoringmysqlnestjsprivacyredisself-hostedselfhostedstatisticstailwindcssweb-analyticswebanalyticswebsite
saving visitor details to pageviews as well
Blue Mouse70dc3f815e80
diff --git a/src/analytics/analytics.controller.ts b/src/analytics/analytics.controller.tsindex 56f20759e..a30458702 100644--- a/src/analytics/analytics.controller.ts+++ b/src/analytics/analytics.controller.ts@@ -859,71 +859,43 @@ export class AnalyticsController {const salt = await redis.get(REDIS_SESSION_SALT_KEY)const sessionHash = getSessionKey(ip, userAgent, logDTO.pid, salt)const unique = await this.analyticsService.isUnique(sessionHash)+this.analyticsService.processInteractionSD(sessionHash, logDTO.pid)- let dto: Array<string | number>- let perfDTO: Array<string | number> = []- let ua: UAParser.IResult- let cc- if (unique) {- ua = UAParser(userAgent)- const dv = ua.device.type || 'desktop'- const br = ua.browser.name- const os = ua.os.name- // trying to get country from timezome, otherwise using CloudFlare's IP based country code as a fallback- cc =- ct.getCountryForTimezone(logDTO.tz)?.id ||- (headers['cf-ipcountry'] === 'XX' ? 'NULL' : headers['cf-ipcountry'])- dto = analyticsDTO(- sessionHash,- logDTO.pid,- logDTO.pg,- dv,- br,- os,- logDTO.lc,- logDTO.ref,- logDTO.so,- logDTO.me,- logDTO.ca,- cc,- 0,- 1,- )- } else if (!logDTO.unique) {- dto = analyticsDTO(- 'NULL',- logDTO.pid,- logDTO.pg,- 'NULL',- 'NULL',- 'NULL',- 'NULL',- 'NULL',- 'NULL',- 'NULL',- 'NULL',- 'NULL',- 'NULL',- 0,- )- } else {- throw new BadRequestException(- 'Event was not saved because it was not unique while unique only param is provided',+ if (unique && logDTO.unique) {+ throw new ForbiddenException(+ 'The event was not saved because it was not unique while unique only param is provided',)}- if (!_isEmpty(logDTO.perf) && isPerformanceValid(logDTO.perf)) {- if (!ua) {- ua = UAParser(userAgent)- }+ // trying to get country from timezome, otherwise using CloudFlare's IP based country code as a fallback+ const cc = ct.getCountryForTimezone(logDTO.tz)?.id || (headers['cf-ipcountry'] === 'XX' ? 'NULL' : headers['cf-ipcountry'])- if (!cc) {- cc =- ct.getCountryForTimezone(logDTO.tz)?.id ||- (headers['cf-ipcountry'] === 'XX' ? 'NULL' : headers['cf-ipcountry'])- }+ const ua = UAParser(userAgent)+ const dv = ua.device.type || 'desktop'+ const br = ua.browser.name+ const os = ua.os.name++ const dto = analyticsDTO(+ sessionHash,+ logDTO.pid,+ logDTO.pg,+ dv,+ br,+ os,+ logDTO.lc,+ logDTO.ref,+ logDTO.so,+ logDTO.me,+ logDTO.ca,+ cc,+ 0,+ Number(unique),+ )++ let perfDTO: Array<string | number> = []+ if (!_isEmpty(logDTO.perf) && isPerformanceValid(logDTO.perf)) {const dv = ua.device.type || 'desktop'const br = ua.browser.name@@ -998,58 +970,36 @@ export class AnalyticsController {await this.analyticsService.validate(logDTO, origin)- const ip =- headers['cf-connecting-ip'] || headers['x-forwarded-for'] || reqIP || ''-+ const ip = headers['cf-connecting-ip'] || headers['x-forwarded-for'] || reqIP || ''const salt = await redis.get(REDIS_SESSION_SALT_KEY)const sessionHash = getSessionKey(ip, userAgent, logDTO.pid, salt)const unique = await this.analyticsService.isUnique(sessionHash)+this.analyticsService.processInteractionSD(sessionHash, logDTO.pid)- let dto: Array<string | number>+ const ua = UAParser(userAgent)+ const dv = ua.device.type || 'desktop'+ const br = ua.browser.name+ const os = ua.os.name- if (unique) {- const ua = UAParser(userAgent)- const dv = ua.device.type || 'desktop'- const br = ua.browser.name- const os = ua.os.name- // using CloudFlare's IP based country code- const cc =- headers['cf-ipcountry'] === 'XX' ? 'NULL' : headers['cf-ipcountry']- dto = analyticsDTO(- sessionHash,- logDTO.pid,- 'NULL',- dv,- br,- os,- 'NULL',- 'NULL',- 'NULL',- 'NULL',- 'NULL',- cc,- 0,- 1,- )- } else {- dto = analyticsDTO(- 'NULL',- logDTO.pid,- 'NULL',- 'NULL',- 'NULL',- 'NULL',- 'NULL',- 'NULL',- 'NULL',- 'NULL',- 'NULL',- 'NULL',- 'NULL',- 0,- )- }+ // using CloudFlare's IP based country code+ const cc = headers['cf-ipcountry'] === 'XX' ? 'NULL' : headers['cf-ipcountry']+ const dto = analyticsDTO(+ sessionHash,+ logDTO.pid,+ 'NULL',+ dv,+ br,+ os,+ 'NULL',+ 'NULL',+ 'NULL',+ 'NULL',+ 'NULL',+ cc,+ 0,+ Number(unique),+ )const values = `(${dto.map(getElValue).join(',')})`try {diff --git a/src/analytics/analytics.service.ts b/src/analytics/analytics.service.tsindex 344ac8e5b..bf4e0590f 100644--- a/src/analytics/analytics.service.ts+++ b/src/analytics/analytics.service.ts@@ -185,6 +185,16 @@ const nullifyMissingElements = (results: any[], size?: number): number[] => {return copy}+const generateParamsQuery = (col: string, subQuery: string): string => {+ // TODO: ??? idk, something should be done to improve it++ if (col === 'pg') {+ return `SELECT ${col}, count(*) ${subQuery} AND ${col} IS NOT NULL GROUP BY ${col}`+ }++ return `SELECT ${col}, count(*) ${subQuery} AND ${col} IS NOT NULL AND unique='1' GROUP BY ${col}`+}+@Injectable()export class AnalyticsService {constructor(private readonly projectService: ProjectService) {}@@ -576,7 +586,7 @@ export class AnalyticsService {const params = {}const paramsPromises = _map(cols, async col => {- const query1 = `SELECT ${col}, count(*) ${subQuery} AND ${col} IS NOT NULL GROUP BY ${col}`+ const query1 = generateParamsQuery(col, subQuery)const res = await clickhouse.query(query1, paramsData).toPromise()params[col] = {}