Skip to content

🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.

Enhance filter functionality with new operators and validation (#752)

Bill Yangb6cb3bb407ef
diff --git a/client/src/app/[site]/components/SubHeader/Filters/FilterComponent.tsx b/client/src/app/[site]/components/SubHeader/Filters/FilterComponent.tsx
index 33edbf045..e4aa766ef 100644
--- a/client/src/app/[site]/components/SubHeader/Filters/FilterComponent.tsx
+++ b/client/src/app/[site]/components/SubHeader/Filters/FilterComponent.tsx
@@ -1,11 +1,35 @@
import { Filter, FilterParameter, FilterType } from "@rybbit/shared";
-import { Trash } from "lucide-react";
+import { HelpCircle, Trash } from "lucide-react";
+import { useMemo } from "react";
import { Button } from "../../../../../components/ui/button";
+import { Input } from "../../../../../components/ui/input";
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "../../../../../components/ui/select";
-import { FilterOptions, OperatorOptions } from "./const";
+import { Tooltip, TooltipContent, TooltipTrigger } from "../../../../../components/ui/tooltip";
+import { FilterOptions, StringOperatorOptions, NumericOperatorOptions, isNumericParameter } from "./const";
import { ValueSelect } from "./ValueSelect";
import { IS_CLOUD } from "../../../../../lib/const";
+// Validate regex pattern and return error message if invalid
+function validateRegex(pattern: string): string | null {
+ if (!pattern) return null;
+
+ try {
+ new RegExp(pattern);
+ return null;
+ } catch (e) {
+ return e instanceof Error ? e.message : "Invalid regex pattern";
+ }
+}
+
+const REGEX_EXAMPLES = [
+ { pattern: "^/blog/", description: "Paths starting with /blog/" },
+ { pattern: "/blog/.*", description: "Paths containing /blog/ followed by anything" },
+ { pattern: "\\.(pdf|doc|docx)$", description: "Paths ending in .pdf, .doc, or .docx" },
+ { pattern: "^/products/[0-9]+$", description: "Product pages with numeric IDs" },
+ { pattern: "(?i)newsletter", description: "Case-insensitive match for 'newsletter'" },
+ { pattern: "^(?!.*test).*$", description: "Paths NOT containing 'test'" },
+];
+
export function FilterComponent({
filter,
index,
@@ -23,21 +47,50 @@ export function FilterComponent({
)
: FilterOptions.filter(option => IS_CLOUD || !option.cloudOnly);
+ const isNumeric = isNumericParameter(filter.parameter);
+ const operatorOptions = isNumeric ? NumericOperatorOptions : StringOperatorOptions;
+
+ // Check if we need a text input instead of multi-select
+ const needsTextInput =
+ isNumeric ||
+ filter.type === "regex" ||
+ filter.type === "not_regex" ||
+ filter.type === "greater_than" ||
+ filter.type === "less_than";
+
+ const isRegexFilter = filter.type === "regex" || filter.type === "not_regex";
+ const regexError = useMemo(() => {
+ if (!isRegexFilter) return null;
+ return validateRegex(String(filter.value[0] ?? ""));
+ }, [isRegexFilter, filter.value]);
+
+ const handleParameterChange = (value: string) => {
+ const newParam = value as FilterParameter;
+ const newIsNumeric = isNumericParameter(newParam);
+
+ // Reset filter type if switching between numeric and string parameters
+ // and current type is not compatible
+ let newType = filter.type;
+ if (newIsNumeric && (filter.type === "contains" || filter.type === "not_contains" || filter.type === "regex" || filter.type === "not_regex")) {
+ newType = "equals";
+ } else if (!newIsNumeric && (filter.type === "greater_than" || filter.type === "less_than")) {
+ newType = "equals";
+ }
+
+ updateFilter(
+ {
+ ...filter,
+ value: [],
+ parameter: newParam,
+ type: newType,
+ },
+ index
+ );
+ };
+
return (
- <div className="grid grid-cols-[220px_auto] md:grid-cols-[160px_100px_250px_auto] gap-2">
- <Select
- onValueChange={value => {
- updateFilter(
- {
- ...filter,
- value: [],
- parameter: value as FilterParameter,
- },
- index
- );
- }}
- value={filter.parameter}
- >
+ <div className="grid grid-cols-[220px_auto] md:grid-cols-[160px_130px_250px_auto] gap-2">
+ <Select onValueChange={handleParameterChange} value={filter.parameter}>
<SelectTrigger>
<SelectValue placeholder="Filter" />
</SelectTrigger>
@@ -59,6 +112,8 @@ export function FilterComponent({
{
...filter,
type: value as FilterType,
+ // Clear value when switching to/from regex or numeric comparison
+ value: [],
},
index
);
@@ -68,20 +123,62 @@ export function FilterComponent({
<SelectValue placeholder="Operator" />
</SelectTrigger>
<SelectContent>
- {OperatorOptions.map(option => (
+ {operatorOptions.map(option => (
<SelectItem key={option.value} value={option.value}>
{option.label}
</SelectItem>
))}
</SelectContent>
</Select>
- <ValueSelect
- onChange={value => {
- updateFilter({ ...filter, value: value }, index);
- }}
- parameter={filter.parameter}
- value={filter.value}
- />
+ {needsTextInput ? (
+ <div className="flex items-center gap-1">
+ <div className="flex-1 relative">
+ <Input
+ value={String(filter.value[0] ?? "")}
+ onChange={e => {
+ updateFilter({ ...filter, value: [e.target.value] }, index);
+ }}
+ placeholder={isRegexFilter ? "e.g. ^/blog/.*" : "Enter value..."}
+ className={`h-9 ${regexError ? "border-red-500 focus-visible:ring-red-500" : ""}`}
+ />
+ {regexError && (
+ <div className="absolute -bottom-5 left-0 text-xs text-red-500 truncate max-w-full">
+ {regexError}
+ </div>
+ )}
+ </div>
+ {isRegexFilter && (
+ <Tooltip>
+ <TooltipTrigger asChild>
+ <Button variant="ghost" size="icon" className="h-9 w-9 shrink-0" type="button">
+ <HelpCircle className="h-4 w-4 text-neutral-500" />
+ </Button>
+ </TooltipTrigger>
+ <TooltipContent side="bottom" className="max-w-xs">
+ <div className="space-y-2">
+ <p className="font-medium">Regex Examples:</p>
+ <ul className="text-xs space-y-1">
+ {REGEX_EXAMPLES.map((ex, i) => (
+ <li key={i}>
+ <code className="bg-neutral-100 dark:bg-neutral-800 px-1 rounded">{ex.pattern}</code>
+ <span className="text-neutral-500 ml-1">— {ex.description}</span>
+ </li>
+ ))}
+ </ul>
+ </div>
+ </TooltipContent>
+ </Tooltip>
+ )}
+ </div>
+ ) : (
+ <ValueSelect
+ onChange={value => {
+ updateFilter({ ...filter, value: value }, index);
+ }}
+ parameter={filter.parameter}
+ value={filter.value}
+ />
+ )}
<Button
variant={"ghost"}
size={"icon"}
diff --git a/client/src/app/[site]/components/SubHeader/Filters/Filters.tsx b/client/src/app/[site]/components/SubHeader/Filters/Filters.tsx
index 5a0fdfeac..e508ae514 100644
--- a/client/src/app/[site]/components/SubHeader/Filters/Filters.tsx
+++ b/client/src/app/[site]/components/SubHeader/Filters/Filters.tsx
@@ -4,6 +4,7 @@ import { Tooltip, TooltipContent, TooltipTrigger } from "../../../../../componen
import { useGetRegionName } from "../../../../../lib/geo";
import { removeFilter, updateFilter, useStore } from "../../../../../lib/store";
import { cn } from "../../../../../lib/utils";
+import { isNumericParameter } from "./const";
import { filterTypeToLabel, getParameterNameLabel, getParameterValueLabel } from "./utils";
export function Filters({ availableFilters }: { availableFilters?: FilterParameter[] }) {
@@ -40,18 +41,39 @@ export function Filters({ availableFilters }: { availableFilters?: FilterParamet
<div
className={cn(
"text-emerald-400 font cursor-pointer whitespace-nowrap",
- (filter.type === "not_equals" || filter.type === "not_contains") && "text-red-400"
+ (filter.type === "not_equals" || filter.type === "not_contains" || filter.type === "not_regex") &&
+ "text-red-400"
)}
onClick={() => {
+ const isNumeric = isNumericParameter(filter.parameter);
let newType: FilterType = "equals";
- if (filter.type === "equals") {
- newType = "not_equals";
- } else if (filter.type === "not_equals") {
- newType = "contains";
- } else if (filter.type === "contains") {
- newType = "not_contains";
- } else if (filter.type === "not_contains") {
- newType = "equals";
+
+ if (isNumeric) {
+ // Numeric cycle: equals -> not_equals -> greater_than -> less_than -> equals
+ if (filter.type === "equals") {
+ newType = "not_equals";
+ } else if (filter.type === "not_equals") {
+ newType = "greater_than";
+ } else if (filter.type === "greater_than") {
+ newType = "less_than";
+ } else if (filter.type === "less_than") {
+ newType = "equals";
+ }
+ } else {
+ // String cycle: equals -> not_equals -> contains -> not_contains -> regex -> not_regex -> equals
+ if (filter.type === "equals") {
+ newType = "not_equals";
+ } else if (filter.type === "not_equals") {
+ newType = "contains";
+ } else if (filter.type === "contains") {
+ newType = "not_contains";
+ } else if (filter.type === "not_contains") {
+ newType = "regex";
+ } else if (filter.type === "regex") {
+ newType = "not_regex";
+ } else if (filter.type === "not_regex") {
+ newType = "equals";
+ }
}
updateFilter({ ...filter, type: newType }, i);
diff --git a/client/src/app/[site]/components/SubHeader/Filters/const.tsx b/client/src/app/[site]/components/SubHeader/Filters/const.tsx
index 1ce228761..fd086688b 100644
--- a/client/src/app/[site]/components/SubHeader/Filters/const.tsx
+++ b/client/src/app/[site]/components/SubHeader/Filters/const.tsx
@@ -240,3 +240,24 @@ export const OperatorOptions = [
{ label: "Contains", value: "contains" },
{ label: "Not contains", value: "not_contains" },
];
+
+export const StringOperatorOptions = [
+ { label: "Is", value: "equals" },
+ { label: "Is not", value: "not_equals" },
+ { label: "Contains", value: "contains" },
+ { label: "Not contains", value: "not_contains" },
+ { label: "Matches regex", value: "regex" },
+ { label: "Not matches regex", value: "not_regex" },
+];
+
+export const NumericOperatorOptions = [
+ { label: "Equals", value: "equals" },
+ { label: "Not equals", value: "not_equals" },
+ { label: ">", value: "greater_than" },
+ { label: "<", value: "less_than" },
+];
+
+export const NUMERIC_PARAMETERS: FilterParameter[] = ["lat", "lon"];
+
+export const isNumericParameter = (param: FilterParameter): boolean =>
+ NUMERIC_PARAMETERS.includes(param);
diff --git a/client/src/app/[site]/components/SubHeader/Filters/utils.ts b/client/src/app/[site]/components/SubHeader/Filters/utils.ts
index e5e83a3ef..902a519f0 100644
--- a/client/src/app/[site]/components/SubHeader/Filters/utils.ts
+++ b/client/src/app/[site]/components/SubHeader/Filters/utils.ts
@@ -92,6 +92,14 @@ export const filterTypeToLabel = (type: FilterType) => {
return "contains";
case "not_contains":
return "not contains";
+ case "regex":
+ return "matches";
+ case "not_regex":
+ return "not matches";
+ case "greater_than":
+ return ">";
+ case "less_than":
+ return "<";
default:
return type;
}
diff --git a/client/src/components/MultiSelect.tsx b/client/src/components/MultiSelect.tsx
index bfc925328..4d0dc5fa3 100644
--- a/client/src/components/MultiSelect.tsx
+++ b/client/src/components/MultiSelect.tsx
@@ -24,7 +24,7 @@ export const MultiSelect = forwardRef<any, MultiSelectProps<any>>(({ className,
classNames={{
control: ({ isFocused }) =>
cn(
- "flex min-h-[36px] rounded-md px-3 py-[3px] text-sm shadow-sm transition-colors border border-neutral-200 dark:border-neutral-750",
+ "flex h-[36px] box-border rounded-md px-3 py-[3px] text-sm shadow-sm transition-colors border border-neutral-200 dark:border-neutral-750",
"placeholder:text-muted-foreground ",
"disabled:cursor-not-allowed disabled:opacity-50",
isFocused && "ring-1 ring-ring",
diff --git a/client/src/components/ui/tooltip.tsx b/client/src/components/ui/tooltip.tsx
index 31f40bb3a..41f800d6c 100644
--- a/client/src/components/ui/tooltip.tsx
+++ b/client/src/components/ui/tooltip.tsx
@@ -22,7 +22,7 @@ const TooltipContent = React.forwardRef<
ref={ref}
sideOffset={sideOffset}
className={cn(
- "z-50 overflow-hidden rounded-md bg-neutral-900 border border-neutral-700 px-3 py-1.5 text-xs text-neutral-50 animate-in fade-in-0 zoom-in-95 data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=closed]:zoom-out-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2 dark:bg-neutral-850 dark:border-neutral-750",
+ "z-50 overflow-hidden rounded-md bg-white border border-neutral-100 px-2 py-2 text-xs text-neutral-900 animate-in fade-in-0 zoom-in-95 data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=closed]:zoom-out-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2 dark:bg-neutral-800 dark:border-neutral-750 dark:text-neutral-50",
className
)}
{...props}
diff --git a/server/src/api/admin/getAdminServiceEventCount.ts b/server/src/api/admin/getAdminServiceEventCount.ts
index b86ea84c2..fe48e4da3 100644
--- a/server/src/api/admin/getAdminServiceEventCount.ts
+++ b/server/src/api/admin/getAdminServiceEventCount.ts
@@ -2,7 +2,7 @@ import { FastifyReply, FastifyRequest } from "fastify";
import SqlString from "sqlstring";
import { clickhouse } from "../../db/clickhouse/clickhouse.js";
import { getIsUserAdmin } from "../../lib/auth-utils.js";
-import { processResults } from "../analytics/utils.js";
+import { processResults } from "../analytics/utils/utils.js";
type ServiceEventCountResponse = {
event_date: string;
diff --git a/server/src/api/analytics/events/getEventNames.ts b/server/src/api/analytics/events/getEventNames.ts
index ab10fb405..2dd98f317 100644
--- a/server/src/api/analytics/events/getEventNames.ts
+++ b/server/src/api/analytics/events/getEventNames.ts
@@ -1,7 +1,8 @@
import { FastifyReply, FastifyRequest } from "fastify";
import { clickhouse } from "../../../db/clickhouse/clickhouse.js";
-import { getTimeStatement, processResults, getFilterStatement } from "../utils.js";
+import { getTimeStatement, processResults } from "../utils/utils.js";
import { FilterParams } from "@rybbit/shared";
+import { getFilterStatement } from "../utils/getFilterStatement.js";
export type GetEventNamesResponse = {
eventName: string;
diff --git a/server/src/api/analytics/events/getEventProperties.ts b/server/src/api/analytics/events/getEventProperties.ts
index 5920e5110..ee186f423 100644
--- a/server/src/api/analytics/events/getEventProperties.ts
+++ b/server/src/api/analytics/events/getEventProperties.ts
@@ -1,7 +1,8 @@
import { FastifyReply, FastifyRequest } from "fastify";
import { clickhouse } from "../../../db/clickhouse/clickhouse.js";
-import { getTimeStatement, processResults, getFilterStatement } from "../utils.js";
+import { getTimeStatement, processResults } from "../utils/utils.js";
import { FilterParams } from "@rybbit/shared";
+import { getFilterStatement } from "../utils/getFilterStatement.js";
export type GetEventPropertiesResponse = {
propertyKey: string;
diff --git a/server/src/api/analytics/events/getEvents.ts b/server/src/api/analytics/events/getEvents.ts
index c1db44d64..6d93ab033 100644
--- a/server/src/api/analytics/events/getEvents.ts
+++ b/server/src/api/analytics/events/getEvents.ts
@@ -1,7 +1,8 @@
import { FastifyReply, FastifyRequest } from "fastify";
import { clickhouse } from "../../../db/clickhouse/clickhouse.js";
-import { processResults, getTimeStatement, getFilterStatement } from "../utils.js";
+import { processResults, getTimeStatement } from "../utils/utils.js";
import { FilterParams } from "@rybbit/shared";
+import { getFilterStatement } from "../utils/getFilterStatement.js";
export type GetEventsResponse = {
timestamp: string;
diff --git a/server/src/api/analytics/events/getOutboundLinks.ts b/server/src/api/analytics/events/getOutboundLinks.ts
index 2d1af82b4..18fc5be1e 100644
--- a/server/src/api/analytics/events/getOutboundLinks.ts
+++ b/server/src/api/analytics/events/getOutboundLinks.ts
@@ -1,7 +1,8 @@
import { FastifyReply, FastifyRequest } from "fastify";
import { clickhouse } from "../../../db/clickhouse/clickhouse.js";
-import { getTimeStatement, processResults, getFilterStatement } from "../utils.js";
+import { getTimeStatement, processResults } from "../utils/utils.js";
import { FilterParams } from "@rybbit/shared";
+import { getFilterStatement } from "../utils/getFilterStatement.js";
export type GetOutboundLinksResponse = {
url: string;
diff --git a/server/src/api/analytics/funnels/getFunnel.ts b/server/src/api/analytics/funnels/getFunnel.ts
index 2dcbf1b74..8ad158edd 100644
--- a/server/src/api/analytics/funnels/getFunnel.ts
+++ b/server/src/api/analytics/funnels/getFunnel.ts
@@ -2,7 +2,8 @@ import { FilterParams } from "@rybbit/shared";
import { FastifyReply, FastifyRequest } from "fastify";
import SqlString from "sqlstring";
import { clickhouse } from "../../../db/clickhouse/clickhouse.js";
-import { getFilterStatement, getTimeStatement, patternToRegex, processResults } from "../utils.js";
+import { getTimeStatement, patternToRegex, processResults } from "../utils/utils.js";
+import { getFilterStatement } from "../utils/getFilterStatement.js";
type FunnelStep = {
value: string;
diff --git a/server/src/api/analytics/funnels/getFunnelStepSessions.ts b/server/src/api/analytics/funnels/getFunnelStepSessions.ts
index 14205de62..a418adb63 100644
--- a/server/src/api/analytics/funnels/getFunnelStepSessions.ts
+++ b/server/src/api/analytics/funnels/getFunnelStepSessions.ts
@@ -2,8 +2,9 @@ import { FilterParams } from "@rybbit/shared";
import { FastifyReply, FastifyRequest } from "fastify";
import SqlString from "sqlstring";
import { clickhouse } from "../../../db/clickhouse/clickhouse.js";
-import { getTimeStatement, patternToRegex, processResults, getFilterStatement } from "../utils.js";
+import { getTimeStatement, patternToRegex, processResults } from "../utils/utils.js";
import { GetSessionsResponse } from "../getSessions.js";
+import { getFilterStatement } from "../utils/getFilterStatement.js";
type FunnelStep = {
value: string;
diff --git a/server/src/api/analytics/getErrorBucketed.ts b/server/src/api/analytics/getErrorBucketed.ts
index e4946ed37..cda571bce 100644
--- a/server/src/api/analytics/getErrorBucketed.ts
+++ b/server/src/api/analytics/getErrorBucketed.ts
@@ -2,9 +2,10 @@ import { FilterParams } from "@rybbit/shared";
import { FastifyReply, FastifyRequest } from "fastify";
import SqlString from "sqlstring";
import { clickhouse } from "../../db/clickhouse/clickhouse.js";
-import { validateTimeStatementFillParams } from "./query-validation.js";
-import { getFilterStatement, getTimeStatement, TimeBucketToFn, bucketIntervalMap, processResults } from "./utils.js";
+import { validateTimeStatementFillParams } from "./utils/query-validation.js";
+import { getTimeStatement, TimeBucketToFn, bucketIntervalMap, processResults } from "./utils/utils.js";
import { TimeBucket } from "./types.js";
+import { getFilterStatement } from "./utils/getFilterStatement.js";
function getTimeStatementFill(params: FilterParams, bucket: TimeBucket) {
const { params: validatedParams, bucket: validatedBucket } = validateTimeStatementFillParams(params, bucket);
diff --git a/server/src/api/analytics/getErrorEvents.ts b/server/src/api/analytics/getErrorEvents.ts
index 21c7ddfae..f8015bb8c 100644
--- a/server/src/api/analytics/getErrorEvents.ts
+++ b/server/src/api/analytics/getErrorEvents.ts
@@ -1,7 +1,8 @@
import { FastifyReply, FastifyRequest } from "fastify";
import { clickhouse } from "../../db/clickhouse/clickhouse.js";
-import { getFilterStatement, getTimeStatement, processResults } from "./utils.js";
+import { getTimeStatement, processResults } from "./utils/utils.js";
import { FilterParams } from "@rybbit/shared";
+import { getFilterStatement } from "./utils/getFilterStatement.js";
interface GetErrorEventsRequest {
Params: {
diff --git a/server/src/api/analytics/getErrorNames.ts b/server/src/api/analytics/getErrorNames.ts
index 262c920f5..8447eb1f5 100644
--- a/server/src/api/analytics/getErrorNames.ts
+++ b/server/src/api/analytics/getErrorNames.ts
@@ -1,7 +1,8 @@
import { FastifyReply, FastifyRequest } from "fastify";
import { clickhouse } from "../../db/clickhouse/clickhouse.js";
-import { getFilterStatement, getTimeStatement, processResults } from "./utils.js";
+import { getTimeStatement, processResults } from "./utils/utils.js";
import { FilterParams } from "@rybbit/shared";
+import { getFilterStatement } from "./utils/getFilterStatement.js";
interface GetErrorNamesRequest {
Params: {
diff --git a/server/src/api/analytics/getJourneys.ts b/server/src/api/analytics/getJourneys.ts
index 72f31c897..f8d06e728 100644
--- a/server/src/api/analytics/getJourneys.ts
+++ b/server/src/api/analytics/getJourneys.ts
@@ -1,8 +1,8 @@
-import { FastifyRequest, FastifyReply } from "fastify";
-import { clickhouse } from "../../db/clickhouse/clickhouse.js";
-import { DateTime } from "luxon";
-import { getTimeStatement, getFilterStatement } from "./utils.js";
import { FilterParams } from "@rybbit/shared";
+import { FastifyReply, FastifyRequest } from "fastify";
+import { clickhouse } from "../../db/clickhouse/clickhouse.js";
+import { getFilterStatement } from "./utils/getFilterStatement.js";
+import { getTimeStatement } from "./utils/utils.js";
export const getJourneys = async (
request: FastifyRequest<{
diff --git a/server/src/api/analytics/getLiveUsercount.ts b/server/src/api/analytics/getLiveUsercount.ts
index 5384a9ca6..619c9b2cf 100644
--- a/server/src/api/analytics/getLiveUsercount.ts
+++ b/server/src/api/analytics/getLiveUsercount.ts
@@ -1,6 +1,6 @@
import { FastifyReply, FastifyRequest } from "fastify";
import { clickhouse } from "../../db/clickhouse/clickhouse.js";
-import { processResults } from "./utils.js";
+import { processResults } from "./utils/utils.js";
export const getLiveUsercount = async (
req: FastifyRequest<{
diff --git a/server/src/api/analytics/getMetric.ts b/server/src/api/analytics/getMetric.ts
index 316709789..1193b0f04 100644
--- a/server/src/api/analytics/getMetric.ts
+++ b/server/src/api/analytics/getMetric.ts
@@ -1,7 +1,8 @@
import { FastifyReply, FastifyRequest } from "fastify";
import { clickhouse } from "../../db/clickhouse/clickhouse.js";
import { FilterParameter } from "./types.js";
-import { getFilterStatement, getSqlParam, getTimeStatement, processResults } from "./utils.js";
+import { getTimeStatement, processResults } from "./utils/utils.js";
+import { getFilterStatement, getSqlParam } from "./utils/getFilterStatement.js";
import { FilterParams } from "@rybbit/shared";
interface GetMetricRequest {
diff --git a/server/src/api/analytics/getOrgEventCount.ts b/server/src/api/analytics/getOrgEventCount.ts
index bbfa9026e..3c7d37fb5 100644
--- a/server/src/api/analytics/getOrgEventCount.ts
+++ b/server/src/api/analytics/getOrgEventCount.ts
@@ -2,7 +2,7 @@ import { FastifyReply, FastifyRequest } from "fastify";
import SqlString from "sqlstring";
import { clickhouse } from "../../db/clickhouse/clickhouse.js";
import { getSitesUserHasAccessTo } from "../../lib/auth-utils.js";
-import { processResults } from "./utils.js";
+import { processResults } from "./utils/utils.js";
type OrgEventCountResponse = {
event_date: string;
diff --git a/server/src/api/analytics/getOverview.ts b/server/src/api/analytics/getOverview.ts
index 82503dd3b..47cf1d865 100644
--- a/server/src/api/analytics/getOverview.ts
+++ b/server/src/api/analytics/getOverview.ts
@@ -1,8 +1,8 @@
import { FilterParams } from "@rybbit/shared";
import { FastifyReply, FastifyRequest } from "fastify";
import { clickhouse } from "../../db/clickhouse/clickhouse.js";
-import { getUserHasAccessToSitePublic } from "../../lib/auth-utils.js";
-import { getFilterStatement, getTimeStatement, processResults } from "./utils.js";
+import { getFilterStatement } from "./utils/getFilterStatement.js";
+import { getTimeStatement, processResults } from "./utils/utils.js";
type GetOverviewResponse = {
sessions: number;
diff --git a/server/src/api/analytics/getOverviewBucketed.ts b/server/src/api/analytics/getOverviewBucketed.ts
index b4e0003aa..4006ef3a6 100644
--- a/server/src/api/analytics/getOverviewBucketed.ts
+++ b/server/src/api/analytics/getOverviewBucketed.ts
@@ -2,8 +2,9 @@ import { FilterParams } from "@rybbit/shared";
import { FastifyReply, FastifyRequest } from "fastify";
import SqlString from "sqlstring";
import { clickhouse } from "../../db/clickhouse/clickhouse.js";
-import { validateTimeStatementFillParams } from "./query-validation.js";
-import { getFilterStatement, getTimeStatement, processResults, TimeBucketToFn, bucketIntervalMap } from "./utils.js";
+import { validateTimeStatementFillParams } from "./utils/query-validation.js";
+import { getTimeStatement, processResults, TimeBucketToFn, bucketIntervalMap } from "./utils/utils.js";
+import { getFilterStatement } from "./utils/getFilterStatement.js";
import { TimeBucket } from "./types.js";
function getTimeStatementFill(params: FilterParams, bucket: TimeBucket) {
diff --git a/server/src/api/analytics/getPageTitles.ts b/server/src/api/analytics/getPageTitles.ts
index b30f17ee9..a3b22513d 100644
--- a/server/src/api/analytics/getPageTitles.ts
+++ b/server/src/api/analytics/getPageTitles.ts
@@ -1,7 +1,8 @@
import { FastifyReply, FastifyRequest } from "fastify";
import { clickhouse } from "../../db/clickhouse/clickhouse.js";
-import { getFilterStatement, getTimeStatement, processResults } from "./utils.js";
+import { getTimeStatement, processResults } from "./utils/utils.js";
import { FilterParams } from "@rybbit/shared";
+import { getFilterStatement } from "./utils/getFilterStatement.js";
interface GetPageTitlesRequest {
Params: {
diff --git a/server/src/api/analytics/getRetention.ts b/server/src/api/analytics/getRetention.ts
index c198e00e7..9a2c7d489 100644
--- a/server/src/api/analytics/getRetention.ts
+++ b/server/src/api/analytics/getRetention.ts
@@ -1,6 +1,6 @@
import { FastifyReply, FastifyRequest } from "fastify";
import { clickhouse } from "../../db/clickhouse/clickhouse.js";
-import { processResults } from "./utils.js";
+import { processResults } from "./utils/utils.js";
// Define the expected shape of a single data row from the query
interface RetentionDataRow {
diff --git a/server/src/api/analytics/getSession.ts b/server/src/api/analytics/getSession.ts
index 6d6197a0d..cc88b0716 100644
--- a/server/src/api/analytics/getSession.ts
+++ b/server/src/api/analytics/getSession.ts
@@ -1,6 +1,6 @@
import { FastifyReply, FastifyRequest } from "fastify";
import { clickhouse } from "../../db/clickhouse/clickhouse.js";
-import { processResults } from "./utils.js";
+import { processResults } from "./utils/utils.js";
export interface SessionDetails {
session_id: string;
diff --git a/server/src/api/analytics/getSessionLocations.ts b/server/src/api/analytics/getSessionLocations.ts
index 3a4f440ec..e41e970eb 100644
--- a/server/src/api/analytics/getSessionLocations.ts
+++ b/server/src/api/analytics/getSessionLocations.ts
@@ -1,7 +1,8 @@
import { FilterParams } from "@rybbit/shared";
import { FastifyReply, FastifyRequest } from "fastify";
import { clickhouse } from "../../db/clickhouse/clickhouse.js";
-import { getFilterStatement, getTimeStatement, processResults } from "./utils.js";
+import { getTimeStatement, processResults } from "./utils/utils.js";
+import { getFilterStatement } from "./utils/getFilterStatement.js";
export async function getSessionLocations(
req: FastifyRequest<{
diff --git a/server/src/api/analytics/getSessions.ts b/server/src/api/analytics/getSessions.ts
index c81881188..3b72e3ac9 100644
--- a/server/src/api/analytics/getSessions.ts
+++ b/server/src/api/analytics/getSessions.ts
@@ -1,7 +1,8 @@
import { FastifyReply, FastifyRequest } from "fastify";
import { clickhouse } from "../../db/clickhouse/clickhouse.js";
-import { enrichWithTraits, getFilterStatement, getTimeStatement, processResults } from "./utils.js";
+import { enrichWithTraits, getTimeStatement, processResults } from "./utils/utils.js";
import { FilterParams } from "@rybbit/shared";
+import { getFilterStatement } from "./utils/getFilterStatement.js";
export type GetSessionsResponse = {
session_id: string;
diff --git a/server/src/api/analytics/getUserInfo.ts b/server/src/api/analytics/getUserInfo.ts
index bd767efa6..4e84ef236 100644
--- a/server/src/api/analytics/getUserInfo.ts
+++ b/server/src/api/analytics/getUserInfo.ts
@@ -3,7 +3,7 @@ import { eq, and } from "drizzle-orm";
import { clickhouse } from "../../db/clickhouse/clickhouse.js";
import { db } from "../../db/postgres/postgres.js";
import { userProfiles, userAliases } from "../../db/postgres/schema.js";
-import { processResults } from "./utils.js";
+import { processResults } from "./utils/utils.js";
interface UserPageviewData {
sessions: number;
@@ -149,7 +149,7 @@ export async function getUserInfo(
}
const traits = profileResult[0]?.traits || null;
- const linked_devices = aliasesResult.map((alias) => ({
+ const linked_devices = aliasesResult.map(alias => ({
anonymous_id: alias.anonymous_id,
created_at: alias.created_at,
}));
diff --git a/server/src/api/analytics/getUserSessionCount.ts b/server/src/api/analytics/getUserSessionCount.ts
index 67055e885..b4f993754 100644
--- a/server/src/api/analytics/getUserSessionCount.ts
+++ b/server/src/api/analytics/getUserSessionCount.ts
@@ -1,6 +1,6 @@
import { FastifyReply, FastifyRequest } from "fastify";
import { clickhouse } from "../../db/clickhouse/clickhouse.js";
-import { processResults } from "./utils.js";
+import { processResults } from "./utils/utils.js";
import SqlString from "sqlstring";
export interface GetUserSessionCountRequest {
diff --git a/server/src/api/analytics/getUserSessions.ts b/server/src/api/analytics/getUserSessions.ts
index c227e8489..14fc17f34 100644
--- a/server/src/api/analytics/getUserSessions.ts
+++ b/server/src/api/analytics/getUserSessions.ts
@@ -1,7 +1,8 @@
import { FilterParams } from "@rybbit/shared";
import { FastifyReply, FastifyRequest } from "fastify";
import { clickhouse } from "../../db/clickhouse/clickhouse.js";
-import { getFilterStatement, getTimeStatement, processResults } from "./utils.js";
+import { getTimeStatement, processResults } from "./utils/utils.js";
+import { getFilterStatement } from "./utils/getFilterStatement.js";
// Individual pageview type
type Pageview = {
diff --git a/server/src/api/analytics/getUsers.ts b/server/src/api/analytics/getUsers.ts
index 7d782d820..cc4e30380 100644
--- a/server/src/api/analytics/getUsers.ts
+++ b/server/src/api/analytics/getUsers.ts
@@ -1,7 +1,8 @@
import { FastifyReply, FastifyRequest } from "fastify";
import { clickhouse } from "../../db/clickhouse/clickhouse.js";
-import { enrichWithTraits, getFilterStatement, getTimeStatement, processResults } from "./utils.js";
+import { enrichWithTraits, getTimeStatement, processResults } from "./utils/utils.js";
import { FilterParams } from "@rybbit/shared";
+import { getFilterStatement } from "./utils/getFilterStatement.js";
export type GetUsersResponse = {
user_id: string; // Device fingerprint
diff --git a/server/src/api/analytics/goals/getGoalSessions.ts b/server/src/api/analytics/goals/getGoalSessions.ts
index 59ea554fc..f61f4568d 100644
--- a/server/src/api/analytics/goals/getGoalSessions.ts
+++ b/server/src/api/analytics/goals/getGoalSessions.ts
@@ -3,7 +3,7 @@ import { clickhouse } from "../../../db/clickhouse/clickhouse.js";
import { db } from "../../../db/postgres/postgres.js";
import { goals } from "../../../db/postgres/schema.js";
import { eq } from "drizzle-orm";
-import { getTimeStatement, processResults, patternToRegex } from "../utils.js";
+import { getTimeStatement, processResults, patternToRegex } from "../utils/utils.js";
import { FilterParams } from "@rybbit/shared";
import { GetSessionsResponse } from "../getSessions.js";
import SqlString from "sqlstring";
diff --git a/server/src/api/analytics/goals/getGoals.ts b/server/src/api/analytics/goals/getGoals.ts
index d9986ff79..b97f12c6c 100644
--- a/server/src/api/analytics/goals/getGoals.ts
+++ b/server/src/api/analytics/goals/getGoals.ts
@@ -3,9 +3,10 @@ import { db } from "../../../db/postgres/postgres.js";
import { goals } from "../../../db/postgres/schema.js";
import { clickhouse } from "../../../db/clickhouse/clickhouse.js";
import { eq, desc, asc, sql } from "drizzle-orm";
-import { getFilterStatement, getTimeStatement, processResults, patternToRegex } from "../utils.js";
+import { getTimeStatement, processResults, patternToRegex } from "../utils/utils.js";
import SqlString from "sqlstring";
import { FilterParams } from "@rybbit/shared";
+import { getFilterStatement } from "../utils/getFilterStatement.js";
// Types for the response
interface GoalWithConversions {
diff --git a/server/src/api/analytics/performance/getPerformanceByDimension.ts b/server/src/api/analytics/performance/getPerformanceByDimension.ts
index 8496ce9f1..2d31d071d 100644
--- a/server/src/api/analytics/performance/getPerformanceByDimension.ts
+++ b/server/src/api/analytics/performance/getPerformanceByDimension.ts
@@ -1,7 +1,8 @@
import { FastifyReply, FastifyRequest } from "fastify";
import { clickhouse } from "../../../db/clickhouse/clickhouse.js";
-import { getFilterStatement, getTimeStatement, processResults } from "../utils.js";
+import { getTimeStatement, processResults } from "../utils/utils.js";
import { FilterParams } from "@rybbit/shared";
+import { getFilterStatement } from "../utils/getFilterStatement.js";
interface GetPerformanceByDimensionRequest {
Params: {
diff --git a/server/src/api/analytics/performance/getPerformanceOverview.ts b/server/src/api/analytics/performance/getPerformanceOverview.ts
index 0423f2b11..e7da2f4a0 100644
--- a/server/src/api/analytics/performance/getPerformanceOverview.ts
+++ b/server/src/api/analytics/performance/getPerformanceOverview.ts
@@ -1,8 +1,9 @@
import { FastifyReply, FastifyRequest } from "fastify";
import { clickhouse } from "../../../db/clickhouse/clickhouse.js";
-import { getFilterStatement, getTimeStatement, processResults } from "../utils.js";
+import { getTimeStatement, processResults } from "../utils/utils.js";
import { PerformanceOverviewMetrics } from "../types.js";
import { FilterParams } from "@rybbit/shared";
+import { getFilterStatement } from "../utils/getFilterStatement.js";
const getQuery = (params: FilterParams, siteId: number) => {
const timeStatement = getTimeStatement(params);
diff --git a/server/src/api/analytics/performance/getPerformanceTimeSeries.ts b/server/src/api/analytics/performance/getPerformanceTimeSeries.ts
index c7418aaec..c218184e8 100644
--- a/server/src/api/analytics/performance/getPerformanceTimeSeries.ts
+++ b/server/src/api/analytics/performance/getPerformanceTimeSeries.ts
@@ -1,10 +1,11 @@
import { FastifyReply, FastifyRequest } from "fastify";
import { clickhouse } from "../../../db/clickhouse/clickhouse.js";
-import { getFilterStatement, getTimeStatement, processResults, TimeBucketToFn, bucketIntervalMap } from "../utils.js";
+import { getTimeStatement, processResults, TimeBucketToFn, bucketIntervalMap } from "../utils/utils.js";
import SqlString from "sqlstring";
-import { validateTimeStatementFillParams } from "../query-validation.js";
+import { validateTimeStatementFillParams } from "../utils/query-validation.js";
import { TimeBucket, PerformanceTimeSeriesPoint } from "../types.js";
import { FilterParams } from "@rybbit/shared";
+import { getFilterStatement } from "../utils/getFilterStatement.js";
function getTimeStatementFill(params: FilterParams, bucket: TimeBucket) {
const { params: validatedParams, bucket: validatedBucket } = validateTimeStatementFillParams(params, bucket);
diff --git a/server/src/api/analytics/utils/getFilterStatement.test.ts b/server/src/api/analytics/utils/getFilterStatement.test.ts
new file mode 100644
index 000000000..ba22d39be
--- /dev/null
+++ b/server/src/api/analytics/utils/getFilterStatement.test.ts
@@ -0,0 +1,391 @@
+import { describe, it, expect } from "vitest";
+import { getFilterStatement, getSqlParam } from "./getFilterStatement.js";
+import { FilterParameter } from "../types.js";
+
+describe("getSqlParam", () => {
+ describe("UTM parameters", () => {
+ it("should handle utm_source", () => {
+ expect(getSqlParam("utm_source")).toBe("url_parameters['utm_source']");
+ });
+
+ it("should handle utm_medium", () => {
+ expect(getSqlParam("utm_medium")).toBe("url_parameters['utm_medium']");
+ });
+
+ it("should handle utm_campaign", () => {
+ expect(getSqlParam("utm_campaign")).toBe("url_parameters['utm_campaign']");
+ });
+
+ it("should handle utm_term", () => {
+ expect(getSqlParam("utm_term")).toBe("url_parameters['utm_term']");
+ });
+
+ it("should handle utm_content", () => {
+ expect(getSqlParam("utm_content")).toBe("url_parameters['utm_content']");
+ });
+ });
+
+ describe("URL parameters with url_param: prefix", () => {
+ it("should handle url_param:campaign_id", () => {
+ // url_param: prefix is handled at runtime for custom URL params
+ expect(getSqlParam("url_param:campaign_id" as FilterParameter)).toBe("url_parameters['campaign_id']");
+ });
+
+ it("should handle url_param:ref", () => {
+ expect(getSqlParam("url_param:ref" as FilterParameter)).toBe("url_parameters['ref']");
+ });
+ });
+
+ describe("Special parameters", () => {
+ it("should handle referrer", () => {
+ expect(getSqlParam("referrer")).toBe("domainWithoutWWW(referrer)");
+ });
+
+ it("should handle entry_page", () => {
+ expect(getSqlParam("entry_page")).toBe(
+ "(SELECT argMin(pathname, timestamp) FROM events WHERE session_id = events.session_id)"
+ );
+ });
+
+ it("should handle exit_page", () => {
+ expect(getSqlParam("exit_page")).toBe(
+ "(SELECT argMax(pathname, timestamp) FROM events WHERE session_id = events.session_id)"
+ );
+ });
+
+ it("should handle dimensions", () => {
+ expect(getSqlParam("dimensions")).toBe("concat(toString(screen_width), 'x', toString(screen_height))");
+ });
+
+ it("should handle city", () => {
+ expect(getSqlParam("city")).toBe("concat(toString(region), '-', toString(city))");
+ });
+
+ it("should handle browser_version", () => {
+ expect(getSqlParam("browser_version")).toBe("concat(toString(browser), ' ', toString(browser_version))");
+ });
+
+ it("should handle operating_system_version", () => {
+ const result = getSqlParam("operating_system_version");
+ expect(result).toContain("CASE");
+ expect(result).toContain("Windows 10/11");
+ });
+ });
+
+ describe("Standard parameters", () => {
+ it("should return browser as-is", () => {
+ expect(getSqlParam("browser")).toBe("browser");
+ });
+
+ it("should return country as-is", () => {
+ expect(getSqlParam("country")).toBe("country");
+ });
+
+ it("should return pathname as-is", () => {
+ expect(getSqlParam("pathname")).toBe("pathname");
+ });
+
+ it("should return device_type as-is", () => {
+ expect(getSqlParam("device_type")).toBe("device_type");
+ });
+ });
+});
+
+describe("getFilterStatement", () => {
+ describe("Empty and invalid inputs", () => {
+ it("should return empty string for empty filters", () => {
+ expect(getFilterStatement("")).toBe("");
+ });
+
+ it("should return empty string for empty array", () => {
+ expect(getFilterStatement("[]")).toBe("");
+ });
+
+ it("should throw for invalid JSON", () => {
+ expect(() => getFilterStatement("invalid json")).toThrow("Invalid JSON format");
+ });
+ });
+
+ describe("Basic equals filters", () => {
+ it("should handle single value equals filter", () => {
+ const filters = JSON.stringify([{ parameter: "browser", type: "equals", value: ["Chrome"] }]);
+ const result = getFilterStatement(filters);
+ expect(result).toBe("AND browser = 'Chrome'");
+ });
+
+ it("should handle multiple values equals filter with OR", () => {
+ const filters = JSON.stringify([{ parameter: "browser", type: "equals", value: ["Chrome", "Firefox"] }]);
+ const result = getFilterStatement(filters);
+ expect(result).toBe("AND (browser = 'Chrome' OR browser = 'Firefox')");
+ });
+
+ it("should handle multiple filters with AND", () => {
+ const filters = JSON.stringify([
+ { parameter: "browser", type: "equals", value: ["Chrome"] },
+ { parameter: "country", type: "equals", value: ["US"] },
+ ]);
+ const result = getFilterStatement(filters);
+ expect(result).toBe("AND browser = 'Chrome' AND country = 'US'");
+ });
+ });
+
+ describe("Not equals filters", () => {
+ it("should handle not_equals filter", () => {
+ const filters = JSON.stringify([{ parameter: "browser", type: "not_equals", value: ["Chrome"] }]);
+ const result = getFilterStatement(filters);
+ expect(result).toBe("AND browser != 'Chrome'");
+ });
+
+ it("should handle multiple not_equals values", () => {
+ const filters = JSON.stringify([{ parameter: "browser", type: "not_equals", value: ["Chrome", "Firefox"] }]);
+ const result = getFilterStatement(filters);
+ expect(result).toBe("AND (browser != 'Chrome' OR browser != 'Firefox')");
+ });
+ });
+
+ describe("Contains filters", () => {
+ it("should handle contains filter with LIKE and wildcards", () => {
+ const filters = JSON.stringify([{ parameter: "pathname", type: "contains", value: ["/blog"] }]);
+ const result = getFilterStatement(filters);
+ expect(result).toBe("AND pathname LIKE '%/blog%'");
+ });
+
+ it("should handle not_contains filter", () => {
+ const filters = JSON.stringify([{ parameter: "pathname", type: "not_contains", value: ["/admin"] }]);
+ const result = getFilterStatement(filters);
+ expect(result).toBe("AND pathname NOT LIKE '%/admin%'");
+ });
+ });
+
+ describe("Regex filters", () => {
+ it("should handle regex filter", () => {
+ const filters = JSON.stringify([{ parameter: "pathname", type: "regex", value: ["^/blog/.*"] }]);
+ const result = getFilterStatement(filters);
+ expect(result).toBe("AND match(pathname, '^/blog/.*')");
+ });
+
+ it("should handle not_regex filter", () => {
+ const filters = JSON.stringify([{ parameter: "pathname", type: "not_regex", value: ["^/admin/.*"] }]);
+ const result = getFilterStatement(filters);
+ expect(result).toBe("AND NOT match(pathname, '^/admin/.*')");
+ });
+
+ it("should throw for empty regex pattern", () => {
+ const filters = JSON.stringify([{ parameter: "pathname", type: "regex", value: [""] }]);
+ expect(() => getFilterStatement(filters)).toThrow("Regex pattern cannot be empty");
+ });
+
+ it("should throw for invalid regex pattern", () => {
+ const filters = JSON.stringify([{ parameter: "pathname", type: "regex", value: ["[invalid"] }]);
+ expect(() => getFilterStatement(filters)).toThrow("Invalid regex pattern");
+ });
+
+ it("should throw for regex pattern exceeding max length", () => {
+ const longPattern = "a".repeat(501);
+ const filters = JSON.stringify([{ parameter: "pathname", type: "regex", value: [longPattern] }]);
+ expect(() => getFilterStatement(filters)).toThrow("Regex pattern too long");
+ });
+ });
+
+ describe("Numeric comparison filters", () => {
+ it("should handle greater_than filter", () => {
+ const filters = JSON.stringify([{ parameter: "lat", type: "greater_than", value: ["40.0"] }]);
+ const result = getFilterStatement(filters);
+ expect(result).toBe("AND lat > 40");
+ });
+
+ it("should handle less_than filter", () => {
+ const filters = JSON.stringify([{ parameter: "lon", type: "less_than", value: ["-70.0"] }]);
+ const result = getFilterStatement(filters);
+ expect(result).toBe("AND lon < -70");
+ });
+
+ it("should throw for invalid numeric value", () => {
+ const filters = JSON.stringify([{ parameter: "lat", type: "greater_than", value: ["not-a-number"] }]);
+ expect(() => getFilterStatement(filters)).toThrow("Invalid numeric value");
+ });
+ });
+
+ describe("Lat/Lon tolerance handling", () => {
+ it("should handle lat equals with tolerance", () => {
+ const filters = JSON.stringify([{ parameter: "lat", type: "equals", value: ["40.7128"] }]);
+ const result = getFilterStatement(filters);
+ expect(result).toContain("lat >= 40.7118");
+ expect(result).toContain("lat <= 40.7138");
+ });
+
+ it("should handle lon equals with multiple values", () => {
+ const filters = JSON.stringify([{ parameter: "lon", type: "equals", value: ["-74.006", "-73.5"] }]);
+ const result = getFilterStatement(filters);
+ expect(result).toContain("lon >= -74.007");
+ expect(result).toContain("lon <= -74.005");
+ expect(result).toContain("lon >= -73.501");
+ expect(result).toContain("lon <= -73.499");
+ expect(result).toContain(" OR ");
+ });
+ });
+
+ describe("User ID special handling", () => {
+ it("should check both user_id and identified_user_id for equals", () => {
+ const filters = JSON.stringify([{ parameter: "user_id", type: "equals", value: ["user123"] }]);
+ const result = getFilterStatement(filters);
+ expect(result).toBe("AND (user_id = 'user123' OR identified_user_id = 'user123')");
+ });
+
+ it("should check both user_id and identified_user_id for not_equals", () => {
+ const filters = JSON.stringify([{ parameter: "user_id", type: "not_equals", value: ["user123"] }]);
+ const result = getFilterStatement(filters);
+ expect(result).toBe("AND (user_id != 'user123' AND identified_user_id != 'user123')");
+ });
+
+ it("should handle multiple user IDs with equals using OR", () => {
+ const filters = JSON.stringify([{ parameter: "user_id", type: "equals", value: ["user1", "user2"] }]);
+ const result = getFilterStatement(filters);
+ expect(result).toContain("user_id = 'user1' OR identified_user_id = 'user1'");
+ expect(result).toContain("user_id = 'user2' OR identified_user_id = 'user2'");
+ expect(result).toContain(" OR ");
+ });
+
+ it("should handle multiple user IDs with not_equals using AND", () => {
+ const filters = JSON.stringify([{ parameter: "user_id", type: "not_equals", value: ["user1", "user2"] }]);
+ const result = getFilterStatement(filters);
+ expect(result).toContain("user_id != 'user1' AND identified_user_id != 'user1'");
+ expect(result).toContain("user_id != 'user2' AND identified_user_id != 'user2'");
+ });
+ });
+
+ describe("Event name session-level filter", () => {
+ it("should create subquery for event_name filter", () => {
+ const filters = JSON.stringify([{ parameter: "event_name", type: "equals", value: ["click"] }]);
+ const result = getFilterStatement(filters);
+ expect(result).toContain("session_id IN");
+ expect(result).toContain("SELECT DISTINCT session_id");
+ expect(result).toContain("FROM events");
+ expect(result).toContain("event_name = 'click'");
+ });
+
+ it("should include siteId in event_name subquery when provided", () => {
+ const filters = JSON.stringify([{ parameter: "event_name", type: "equals", value: ["click"] }]);
+ const result = getFilterStatement(filters, 123);
+ expect(result).toContain("site_id = 123");
+ });
+
+ it("should include timeStatement in event_name subquery when provided", () => {
+ const filters = JSON.stringify([{ parameter: "event_name", type: "equals", value: ["click"] }]);
+ const result = getFilterStatement(filters, 123, "AND timestamp > now() - INTERVAL 1 DAY");
+ expect(result).toContain("timestamp > now() - INTERVAL 1 DAY");
+ });
+ });
+
+ describe("Entry page filter", () => {
+ it("should create subquery for entry_page filter", () => {
+ const filters = JSON.stringify([{ parameter: "entry_page", type: "equals", value: ["/home"] }]);
+ const result = getFilterStatement(filters);
+ expect(result).toContain("session_id IN");
+ expect(result).toContain("argMin(pathname, timestamp) AS entry_pathname");
+ expect(result).toContain("entry_pathname = '/home'");
+ });
+
+ it("should handle multiple entry_page values", () => {
+ const filters = JSON.stringify([{ parameter: "entry_page", type: "equals", value: ["/home", "/landing"] }]);
+ const result = getFilterStatement(filters);
+ expect(result).toContain("entry_pathname = '/home'");
+ expect(result).toContain("entry_pathname = '/landing'");
+ expect(result).toContain(" OR ");
+ });
+
+ it("should handle entry_page contains filter", () => {
+ const filters = JSON.stringify([{ parameter: "entry_page", type: "contains", value: ["/blog"] }]);
+ const result = getFilterStatement(filters);
+ expect(result).toContain("entry_pathname LIKE '%/blog%'");
+ });
+ });
+
+ describe("Exit page filter", () => {
+ it("should create subquery for exit_page filter", () => {
+ const filters = JSON.stringify([{ parameter: "exit_page", type: "equals", value: ["/checkout"] }]);
+ const result = getFilterStatement(filters);
+ expect(result).toContain("session_id IN");
+ expect(result).toContain("argMax(pathname, timestamp) AS exit_pathname");
+ expect(result).toContain("exit_pathname = '/checkout'");
+ });
+
+ it("should handle multiple exit_page values", () => {
+ const filters = JSON.stringify([{ parameter: "exit_page", type: "equals", value: ["/checkout", "/thank-you"] }]);
+ const result = getFilterStatement(filters);
+ expect(result).toContain("exit_pathname = '/checkout'");
+ expect(result).toContain("exit_pathname = '/thank-you'");
+ expect(result).toContain(" OR ");
+ });
+ });
+
+ describe("UTM filter handling", () => {
+ it("should use url_parameters map for utm_source", () => {
+ const filters = JSON.stringify([{ parameter: "utm_source", type: "equals", value: ["google"] }]);
+ const result = getFilterStatement(filters);
+ expect(result).toBe("AND url_parameters['utm_source'] = 'google'");
+ });
+
+ it("should use url_parameters map for utm_medium", () => {
+ const filters = JSON.stringify([{ parameter: "utm_medium", type: "equals", value: ["cpc"] }]);
+ const result = getFilterStatement(filters);
+ expect(result).toBe("AND url_parameters['utm_medium'] = 'cpc'");
+ });
+ });
+
+ describe("SQL injection prevention", () => {
+ it("should escape single quotes in values", () => {
+ const filters = JSON.stringify([
+ { parameter: "browser", type: "equals", value: ["Chrome'; DROP TABLE users;--"] },
+ ]);
+ const result = getFilterStatement(filters);
+ // SqlString.escape escapes the single quote with a backslash, making the SQL safe
+ expect(result).toContain("\\'");
+ expect(result).toBe("AND browser = 'Chrome\\'; DROP TABLE users;--'");
+ });
+
+ it("should escape backslashes in values", () => {
+ const filters = JSON.stringify([{ parameter: "pathname", type: "equals", value: ["/path\\with\\backslashes"] }]);
+ const result = getFilterStatement(filters);
+ expect(result).toContain("\\\\");
+ });
+ });
+
+ describe("Special parameter transformations", () => {
+ it("should use domainWithoutWWW for referrer", () => {
+ const filters = JSON.stringify([{ parameter: "referrer", type: "equals", value: ["google.com"] }]);
+ const result = getFilterStatement(filters);
+ expect(result).toBe("AND domainWithoutWWW(referrer) = 'google.com'");
+ });
+
+ it("should use dimensions concat for dimensions filter", () => {
+ const filters = JSON.stringify([{ parameter: "dimensions", type: "equals", value: ["1920x1080"] }]);
+ const result = getFilterStatement(filters);
+ expect(result).toContain("concat(toString(screen_width), 'x', toString(screen_height))");
+ expect(result).toContain("= '1920x1080'");
+ });
+
+ it("should use city concat for city filter", () => {
+ const filters = JSON.stringify([{ parameter: "city", type: "equals", value: ["CA-San Francisco"] }]);
+ const result = getFilterStatement(filters);
+ expect(result).toContain("concat(toString(region), '-', toString(city))");
+ expect(result).toContain("= 'CA-San Francisco'");
+ });
+ });
+
+ describe("Complex multi-filter scenarios", () => {
+ it("should handle multiple different filter types together", () => {
+ const filters = JSON.stringify([
+ { parameter: "browser", type: "equals", value: ["Chrome"] },
+ { parameter: "pathname", type: "contains", value: ["/blog"] },
+ { parameter: "country", type: "not_equals", value: ["CN"] },
+ ]);
+ const result = getFilterStatement(filters);
+ expect(result).toContain("browser = 'Chrome'");
+ expect(result).toContain("pathname LIKE '%/blog%'");
+ expect(result).toContain("country != 'CN'");
+ // 1 at start + 2 between 3 filters = 3 ANDs total
+ expect(result.match(/AND/g)?.length).toBe(3);
+ });
+ });
+});
diff --git a/server/src/api/analytics/utils.ts b/server/src/api/analytics/utils/getFilterStatement.ts
similarity index 57%
rename from server/src/api/analytics/utils.ts
rename to server/src/api/analytics/utils/getFilterStatement.ts
index f9214f45a..dcb18dc0c 100644
--- a/server/src/api/analytics/utils.ts
+++ b/server/src/api/analytics/utils/getFilterStatement.ts
@@ -1,95 +1,6 @@
-import { ResultSet } from "@clickhouse/client";
-import { FilterParams } from "@rybbit/shared";
-import { and, eq, inArray } from "drizzle-orm";
import SqlString from "sqlstring";
-import { db } from "../../db/postgres/postgres.js";
-import { userProfiles } from "../../db/postgres/schema.js";
-import { filterParamSchema, validateFilters, validateTimeStatementParams } from "./query-validation.js";
-import { FilterParameter, FilterType } from "./types.js";
-
-export function getTimeStatement(
- params: Pick<FilterParams, "start_date" | "end_date" | "time_zone" | "past_minutes_start" | "past_minutes_end">
-) {
- const { start_date, end_date, time_zone, past_minutes_start, past_minutes_end } = params;
-
- // Construct the legacy format for validation
- const pastMinutesRange =
- past_minutes_start !== undefined && past_minutes_end !== undefined
- ? { start: Number(past_minutes_start), end: Number(past_minutes_end) }
- : undefined;
-
- const date = start_date && end_date && time_zone ? { start_date, end_date, time_zone } : undefined;
-
- // Sanitize inputs with Zod
- const sanitized = validateTimeStatementParams({
- date,
- pastMinutesRange,
- });
-
- if (sanitized.date) {
- const { start_date, end_date, time_zone } = sanitized.date;
- if (!start_date && !end_date) {
- return "";
- }
-
- // Use SqlString.escape for date and timeZone values
- return `AND timestamp >= toTimeZone(
- toStartOfDay(toDateTime(${SqlString.escape(start_date)}, ${SqlString.escape(time_zone)})),
- 'UTC'
- )
- AND timestamp < if(
- toDate(${SqlString.escape(end_date)}) = toDate(now(), ${SqlString.escape(time_zone)}),
- now(),
- toTimeZone(
- toStartOfDay(toDateTime(${SqlString.escape(end_date)}, ${SqlString.escape(time_zone)})) + INTERVAL 1 DAY,
- 'UTC'
- )
- )`;
- }
-
- // Handle specific range of past minutes - convert to exact timestamps for better performance
- if (sanitized.pastMinutesRange) {
- const { start, end } = sanitized.pastMinutesRange;
-
- // Calculate exact timestamps in JavaScript to avoid runtime ClickHouse calculations
- const now = new Date();
- const startTimestamp = new Date(now.getTime() - start * 60 * 1000);
- const endTimestamp = new Date(now.getTime() - end * 60 * 1000);
-
- // Format as YYYY-MM-DD HH:MM:SS without milliseconds for ClickHouse
- const startIso = startTimestamp.toISOString().slice(0, 19).replace("T", " ");
- const endIso = endTimestamp.toISOString().slice(0, 19).replace("T", " ");
-
- return `AND timestamp > toDateTime(${SqlString.escape(startIso)}) AND timestamp <= toDateTime(${SqlString.escape(endIso)})`;
- }
-
- // If no valid time parameters were provided, return empty string
- return "";
-}
-
-export async function processResults<T>(results: ResultSet<"JSONEachRow">): Promise<T[]> {
- const data: T[] = await results.json();
- for (const row of data) {
- for (const key in row) {
- // Only convert to number if the value is not null/undefined and is a valid number
- if (
- key !== "session_id" &&
- key !== "user_id" &&
- key !== "identified_user_id" &&
- key !== "effective_user_id" &&
- row[key] !== null &&
- row[key] !== undefined &&
- row[key] !== "" &&
- row[key] !== true &&
- row[key] !== false &&
- !isNaN(Number(row[key]))
- ) {
- row[key] = Number(row[key]) as any;
- }
- }
- }
- return data;
-}
+import { filterParamSchema, validateFilters } from "./query-validation.js";
+import { FilterParameter, FilterType } from "../types.js";
const filterTypeToOperator = (type: FilterType) => {
switch (type) {
@@ -101,6 +12,13 @@ const filterTypeToOperator = (type: FilterType) => {
return "LIKE";
case "not_contains":
return "NOT LIKE";
+ case "greater_than":
+ return ">";
+ case "less_than":
+ return "<";
+ case "regex":
+ case "not_regex":
+ return null; // Handled separately with match() function
}
};
@@ -136,10 +54,10 @@ export const getSqlParam = (parameter: FilterParameter) => {
return "concat(toString(browser), ' ', toString(browser_version))";
}
if (parameter === "operating_system_version") {
- return `CASE
- WHEN concat(toString(operating_system), ' ', toString(operating_system_version)) = 'Windows 10'
- THEN 'Windows 10/11'
- ELSE concat(toString(operating_system), ' ', toString(operating_system_version))
+ return `CASE
+ WHEN concat(toString(operating_system), ' ', toString(operating_system_version)) = 'Windows 10'
+ THEN 'Windows 10/11'
+ ELSE concat(toString(operating_system), ' ', toString(operating_system_version))
END`;
}
return filterParamSchema.parse(parameter);
@@ -291,7 +209,42 @@ export function getFilterStatement(filters: string, siteId?: number, timeStateme
}
}
- // Special handling for lat/lon with tolerance
+ // Handle regex filters using ClickHouse match() function
+ if (filter.type === "regex" || filter.type === "not_regex") {
+ const pattern = String(filter.value[0] ?? "");
+
+ // Validate regex pattern
+ if (!pattern) {
+ throw new Error("Regex pattern cannot be empty");
+ }
+
+ // Check if it's a valid regex by trying to compile it
+ try {
+ new RegExp(pattern);
+ } catch (e) {
+ throw new Error(`Invalid regex pattern: ${e instanceof Error ? e.message : "Unknown error"}`);
+ }
+
+ // Additional safety: limit pattern length to prevent abuse
+ if (pattern.length > 500) {
+ throw new Error("Regex pattern too long (max 500 characters)");
+ }
+
+ const regexPattern = SqlString.escape(pattern);
+ const matchExpr = `match(${getSqlParam(filter.parameter)}, ${regexPattern})`;
+ return filter.type === "regex" ? matchExpr : `NOT ${matchExpr}`;
+ }
+
+ // Handle numeric comparison filters (>, <)
+ if (filter.type === "greater_than" || filter.type === "less_than") {
+ const numericValue = Number(filter.value[0]);
+ if (isNaN(numericValue)) {
+ throw new Error(`Invalid numeric value for ${filter.type} filter: ${filter.value[0]}`);
+ }
+ return `${getSqlParam(filter.parameter)} ${filterTypeToOperator(filter.type)} ${numericValue}`;
+ }
+
+ // Special handling for lat/lon with tolerance (only for equals/not_equals)
if (filter.parameter === "lat" || filter.parameter === "lon") {
const tolerance = 0.001;
if (filter.value.length === 1) {
@@ -322,88 +275,3 @@ export function getFilterStatement(filters: string, siteId?: number, timeStateme
.join(" AND ")
);
}
-
-/**
- * Converts wildcard path patterns to ClickHouse regex pattern
- * - Supports * for matching a single path segment (not including /)
- * - Supports ** for matching multiple path segments (including /)
- * @param pattern Path pattern with wildcards
- * @returns ClickHouse-compatible regex string
- */
-export function patternToRegex(pattern: string): string {
- // Escape special regex characters except * which we'll handle specially
- const escapedPattern = pattern.replace(/[.+?^${}()|[\]\\]/g, "\\$&");
-
- // Replace ** with a temporary marker
- const withDoubleStar = escapedPattern.replace(/\*\*/g, "{{DOUBLE_STAR}}");
-
- // Replace * with [^/]+ (any characters except /)
- const withSingleStar = withDoubleStar.replace(/\*/g, "[^/]+");
-
- // Replace the double star marker with .* (any characters including /)
- const finalRegex = withSingleStar.replace(/{{DOUBLE_STAR}}/g, ".*");
-
- // Anchor the regex to start/end of string for exact matches
- return `^${finalRegex}$`;
-}
-
-// Time bucket mapping constants
-export const TimeBucketToFn = {
- minute: "toStartOfMinute",
- five_minutes: "toStartOfFiveMinutes",
- ten_minutes: "toStartOfTenMinutes",
- fifteen_minutes: "toStartOfFifteenMinutes",
- hour: "toStartOfHour",
- day: "toStartOfDay",
- week: "toStartOfWeek",
- month: "toStartOfMonth",
- year: "toStartOfYear",
-} as const;
-
-export const bucketIntervalMap = {
- minute: "1 MINUTE",
- five_minutes: "5 MINUTES",
- ten_minutes: "10 MINUTES",
- fifteen_minutes: "15 MINUTES",
- hour: "1 HOUR",
- day: "1 DAY",
- week: "7 DAY",
- month: "1 MONTH",
- year: "1 YEAR",
-} as const;
-
-/**
- * Enriches data with user traits from Postgres for identified users.
- * This is a shared utility to avoid duplicating the traits fetching logic.
- * Uses identified_user_id to look up traits since that's the custom user ID.
- */
-export async function enrichWithTraits<T extends { identified_user_id: string }>(
- data: T[],
- siteId: number
-): Promise<Array<T & { traits: Record<string, unknown> | null }>> {
- const identifiedUserIds = [
- ...new Set(data.filter((item) => item.identified_user_id).map((item) => item.identified_user_id)),
- ];
-
- let traitsMap: Map<string, Record<string, unknown>> = new Map();
- if (identifiedUserIds.length > 0) {
- const profiles = await db
- .select({
- userId: userProfiles.userId,
- traits: userProfiles.traits,
- })
- .from(userProfiles)
- .where(and(eq(userProfiles.siteId, siteId), inArray(userProfiles.userId, identifiedUserIds)));
-
- traitsMap = new Map(
- profiles.map((p) => [
- p.userId,
- p.traits && typeof p.traits === "object" && !Array.isArray(p.traits)
- ? (p.traits as Record<string, unknown>)
- : {},
- ])
- );
- }
-
- return data.map((item) => ({ ...item, traits: traitsMap.get(item.identified_user_id) || null }));
-}
diff --git a/server/src/api/analytics/query-validation.ts b/server/src/api/analytics/utils/query-validation.ts
similarity index 97%
rename from server/src/api/analytics/query-validation.ts
rename to server/src/api/analytics/utils/query-validation.ts
index 59797b1c5..12ae929e0 100644
--- a/server/src/api/analytics/query-validation.ts
+++ b/server/src/api/analytics/utils/query-validation.ts
@@ -179,7 +179,16 @@ const timeBucketSchema = z.enum([
/**
* Schema for filter type values
*/
-const filterTypeSchema = z.enum(["equals", "not_equals", "contains", "not_contains"]);
+const filterTypeSchema = z.enum([
+ "equals",
+ "not_equals",
+ "contains",
+ "not_contains",
+ "regex",
+ "not_regex",
+ "greater_than",
+ "less_than",
+]);
/**
* Schema for filter parameter values
diff --git a/server/src/api/analytics/utils/utils.ts b/server/src/api/analytics/utils/utils.ts
new file mode 100644
index 000000000..10076a951
--- /dev/null
+++ b/server/src/api/analytics/utils/utils.ts
@@ -0,0 +1,176 @@
+import { ResultSet } from "@clickhouse/client";
+import { FilterParams } from "@rybbit/shared";
+import { and, eq, inArray } from "drizzle-orm";
+import SqlString from "sqlstring";
+import { db } from "../../../db/postgres/postgres.js";
+import { userProfiles } from "../../../db/postgres/schema.js";
+import { validateTimeStatementParams } from "./query-validation.js";
+
+export function getTimeStatement(
+ params: Pick<FilterParams, "start_date" | "end_date" | "time_zone" | "past_minutes_start" | "past_minutes_end">
+) {
+ const { start_date, end_date, time_zone, past_minutes_start, past_minutes_end } = params;
+
+ // Construct the legacy format for validation
+ const pastMinutesRange =
+ past_minutes_start !== undefined && past_minutes_end !== undefined
+ ? { start: Number(past_minutes_start), end: Number(past_minutes_end) }
+ : undefined;
+
+ const date = start_date && end_date && time_zone ? { start_date, end_date, time_zone } : undefined;
+
+ // Sanitize inputs with Zod
+ const sanitized = validateTimeStatementParams({
+ date,
+ pastMinutesRange,
+ });
+
+ if (sanitized.date) {
+ const { start_date, end_date, time_zone } = sanitized.date;
+ if (!start_date && !end_date) {
+ return "";
+ }
+
+ // Use SqlString.escape for date and timeZone values
+ return `AND timestamp >= toTimeZone(
+ toStartOfDay(toDateTime(${SqlString.escape(start_date)}, ${SqlString.escape(time_zone)})),
+ 'UTC'
+ )
+ AND timestamp < if(
+ toDate(${SqlString.escape(end_date)}) = toDate(now(), ${SqlString.escape(time_zone)}),
+ now(),
+ toTimeZone(
+ toStartOfDay(toDateTime(${SqlString.escape(end_date)}, ${SqlString.escape(time_zone)})) + INTERVAL 1 DAY,
+ 'UTC'
+ )
+ )`;
+ }
+
+ // Handle specific range of past minutes - convert to exact timestamps for better performance
+ if (sanitized.pastMinutesRange) {
+ const { start, end } = sanitized.pastMinutesRange;
+
+ // Calculate exact timestamps in JavaScript to avoid runtime ClickHouse calculations
+ const now = new Date();
+ const startTimestamp = new Date(now.getTime() - start * 60 * 1000);
+ const endTimestamp = new Date(now.getTime() - end * 60 * 1000);
+
+ // Format as YYYY-MM-DD HH:MM:SS without milliseconds for ClickHouse
+ const startIso = startTimestamp.toISOString().slice(0, 19).replace("T", " ");
+ const endIso = endTimestamp.toISOString().slice(0, 19).replace("T", " ");
+
+ return `AND timestamp > toDateTime(${SqlString.escape(startIso)}) AND timestamp <= toDateTime(${SqlString.escape(endIso)})`;
+ }
+
+ // If no valid time parameters were provided, return empty string
+ return "";
+}
+
+export async function processResults<T>(results: ResultSet<"JSONEachRow">): Promise<T[]> {
+ const data: T[] = await results.json();
+ for (const row of data) {
+ for (const key in row) {
+ // Only convert to number if the value is not null/undefined and is a valid number
+ if (
+ key !== "session_id" &&
+ key !== "user_id" &&
+ key !== "identified_user_id" &&
+ key !== "effective_user_id" &&
+ row[key] !== null &&
+ row[key] !== undefined &&
+ row[key] !== "" &&
+ row[key] !== true &&
+ row[key] !== false &&
+ !isNaN(Number(row[key]))
+ ) {
+ row[key] = Number(row[key]) as any;
+ }
+ }
+ }
+ return data;
+}
+
+/**
+ * Converts wildcard path patterns to ClickHouse regex pattern
+ * - Supports * for matching a single path segment (not including /)
+ * - Supports ** for matching multiple path segments (including /)
+ * @param pattern Path pattern with wildcards
+ * @returns ClickHouse-compatible regex string
+ */
+export function patternToRegex(pattern: string): string {
+ // Escape special regex characters except * which we'll handle specially
+ const escapedPattern = pattern.replace(/[.+?^${}()|[\]\\]/g, "\\$&");
+
+ // Replace ** with a temporary marker
+ const withDoubleStar = escapedPattern.replace(/\*\*/g, "{{DOUBLE_STAR}}");
+
+ // Replace * with [^/]+ (any characters except /)
+ const withSingleStar = withDoubleStar.replace(/\*/g, "[^/]+");
+
+ // Replace the double star marker with .* (any characters including /)
+ const finalRegex = withSingleStar.replace(/{{DOUBLE_STAR}}/g, ".*");
+
+ // Anchor the regex to start/end of string for exact matches
+ return `^${finalRegex}$`;
+}
+
+// Time bucket mapping constants
+export const TimeBucketToFn = {
+ minute: "toStartOfMinute",
+ five_minutes: "toStartOfFiveMinutes",
+ ten_minutes: "toStartOfTenMinutes",
+ fifteen_minutes: "toStartOfFifteenMinutes",
+ hour: "toStartOfHour",
+ day: "toStartOfDay",
+ week: "toStartOfWeek",
+ month: "toStartOfMonth",
+ year: "toStartOfYear",
+} as const;
+
+export const bucketIntervalMap = {
+ minute: "1 MINUTE",
+ five_minutes: "5 MINUTES",
+ ten_minutes: "10 MINUTES",
+ fifteen_minutes: "15 MINUTES",
+ hour: "1 HOUR",
+ day: "1 DAY",
+ week: "7 DAY",
+ month: "1 MONTH",
+ year: "1 YEAR",
+} as const;
+
+/**
+ * Enriches data with user traits from Postgres for identified users.
+ * This is a shared utility to avoid duplicating the traits fetching logic.
+ * Uses identified_user_id to look up traits since that's the custom user ID.
+ */
+export async function enrichWithTraits<T extends { identified_user_id: string }>(
+ data: T[],
+ siteId: number
+): Promise<Array<T & { traits: Record<string, unknown> | null }>> {
+ const identifiedUserIds = [
+ ...new Set(data.filter(item => item.identified_user_id).map(item => item.identified_user_id)),
+ ];
+
+ let traitsMap: Map<string, Record<string, unknown>> = new Map();
+ if (identifiedUserIds.length > 0) {
+ const profiles = await db
+ .select({
+ userId: userProfiles.userId,
+ traits: userProfiles.traits,
+ })
+ .from(userProfiles)
+ .where(and(eq(userProfiles.siteId, siteId), inArray(userProfiles.userId, identifiedUserIds)));
+
+ traitsMap = new Map(
+ profiles.map(p => [
+ p.userId,
+ p.traits && typeof p.traits === "object" && !Array.isArray(p.traits)
+ ? (p.traits as Record<string, unknown>)
+ : {},
+ ])
+ );
+ }
+
+ return data.map(item => ({ ...item, traits: traitsMap.get(item.identified_user_id) || null }));
+}
diff --git a/server/src/api/sessionReplay/getSessionReplayEvents.ts b/server/src/api/sessionReplay/getSessionReplayEvents.ts
index 43b2fa941..bc4e989aa 100644
--- a/server/src/api/sessionReplay/getSessionReplayEvents.ts
+++ b/server/src/api/sessionReplay/getSessionReplayEvents.ts
@@ -1,6 +1,6 @@
import { FastifyReply, FastifyRequest } from "fastify";
import { SessionReplayQueryService } from "../../services/replay/sessionReplayQueryService.js";
-import { enrichWithTraits } from "../analytics/utils.js";
+import { enrichWithTraits } from "../analytics/utils/utils.js";
export async function getSessionReplayEvents(
request: FastifyRequest<{
diff --git a/server/src/api/sessionReplay/getSessionReplays.ts b/server/src/api/sessionReplay/getSessionReplays.ts
index 0aff9446d..76be96ec2 100644
--- a/server/src/api/sessionReplay/getSessionReplays.ts
+++ b/server/src/api/sessionReplay/getSessionReplays.ts
@@ -1,7 +1,7 @@
import { FastifyReply, FastifyRequest } from "fastify";
import { FilterParams } from "@rybbit/shared";
import { SessionReplayQueryService } from "../../services/replay/sessionReplayQueryService.js";
-import { enrichWithTraits } from "../analytics/utils.js";
+import { enrichWithTraits } from "../analytics/utils/utils.js";
export async function getSessionReplays(
request: FastifyRequest<{
diff --git a/server/src/api/sites/getSitesFromOrg.ts b/server/src/api/sites/getSitesFromOrg.ts
index 780aa5df0..3f61caa45 100644
--- a/server/src/api/sites/getSitesFromOrg.ts
+++ b/server/src/api/sites/getSitesFromOrg.ts
@@ -5,7 +5,7 @@ import { db } from "../../db/postgres/postgres.js";
import { sites, member, organization } from "../../db/postgres/schema.js";
import { getSessionFromReq, getIsUserAdmin } from "../../lib/auth-utils.js";
import { IS_CLOUD, DEFAULT_EVENT_LIMIT } from "../../lib/const.js";
-import { processResults } from "../analytics/utils.js";
+import { processResults } from "../analytics/utils/utils.js";
import { getSubscriptionInner } from "../stripe/getSubscription.js";
export async function getSitesFromOrg(
diff --git a/server/src/api/uptime/getMonitorEvents.ts b/server/src/api/uptime/getMonitorEvents.ts
index 136c72366..2f2e17531 100644
--- a/server/src/api/uptime/getMonitorEvents.ts
+++ b/server/src/api/uptime/getMonitorEvents.ts
@@ -4,7 +4,7 @@ import { db } from "../../db/postgres/postgres.js";
import { uptimeMonitors, member } from "../../db/postgres/schema.js";
import { clickhouse } from "../../db/clickhouse/clickhouse.js";
import { getSessionFromReq } from "../../lib/auth-utils.js";
-import { processResults } from "../analytics/utils.js";
+import { processResults } from "../analytics/utils/utils.js";
import { getMonitorEventsQuerySchema, type GetMonitorEventsQuery } from "./schemas.js";
interface GetMonitorEventsRequest {
diff --git a/server/src/api/uptime/getMonitorStats.ts b/server/src/api/uptime/getMonitorStats.ts
index ad9dff6a5..66fb2dc62 100644
--- a/server/src/api/uptime/getMonitorStats.ts
+++ b/server/src/api/uptime/getMonitorStats.ts
@@ -4,7 +4,7 @@ import { clickhouse } from "../../db/clickhouse/clickhouse.js";
import { db } from "../../db/postgres/postgres.js";
import { member, uptimeMonitors } from "../../db/postgres/schema.js";
import { getSessionFromReq } from "../../lib/auth-utils.js";
-import { processResults, TimeBucketToFn } from "../analytics/utils.js";
+import { processResults, TimeBucketToFn } from "../analytics/utils/utils.js";
import { getMonitorStatsQuerySchema, type GetMonitorStatsQuery } from "./schemas.js";
import { DateTime } from "luxon";
diff --git a/server/src/api/uptime/getMonitorStatus.ts b/server/src/api/uptime/getMonitorStatus.ts
index 1e2eefe02..d10787f09 100644
--- a/server/src/api/uptime/getMonitorStatus.ts
+++ b/server/src/api/uptime/getMonitorStatus.ts
@@ -4,7 +4,7 @@ import { db } from "../../db/postgres/postgres.js";
import { uptimeMonitors, uptimeMonitorStatus, member } from "../../db/postgres/schema.js";
import { clickhouse } from "../../db/clickhouse/clickhouse.js";
import { getSessionFromReq } from "../../lib/auth-utils.js";
-import { processResults } from "../analytics/utils.js";
+import { processResults } from "../analytics/utils/utils.js";
interface GetMonitorStatusRequest {
Params: {
diff --git a/server/src/api/uptime/getMonitorUptime.ts b/server/src/api/uptime/getMonitorUptime.ts
index 5d71898c7..42171ddf0 100644
--- a/server/src/api/uptime/getMonitorUptime.ts
+++ b/server/src/api/uptime/getMonitorUptime.ts
@@ -4,7 +4,7 @@ import { db } from "../../db/postgres/postgres.js";
import { uptimeMonitors, member } from "../../db/postgres/schema.js";
import { clickhouse } from "../../db/clickhouse/clickhouse.js";
import { getSessionFromReq } from "../../lib/auth-utils.js";
-import { processResults } from "../analytics/utils.js";
+import { processResults } from "../analytics/utils/utils.js";
interface GetMonitorUptimeRequest {
Params: {
diff --git a/server/src/api/uptime/getMonitorUptimeBuckets.ts b/server/src/api/uptime/getMonitorUptimeBuckets.ts
index 8cf898f20..bdf4c2e54 100644
--- a/server/src/api/uptime/getMonitorUptimeBuckets.ts
+++ b/server/src/api/uptime/getMonitorUptimeBuckets.ts
@@ -5,7 +5,7 @@ import { clickhouse } from "../../db/clickhouse/clickhouse.js";
import { db } from "../../db/postgres/postgres.js";
import { member, uptimeMonitors } from "../../db/postgres/schema.js";
import { getSessionFromReq } from "../../lib/auth-utils.js";
-import { processResults } from "../analytics/utils.js";
+import { processResults } from "../analytics/utils/utils.js";
// Query params schema
const getMonitorUptimeBucketsQuerySchema = z.object({
diff --git a/server/src/api/uptime/getMonitors.ts b/server/src/api/uptime/getMonitors.ts
index 0a30d0ca9..2fe24b4e0 100644
--- a/server/src/api/uptime/getMonitors.ts
+++ b/server/src/api/uptime/getMonitors.ts
@@ -5,7 +5,7 @@ import { db } from "../../db/postgres/postgres.js";
import { uptimeMonitors, uptimeMonitorStatus, member } from "../../db/postgres/schema.js";
import { clickhouse } from "../../db/clickhouse/clickhouse.js";
import { getSessionFromReq } from "../../lib/auth-utils.js";
-import { processResults } from "../analytics/utils.js";
+import { processResults } from "../analytics/utils/utils.js";
import { getMonitorsQuerySchema, type GetMonitorsQuery } from "./schemas.js";
interface GetMonitorsRequest {
diff --git a/server/src/services/import/importQuotaTracker.ts b/server/src/services/import/importQuotaTracker.ts
index a21a8fd5f..ea715c9ba 100644
--- a/server/src/services/import/importQuotaTracker.ts
+++ b/server/src/services/import/importQuotaTracker.ts
@@ -3,7 +3,7 @@ import { clickhouse } from "../../db/clickhouse/clickhouse.js";
import { db } from "../../db/postgres/postgres.js";
import { sites, organization } from "../../db/postgres/schema.js";
import { eq } from "drizzle-orm";
-import { processResults } from "../../api/analytics/utils.js";
+import { processResults } from "../../api/analytics/utils/utils.js";
import { getBestSubscription, type SubscriptionInfo } from "../../lib/subscriptionUtils.js";
import { IS_CLOUD } from "../../lib/const.js";
@@ -34,11 +34,7 @@ export class ImportQuotaTracker {
private readonly monthlyLimit: number;
private readonly oldestAllowedMonth: string;
- private constructor(
- monthlyUsage: Map<string, number>,
- monthlyLimit: number,
- oldestAllowedMonth: string
- ) {
+ private constructor(monthlyUsage: Map<string, number>, monthlyLimit: number, oldestAllowedMonth: string) {
this.monthlyUsage = monthlyUsage;
this.monthlyLimit = monthlyLimit;
this.oldestAllowedMonth = oldestAllowedMonth;
diff --git a/server/src/services/replay/sessionReplayIngestService.ts b/server/src/services/replay/sessionReplayIngestService.ts
index 23c329828..a2acc71aa 100644
--- a/server/src/services/replay/sessionReplayIngestService.ts
+++ b/server/src/services/replay/sessionReplayIngestService.ts
@@ -1,7 +1,7 @@
import { DateTime } from "luxon";
import { clickhouse } from "../../db/clickhouse/clickhouse.js";
import { RecordSessionReplayRequest } from "../../types/sessionReplay.js";
-import { processResults } from "../../api/analytics/utils.js";
+import { processResults } from "../../api/analytics/utils/utils.js";
import { parseTrackingData } from "./trackingUtils.js";
import { sessionsService } from "../sessions/sessionsService.js";
import { userIdService } from "../userId/userIdService.js";
diff --git a/server/src/services/replay/sessionReplayQueryService.ts b/server/src/services/replay/sessionReplayQueryService.ts
index fb68d35d6..594747f53 100644
--- a/server/src/services/replay/sessionReplayQueryService.ts
+++ b/server/src/services/replay/sessionReplayQueryService.ts
@@ -4,9 +4,10 @@ import {
SessionReplayListItem,
GetSessionReplayEventsResponse,
} from "../../types/sessionReplay.js";
-import { processResults, getTimeStatement, getFilterStatement } from "../../api/analytics/utils.js";
+import { processResults, getTimeStatement } from "../../api/analytics/utils/utils.js";
import { FilterParams } from "@rybbit/shared";
import { r2Storage } from "../storage/r2StorageService.js";
+import { getFilterStatement } from "../../api/analytics/utils/getFilterStatement.js";
/**
* Service responsible for querying/retrieving session replay data
@@ -288,7 +289,7 @@ export class SessionReplayQueryService {
const r2Keys = await processResults<{ event_data_key: string }>(r2KeysResult);
// Delete all R2 batches in parallel
- await Promise.all(r2Keys.map((row) => r2Storage.deleteBatch(row.event_data_key)));
+ await Promise.all(r2Keys.map(row => r2Storage.deleteBatch(row.event_data_key)));
} catch (error) {
console.error(`Failed to delete R2 data for session ${sessionId}:`, error);
// Continue with ClickHouse deletion even if R2 fails
diff --git a/server/src/services/telemetryService.ts b/server/src/services/telemetryService.ts
index a57667199..34ad65550 100644
--- a/server/src/services/telemetryService.ts
+++ b/server/src/services/telemetryService.ts
@@ -5,7 +5,7 @@ import path from "path";
import { fileURLToPath } from "url";
import { clickhouse } from "../db/clickhouse/clickhouse.js";
import { IS_CLOUD, DISABLE_TELEMETRY, SECRET } from "../lib/const.js";
-import { processResults } from "../api/analytics/utils.js";
+import { processResults } from "../api/analytics/utils/utils.js";
import { createServiceLogger } from "../lib/logger/logger.js";
const __filename = fileURLToPath(import.meta.url);
diff --git a/server/src/services/usageService.ts b/server/src/services/usageService.ts
index fcad613f5..9ae162471 100644
--- a/server/src/services/usageService.ts
+++ b/server/src/services/usageService.ts
@@ -1,7 +1,7 @@
import { and, eq } from "drizzle-orm";
import { DateTime } from "luxon";
import * as cron from "node-cron";
-import { processResults } from "../api/analytics/utils.js";
+import { processResults } from "../api/analytics/utils/utils.js";
import { clickhouse } from "../db/clickhouse/clickhouse.js";
import { db } from "../db/postgres/postgres.js";
import { member, organization, sites, user } from "../db/postgres/schema.js";
diff --git a/server/src/services/weekyReports/weeklyReportService.ts b/server/src/services/weekyReports/weeklyReportService.ts
index cf3b51e1e..88f6f3b25 100644
--- a/server/src/services/weekyReports/weeklyReportService.ts
+++ b/server/src/services/weekyReports/weeklyReportService.ts
@@ -4,7 +4,7 @@ import { eq } from "drizzle-orm";
import { db } from "../../db/postgres/postgres.js";
import { organization, member, user, sites } from "../../db/postgres/schema.js";
import { clickhouse } from "../../db/clickhouse/clickhouse.js";
-import { processResults } from "../../api/analytics/utils.js";
+import { processResults } from "../../api/analytics/utils/utils.js";
import { createServiceLogger } from "../../lib/logger/logger.js";
import { sendWeeklyReportEmail } from "../../lib/email/email.js";
import { IS_CLOUD } from "../../lib/const.js";
diff --git a/shared/src/filters.ts b/shared/src/filters.ts
index 15da32777..805d80d52 100644
--- a/shared/src/filters.ts
+++ b/shared/src/filters.ts
@@ -1,4 +1,12 @@
-export type FilterType = "equals" | "not_equals" | "contains" | "not_contains";
+export type FilterType =
+ | "equals"
+ | "not_equals"
+ | "contains"
+ | "not_contains"
+ | "regex"
+ | "not_regex"
+ | "greater_than"
+ | "less_than";
export type FilterParameter =
| "browser"