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: validate goal filters and align session counts

Blue Mouse3091055f8443
diff --git a/backend/apps/cloud/src/goal/goal.controller.ts b/backend/apps/cloud/src/goal/goal.controller.ts
index 765528149..d6b6578c8 100644
--- a/backend/apps/cloud/src/goal/goal.controller.ts
+++ b/backend/apps/cloud/src/goal/goal.controller.ts
@@ -914,6 +914,7 @@ export class GoalController {
WHERE
pid = {pid:FixedString(12)}
AND type IN ('pageview', 'custom_event')
+ AND psid != 0
${filtersQuery}
AND created BETWEEN {groupFrom:String} AND {groupTo:String}
`
diff --git a/backend/apps/community/src/analytics/analytics.service.ts b/backend/apps/community/src/analytics/analytics.service.ts
index 80e461c98..802b3951a 100644
--- a/backend/apps/community/src/analytics/analytics.service.ts
+++ b/backend/apps/community/src/analytics/analytics.service.ts
@@ -2150,7 +2150,7 @@ export class AnalyticsService {
GROUP BY psid
),
session_starts AS (
- SELECT fs.psid, min(s.firstSeen) AS sessionStart
+ SELECT fs.psid, s.firstSeen AS sessionStart
FROM funnel_sessions fs
INNER JOIN (
SELECT psid, firstSeen, lastSeen
@@ -2159,7 +2159,6 @@ export class AnalyticsService {
) s ON s.psid = fs.psid
WHERE s.firstSeen <= fs.firstStepAt
AND s.lastSeen >= fs.conversionAt
- GROUP BY fs.psid
),
first_pages AS (
SELECT fs.psid, min(e.created) AS firstPageAt
diff --git a/backend/apps/community/src/goal/goal.controller.ts b/backend/apps/community/src/goal/goal.controller.ts
index 13aab725d..0a8e160e4 100644
--- a/backend/apps/community/src/goal/goal.controller.ts
+++ b/backend/apps/community/src/goal/goal.controller.ts
@@ -832,6 +832,7 @@ export class GoalController {
WHERE
pid = {pid:FixedString(12)}
AND type IN ('pageview', 'custom_event')
+ AND psid != 0
${filtersQuery}
AND created BETWEEN {groupFrom:String} AND {groupTo:String}
`
diff --git a/web/app/pages/Project/View/components/FilterRowsEditor.tsx b/web/app/pages/Project/View/components/FilterRowsEditor.tsx
index 6493dff14..c1b8208b9 100644
--- a/web/app/pages/Project/View/components/FilterRowsEditor.tsx
+++ b/web/app/pages/Project/View/components/FilterRowsEditor.tsx
@@ -76,7 +76,7 @@ const getMetadataColumnBase = (column: string) => {
}
const getOperatorsForColumn = (column: string) => {
- if (column === 'ev:key' || column === 'tag:key') {
+ if (column.startsWith('ev:key') || column.startsWith('tag:key')) {
return OPERATORS.filter(
(operator) => operator.value === 'is' || operator.value === 'isNot',
)
@@ -354,7 +354,11 @@ const FilterRowsEditor = ({
const fetchFilterValues = useCallback(
async (column: string) => {
- if (getMetadataColumnBase(column) !== column || column.includes(':')) {
+ if (
+ (getMetadataColumnBase(column) !== column || column.includes(':')) &&
+ !column.startsWith('ev:') &&
+ !column.startsWith('tag:')
+ ) {
setFilterValuesCache((prev) =>
prev[column] ? prev : { ...prev, [column]: [] },
)
diff --git a/web/app/routes/projects.$id.tsx b/web/app/routes/projects.$id.tsx
index a2e60a905..4fcd07e1e 100644
--- a/web/app/routes/projects.$id.tsx
+++ b/web/app/routes/projects.$id.tsx
@@ -63,7 +63,40 @@ import {
} from '~/utils/session.server'
const GOAL_CONDITION_RELATIONS = new Set(['AND', 'OR'])
-const GOAL_CONDITION_EVENT_TYPES = new Set(['any', 'pageview', 'custom_event'])
+const GOAL_CONDITION_COMMON_FIELDS = [
+ 'host',
+ 'ref',
+ 'so',
+ 'me',
+ 'ca',
+ 'te',
+ 'co',
+ 'cc',
+ 'dv',
+ 'br',
+ 'os',
+]
+const GOAL_CONDITION_EVENT_TYPES: Record<string, Set<string>> = {
+ any: new Set([
+ ...GOAL_CONDITION_COMMON_FIELDS,
+ 'page',
+ 'pg',
+ 'event',
+ 'event_name',
+ ]),
+ pageview: new Set([
+ ...GOAL_CONDITION_COMMON_FIELDS,
+ 'page',
+ 'pg',
+ 'metadata',
+ ]),
+ custom_event: new Set([
+ ...GOAL_CONDITION_COMMON_FIELDS,
+ 'event',
+ 'event_name',
+ 'metadata',
+ ]),
+}
const GOAL_CONDITION_OPERATORS = new Set([
'equals',
'not_equals',
@@ -72,6 +105,12 @@ const GOAL_CONDITION_OPERATORS = new Set([
'exists',
'not_exists',
])
+const GOAL_CONDITION_VALUE_OPERATORS = new Set([
+ 'equals',
+ 'not_equals',
+ 'contains',
+ 'not_contains',
+])
type GoalMetadataFilter = {
key: string
@@ -176,17 +215,47 @@ const isGoalConditions = (value: unknown) => {
return (
Array.isArray(value.conditions) &&
- value.conditions.every(
- (condition) =>
- isRecord(condition) &&
- GOAL_CONDITION_EVENT_TYPES.has(String(condition.eventType)) &&
- typeof condition.field === 'string' &&
- GOAL_CONDITION_OPERATORS.has(String(condition.operator)) &&
- (condition.value === undefined ||
- typeof condition.value === 'string') &&
- (condition.metadataKey === undefined ||
- typeof condition.metadataKey === 'string'),
- )
+ value.conditions.every((condition) => {
+ if (
+ !isRecord(condition) ||
+ typeof condition.eventType !== 'string' ||
+ typeof condition.field !== 'string' ||
+ typeof condition.operator !== 'string'
+ ) {
+ return false
+ }
+
+ const allowedFields = GOAL_CONDITION_EVENT_TYPES[condition.eventType]
+ if (
+ !allowedFields ||
+ !allowedFields.has(condition.field) ||
+ !GOAL_CONDITION_OPERATORS.has(condition.operator)
+ ) {
+ return false
+ }
+
+ if (condition.field === 'metadata') {
+ if (
+ typeof condition.metadataKey !== 'string' ||
+ condition.metadataKey.trim() === ''
+ ) {
+ return false
+ }
+ } else if (
+ condition.metadataKey !== undefined &&
+ typeof condition.metadataKey !== 'string'
+ ) {
+ return false
+ }
+
+ if (GOAL_CONDITION_VALUE_OPERATORS.has(condition.operator)) {
+ return typeof condition.value === 'string' && condition.value !== ''
+ }
+
+ return (
+ condition.value === undefined || typeof condition.value === 'string'
+ )
+ })
)
}