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!

fix coderabbit PR review

Blue Mousea659a1d364f2
diff --git a/backend/apps/cloud/src/admin/admin.service.ts b/backend/apps/cloud/src/admin/admin.service.ts
index ce99d3bac..45dc35171 100644
--- a/backend/apps/cloud/src/admin/admin.service.ts
+++ b/backend/apps/cloud/src/admin/admin.service.ts
@@ -2139,6 +2139,7 @@ export class AdminService {
FROM events
WHERE pid IN ({pids:Array(FixedString(12))})
AND type IN ('pageview', 'custom_event', 'error', 'captcha', 'outbound')
+ AND importID IS NULL
AND created >= {from:String}
GROUP BY pid
`
diff --git a/backend/apps/cloud/src/analytics/v2/__tests__/outbound-links.spec.ts b/backend/apps/cloud/src/analytics/v2/__tests__/outbound-links.spec.ts
index a871ea124..d91b65379 100644
--- a/backend/apps/cloud/src/analytics/v2/__tests__/outbound-links.spec.ts
+++ b/backend/apps/cloud/src/analytics/v2/__tests__/outbound-links.spec.ts
@@ -1,3 +1,4 @@
+import { UnprocessableEntityException } from '@nestjs/common'
import { plainToInstance } from 'class-transformer'
import { validate } from 'class-validator'
import { AnalyticsService } from '../../analytics.service'
@@ -118,6 +119,19 @@ describe('Outbound links', () => {
).rejects.toThrow('clicks and visitors')
})
+ it.each(['page', 'entry_page', 'exit_page'])(
+ 'rejects clicks for the %s dimension',
+ async (dimension) => {
+ const analytics = Object.create(AnalyticsService.prototype)
+ const service = new AnalyticsV2Service(analytics, {} as never)
+ for (const metrics of ['clicks', 'visitors,clicks']) {
+ await expect(
+ service.getBreakdown(pid, { dimension, metrics }, 'traffic'),
+ ).rejects.toThrow(UnprocessableEntityException)
+ }
+ },
+ )
+
it('keeps normal traffic queries on pageviews', async () => {
const analytics = Object.create(AnalyticsService.prototype)
const service = new AnalyticsV2Service(analytics, {} as never)
diff --git a/backend/apps/cloud/src/analytics/v2/analytics-v2.service.ts b/backend/apps/cloud/src/analytics/v2/analytics-v2.service.ts
index bb29fca78..a26999830 100644
--- a/backend/apps/cloud/src/analytics/v2/analytics-v2.service.ts
+++ b/backend/apps/cloud/src/analytics/v2/analytics-v2.service.ts
@@ -294,6 +294,11 @@ export class AnalyticsV2Service {
'Outbound links support the clicks and visitors metrics',
)
}
+ if (!isOutbound && metrics.some((metric) => metric.api === 'clicks')) {
+ throw new UnprocessableEntityException(
+ 'The clicks metric is only supported for outbound_link',
+ )
+ }
const filters = this.prepareFilters(
dto.filters,
dataType,
diff --git a/backend/apps/community/src/analytics/v2/__tests__/outbound-links.spec.ts b/backend/apps/community/src/analytics/v2/__tests__/outbound-links.spec.ts
index a871ea124..d91b65379 100644
--- a/backend/apps/community/src/analytics/v2/__tests__/outbound-links.spec.ts
+++ b/backend/apps/community/src/analytics/v2/__tests__/outbound-links.spec.ts
@@ -1,3 +1,4 @@
+import { UnprocessableEntityException } from '@nestjs/common'
import { plainToInstance } from 'class-transformer'
import { validate } from 'class-validator'
import { AnalyticsService } from '../../analytics.service'
@@ -118,6 +119,19 @@ describe('Outbound links', () => {
).rejects.toThrow('clicks and visitors')
})
+ it.each(['page', 'entry_page', 'exit_page'])(
+ 'rejects clicks for the %s dimension',
+ async (dimension) => {
+ const analytics = Object.create(AnalyticsService.prototype)
+ const service = new AnalyticsV2Service(analytics, {} as never)
+ for (const metrics of ['clicks', 'visitors,clicks']) {
+ await expect(
+ service.getBreakdown(pid, { dimension, metrics }, 'traffic'),
+ ).rejects.toThrow(UnprocessableEntityException)
+ }
+ },
+ )
+
it('keeps normal traffic queries on pageviews', async () => {
const analytics = Object.create(AnalyticsService.prototype)
const service = new AnalyticsV2Service(analytics, {} as never)
diff --git a/backend/apps/community/src/analytics/v2/analytics-v2.service.ts b/backend/apps/community/src/analytics/v2/analytics-v2.service.ts
index 5c7515311..11d8c24ae 100644
--- a/backend/apps/community/src/analytics/v2/analytics-v2.service.ts
+++ b/backend/apps/community/src/analytics/v2/analytics-v2.service.ts
@@ -293,6 +293,11 @@ export class AnalyticsV2Service {
'Outbound links support the clicks and visitors metrics',
)
}
+ if (!isOutbound && metrics.some((metric) => metric.api === 'clicks')) {
+ throw new UnprocessableEntityException(
+ 'The clicks metric is only supported for outbound_link',
+ )
+ }
const filters = this.prepareFilters(
dto.filters,
dataType,
diff --git a/packages/tracker-js/src/Lib.ts b/packages/tracker-js/src/Lib.ts
index 970973bcc..759b8ae5d 100644
--- a/packages/tracker-js/src/Lib.ts
+++ b/packages/tracker-js/src/Lib.ts
@@ -450,6 +450,7 @@ export class Lib {
void this.sendRequest('outbound', {
...this.getEventContext(),
+ qs: getQueryString()?.slice(0, 2048),
url: url.href,
title: document.title?.slice(0, 2048) || undefined,
}).catch(() => undefined)
diff --git a/packages/tracker-js/tests/outbound.test.ts b/packages/tracker-js/tests/outbound.test.ts
index dd66ab78c..27a55fda7 100644
--- a/packages/tracker-js/tests/outbound.test.ts
+++ b/packages/tracker-js/tests/outbound.test.ts
@@ -56,6 +56,28 @@ describe('Outbound link capture', () => {
)
})
+ it.each([
+ ['', undefined],
+ ['?q=short', 'q=short'],
+ [`?${'x'.repeat(2048)}`, 'x'.repeat(2048)],
+ [`?${'x'.repeat(2049)}`, 'x'.repeat(2048)],
+ ])('limits the source query string (%#)', (search, qs) => {
+ setLocation({ search })
+ create({ events: { outboundLinks: true } })
+ click('https://tickets.example.org/event')
+ expect(request).toHaveBeenCalledWith('outbound', expect.objectContaining({ qs }))
+ })
+
+ it('preserves campaign fields beyond the query string limit', () => {
+ setLocation({ search: `?q=${'x'.repeat(2048)}&utm_source=newsletter&utm_campaign=launch` })
+ create({ events: { outboundLinks: true } })
+ click('https://tickets.example.org/event')
+ expect(request).toHaveBeenCalledWith(
+ 'outbound',
+ expect.objectContaining({ qs: `q=${'x'.repeat(2046)}`, so: 'newsletter', ca: 'launch' }),
+ )
+ })
+
it.each([
'/inside',
'#section',