🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.
feat(filters): add support for browser and OS version filters (#326)
Bill Yangb737f1ce310d
diff --git a/client/src/api/analytics/usePaginatedSingleCol.ts b/client/src/api/analytics/usePaginatedSingleCol.tsindex 75e97e0d3..f680e3912 100644--- a/client/src/api/analytics/usePaginatedSingleCol.ts+++ b/client/src/api/analytics/usePaginatedSingleCol.ts@@ -2,7 +2,7 @@ import { SingleColResponse } from "@/api/analytics/useSingleCol";import { authedFetch, getStartAndEndDate } from "@/api/utils";import { BACKEND_URL } from "@/lib/const";import { timeZone } from "@/lib/dateTimeUtils";-import { FilterParameter, useStore } from "@/lib/store";+import { Filter, FilterParameter, useStore } from "@/lib/store";import { useQuery, UseQueryResult } from "@tanstack/react-query";type UsePaginatedSingleColOptions = {@@ -10,6 +10,8 @@ type UsePaginatedSingleColOptions = {limit?: number;page?: number;useFilters?: boolean;+ enabled?: boolean;+ additionalFilters?: Filter[];};type PaginatedResponse = {@@ -22,6 +24,8 @@ export function usePaginatedSingleCol({limit = 10,page = 1,useFilters = true,+ enabled = true,+ additionalFilters = [],}: UsePaginatedSingleColOptions): UseQueryResult<PaginatedResponse> {const { time, site, filters } = useStore();@@ -38,7 +42,7 @@ export function usePaginatedSingleCol({parameter,limit,page,- filters: useFilters ? filters : undefined,+ filters: useFilters ? [...filters, ...additionalFilters] : undefined,}: {// Regular date-based approach@@ -47,7 +51,7 @@ export function usePaginatedSingleCol({parameter,limit,page,- filters: useFilters ? filters : undefined,+ filters: useFilters ? [...filters, ...additionalFilters] : undefined,};return useQuery({@@ -59,6 +63,7 @@ export function usePaginatedSingleCol({limit,page,isPast24HoursMode ? "past-minutes" : "date-range",+ additionalFilters,],queryFn: () => {return authedFetch(`${BACKEND_URL}/single-col/${site}`, queryParams)@@ -76,5 +81,6 @@ export function usePaginatedSingleCol({}return undefined;},+ enabled,});}diff --git a/client/src/app/[site]/components/shared/Filters/const.tsx b/client/src/app/[site]/components/shared/Filters/const.tsxindex cb960cd5b..376125019 100644--- a/client/src/app/[site]/components/shared/Filters/const.tsx+++ b/client/src/app/[site]/components/shared/Filters/const.tsx@@ -9,7 +9,6 @@ import {Link,LogIn,LogOut,- MapPin,MapPinHouse,MapPinned,Maximize,@@ -18,7 +17,6 @@ import {Puzzle,Radio,Search,- Share2,TabletSmartphone,Tag,Target,@@ -96,11 +94,21 @@ export const FilterOptions: {value: "operating_system",icon: <Brain className="h-4 w-4" />,},+ {+ label: "Operating System Version",+ value: "operating_system_version",+ icon: <Brain className="h-4 w-4" />,+ },{label: "Browser",value: "browser",icon: <AppWindow className="h-4 w-4" />,},+ {+ label: "Browser Version",+ value: "browser_version",+ icon: <AppWindow className="h-4 w-4" />,+ },{label: "Language",value: "language",diff --git a/client/src/app/[site]/components/shared/Filters/utils.ts b/client/src/app/[site]/components/shared/Filters/utils.tsindex 1943024ca..befe4201e 100644--- a/client/src/app/[site]/components/shared/Filters/utils.ts+++ b/client/src/app/[site]/components/shared/Filters/utils.ts@@ -45,6 +45,10 @@ export function getParameterNameLabel(parameter: FilterParameter) {return "UTM Term";case "utm_content":return "UTM Content";+ case "browser_version":+ return "Browser Version";+ case "operating_system_version":+ return "OS Version";default:return parameter;}diff --git a/client/src/app/[site]/components/shared/StandardSection/Row.tsx b/client/src/app/[site]/components/shared/StandardSection/Row.tsxindex 48c133855..4ba3d59ce 100644--- a/client/src/app/[site]/components/shared/StandardSection/Row.tsx+++ b/client/src/app/[site]/components/shared/StandardSection/Row.tsx@@ -1,7 +1,8 @@import NumberFlow from "@number-flow/react";import { round } from "lodash";-import { SquareArrowOutUpRight } from "lucide-react";-import { ReactNode } from "react";+import { ChevronDown, ChevronRight, SquareArrowOutUpRight } from "lucide-react";+import { ReactNode, useState, useCallback } from "react";+import { usePaginatedSingleCol } from "../../../../../api/analytics/usePaginatedSingleCol";import { SingleColResponse } from "../../../../../api/analytics/useSingleCol";import {addFilter,@@ -10,56 +11,70 @@ import {useStore,} from "../../../../../lib/store";-export const Row = ({- e,+// Custom hook for filter handling logic+const useFilterToggle = () => {+ const filters = useStore((state) => state.filters);++ const toggleFilter = useCallback(+ (parameter: FilterParameter, value: string) => {+ const foundFilter = filters.find(+ (f) => f.parameter === parameter && f.value.some((v) => v === value)+ );+ if (foundFilter) {+ removeFilter(foundFilter);+ } else {+ addFilter({+ parameter,+ value: [value],+ type: "equals",+ });+ }+ },+ [filters]+ );++ return toggleFilter;+};++// Shared row item component+const RowItem = ({+ item,ratio,getKey,getLabel,getValue,getLink,filterParameter,+ onFilterToggle,+ leftContent,}: {- e: SingleColResponse;+ item: SingleColResponse;ratio: number;getKey: (item: SingleColResponse) => string;getLabel: (item: SingleColResponse) => ReactNode;getValue: (item: SingleColResponse) => string;getLink?: (item: SingleColResponse) => string;filterParameter: FilterParameter;+ onFilterToggle: (parameter: FilterParameter, value: string) => void;+ leftContent?: ReactNode;}) => {- const filters = useStore((state) => state.filters);-return (<div- key={getKey(e)}+ key={getKey(item)}className="relative h-6 flex items-center cursor-pointer hover:bg-neutral-850 group"- onClick={() => {- const foundFilter = filters.find(- (f) =>- f.parameter === filterParameter &&- f.value.some((v) => v === getValue(e))- );- if (foundFilter) {- removeFilter(foundFilter);- } else {- addFilter({- parameter: filterParameter,- value: [getValue(e)],- type: "equals",- });- }- }}+ onClick={() => onFilterToggle(filterParameter, getValue(item))}><divclassName="absolute inset-0 bg-dataviz py-2 opacity-25 rounded-md"- style={{ width: `${e.percentage * ratio}%` }}+ style={{ width: `${item.percentage * ratio}%` }}></div><div className="z-10 mx-2 flex justify-between items-center text-xs w-full"><div className="flex items-center gap-1">- {getLabel(e)}+ {leftContent}+ {getLabel(item)}{getLink && (<a- href={getLink(e)}+ href={getLink(item)}target="_blank"onClick={(e) => e.stopPropagation()}>@@ -72,11 +87,11 @@ export const Row = ({</div><div className="text-xs flex gap-2"><div className="hidden group-hover:block text-neutral-400">- {round(e.percentage, 1)}%+ {round(item.percentage, 1)}%</div><NumberFlowrespectMotionPreference={false}- value={e.count}+ value={item.count}format={{ notation: "compact" }}/></div>@@ -84,3 +99,127 @@ export const Row = ({</div>);};++const Subrows = ({+ getKey,+ getValue,+ getLink,+ filterParameter,+ filterValue,+ getSubrowLabel,+}: {+ getKey: (item: SingleColResponse) => string;+ getValue: (item: SingleColResponse) => string;+ getLink?: (item: SingleColResponse) => string;+ filterParameter: FilterParameter;+ filterValue: string;+ getSubrowLabel?: (item: SingleColResponse) => ReactNode;+}) => {+ const toggleFilter = useFilterToggle();+ const parameter = (filterParameter + "_version") as FilterParameter;++ const { data, isLoading, isFetching } = usePaginatedSingleCol({+ parameter,+ limit: 10,+ page: 1,+ additionalFilters: [+ {+ parameter: filterParameter,+ value: [filterValue],+ type: "equals",+ },+ ],+ });++ const itemsForDisplay = data?.data;++ const ratio = itemsForDisplay?.[0]?.percentage+ ? 100 / itemsForDisplay[0].percentage+ : 1;++ if (isLoading || isFetching) {+ return null;+ }++ return (+ <div className="flex flex-col gap-2 pl-2 pt-2">+ {itemsForDisplay?.map((e) => (+ <RowItem+ key={getKey(e)}+ item={e}+ ratio={ratio}+ getKey={getKey}+ getLabel={getSubrowLabel || getValue}+ getValue={getValue}+ getLink={getLink}+ filterParameter={parameter}+ onFilterToggle={toggleFilter}+ />+ ))}+ </div>+ );+};++export const Row = ({+ e,+ ratio,+ getKey,+ getLabel,+ getValue,+ getLink,+ filterParameter,+ getSubrowLabel,+ hasSubrow,+}: {+ e: SingleColResponse;+ ratio: number;+ getKey: (item: SingleColResponse) => string;+ getLabel: (item: SingleColResponse) => ReactNode;+ getValue: (item: SingleColResponse) => string;+ getLink?: (item: SingleColResponse) => string;+ filterParameter: FilterParameter;+ getSubrowLabel?: (item: SingleColResponse) => ReactNode;+ hasSubrow?: boolean;+}) => {+ const toggleFilter = useFilterToggle();+ const [expanded, setExpanded] = useState(false);++ const Icon = expanded ? ChevronDown : ChevronRight;++ const expandIcon = hasSubrow ? (+ <Icon+ className="w-4 h-4 text-neutral-400 hover:text-neutral-100"+ strokeWidth={3}+ onClick={(e) => {+ e.stopPropagation();+ setExpanded((prev) => !prev);+ }}+ />+ ) : null;++ return (+ <div className="flex flex-col">+ <RowItem+ item={e}+ ratio={ratio}+ getKey={getKey}+ getLabel={getLabel}+ getValue={getValue}+ getLink={getLink}+ filterParameter={filterParameter}+ onFilterToggle={toggleFilter}+ leftContent={expandIcon}+ />+ {hasSubrow && expanded && (+ <Subrows+ getKey={getKey}+ getValue={getValue}+ getLink={getLink}+ filterParameter={filterParameter}+ filterValue={getValue(e)}+ getSubrowLabel={getSubrowLabel}+ />+ )}+ </div>+ );+};diff --git a/client/src/app/[site]/components/shared/StandardSection/StandardSection.tsx b/client/src/app/[site]/components/shared/StandardSection/StandardSection.tsxindex f5a2abc4a..4a28cc917 100644--- a/client/src/app/[site]/components/shared/StandardSection/StandardSection.tsx+++ b/client/src/app/[site]/components/shared/StandardSection/StandardSection.tsx@@ -22,6 +22,8 @@ export function StandardSection({filterParameter,expanded,close,+ hasSubrow,+ getSubrowLabel,}: {title: string;getKey: (item: SingleColResponse) => string;@@ -33,6 +35,8 @@ export function StandardSection({filterParameter: FilterParameter;expanded: boolean;close: () => void;+ hasSubrow?: boolean;+ getSubrowLabel?: (item: SingleColResponse) => ReactNode;}) {const { data, isLoading, isFetching, error, refetch } = usePaginatedSingleCol({@@ -55,7 +59,7 @@ export function StandardSection({<CardLoader /></div>)}- <div className="flex flex-col gap-2">+ <div className="flex flex-col gap-2 max-h-[344px] overflow-y-auto">{isLoading ? (<Skeleton />) : error ? (@@ -97,6 +101,8 @@ export function StandardSection({getValue={getValue}getLink={getLink}filterParameter={filterParameter}+ getSubrowLabel={getSubrowLabel}+ hasSubrow={hasSubrow}/>))) : (diff --git a/client/src/app/[site]/events/components/EventList.tsx b/client/src/app/[site]/events/components/EventList.tsxindex fc4ba0c07..3454b7553 100644--- a/client/src/app/[site]/events/components/EventList.tsx+++ b/client/src/app/[site]/events/components/EventList.tsx@@ -164,9 +164,15 @@ export function EventList({><div className="font-medium truncate max-w-[70%] flex items-center gap-1">{isExpanded ? (- <ChevronDown className="h-4 w-4 text-neutral-400" />+ <ChevronDown+ className="h-4 w-4 text-neutral-400 hover:text-neutral-100"+ strokeWidth={3}+ />) : (- <ChevronRight className="h-4 w-4 text-neutral-400" />+ <ChevronRight+ className="h-4 w-4 text-neutral-400 hover:text-neutral-100"+ strokeWidth={3}+ />)}{event.eventName}</div>diff --git a/client/src/app/[site]/funnels/components/FunnelRow.tsx b/client/src/app/[site]/funnels/components/FunnelRow.tsxindex 36181e2bd..7f75d2ff5 100644--- a/client/src/app/[site]/funnels/components/FunnelRow.tsx+++ b/client/src/app/[site]/funnels/components/FunnelRow.tsx@@ -179,7 +179,13 @@ export function FunnelRow({ funnel }: FunnelRowProps) {{getParameterNameLabel(filter.parameter)}</span><span- className={cn("mx-1", filter.type === "not_equals" || filter.type === "not_contains" ? "text-red-400" : "text-emerald-400")}+ className={cn(+ "mx-1",+ filter.type === "not_equals" ||+ filter.type === "not_contains"+ ? "text-red-400"+ : "text-emerald-400"+ )}>{filterTypeToLabel(filter.type)}</span>@@ -223,7 +229,11 @@ export function FunnelRow({ funnel }: FunnelRowProps) {</Button><Button variant="ghost" size="icon" onClick={handleExpand}>- {expanded ? <ChevronUp /> : <ChevronDown />}+ {expanded ? (+ <ChevronUp strokeWidth={3} />+ ) : (+ <ChevronDown strokeWidth={3} />+ )}</Button></div></div>diff --git a/client/src/app/[site]/main/components/sections/Devices.tsx b/client/src/app/[site]/main/components/sections/Devices.tsxindex a9a44e589..0b884117e 100644--- a/client/src/app/[site]/main/components/sections/Devices.tsx+++ b/client/src/app/[site]/main/components/sections/Devices.tsx@@ -14,7 +14,13 @@ import { Browser } from "../../../components/shared/icons/Browser";import { OperatingSystem } from "../../../components/shared/icons/OperatingSystem";import { Button } from "../../../../../components/ui/button";-type Tab = "devices" | "browsers" | "os" | "dimensions";+type Tab =+ | "devices"+ | "browsers"+ | "os"+ | "dimensions"+ | "browser_versions"+ | "os_versions";export function Devices() {const [tab, setTab] = useState<Tab>("browsers");@@ -76,8 +82,18 @@ export function Devices() {{e.value || "Other"}</div>)}+ getSubrowLabel={(e) => {+ const justBrowser = e.value.split(" ").slice(0, -1).join(" ");+ return (+ <div className="flex gap-2 items-center">+ <Browser browser={justBrowser || "Other"} />+ {e.value || "Other"}+ </div>+ );+ }}expanded={expanded}close={close}+ hasSubrow={true}/></TabsContent><TabsContent value="os">@@ -91,9 +107,19 @@ export function Devices() {{e.value || "Other"}</div>)}+ getSubrowLabel={(e) => {+ const justOS = e.value.split(" ").slice(0, -1).join(" ");+ return (+ <div className="flex gap-2 items-center">+ <OperatingSystem os={justOS || "Other"} />+ {e.value || "Other"}+ </div>+ );+ }}filterParameter="operating_system"expanded={expanded}close={close}+ hasSubrow={true}/></TabsContent><TabsContent value="dimensions">diff --git a/client/src/components/Sessions/SessionCard.tsx b/client/src/components/Sessions/SessionCard.tsxindex c152187f6..70a690dd1 100644--- a/client/src/components/Sessions/SessionCard.tsx+++ b/client/src/components/Sessions/SessionCard.tsx@@ -203,9 +203,9 @@ export function SessionCard({ session, onClick, userId }: SessionCardProps) {{/* Expand/Collapse icon */}<div className="ml-2 flex-shrink-0 hidden md:flex">{expanded ? (- <ChevronDown className="w-4 h-4 text-gray-400" />+ <ChevronDown className="w-4 h-4 text-gray-400" strokeWidth={3} />) : (- <ChevronRight className="w-4 h-4 text-gray-400" />+ <ChevronRight className="w-4 h-4 text-gray-400" strokeWidth={3} />)}</div></div>diff --git a/client/src/lib/store.ts b/client/src/lib/store.tsindex c589e66c7..9c6848651 100644--- a/client/src/lib/store.ts+++ b/client/src/lib/store.ts@@ -45,7 +45,9 @@ export type FilterParameter =// derivative parameters| "entry_page"| "exit_page"- | "dimensions";+ | "dimensions"+ | "browser_version"+ | "operating_system_version";export const SESSION_PAGE_FILTERS: FilterParameter[] = ["browser",diff --git a/server/src/api/analytics/query-validation.ts b/server/src/api/analytics/query-validation.tsindex 816d5c920..ed6c97aeb 100644--- a/server/src/api/analytics/query-validation.ts+++ b/server/src/api/analytics/query-validation.ts@@ -197,6 +197,8 @@ export const filterParamSchema = z.enum(["entry_page","exit_page","dimensions",+ "browser_version",+ "operating_system_version",]);/**diff --git a/server/src/api/analytics/types.ts b/server/src/api/analytics/types.tsindex 9839dc524..4fc12ffb7 100644--- a/server/src/api/analytics/types.ts+++ b/server/src/api/analytics/types.ts@@ -22,7 +22,9 @@ export type FilterParameter =// derivative parameters| "entry_page"| "exit_page"- | "dimensions";+ | "dimensions"+ | "browser_version"+ | "operating_system_version";export type Filter = {parameter: FilterParameter;diff --git a/server/src/api/analytics/utils.ts b/server/src/api/analytics/utils.tsindex 7c4ee5dc7..5146155fb 100644--- a/server/src/api/analytics/utils.ts+++ b/server/src/api/analytics/utils.ts@@ -130,6 +130,16 @@ export const getSqlParam = (parameter: FilterParameter) => {if (parameter === "city") {return "concat(toString(region), '-', toString(city))";}+ if (parameter === "browser_version") {+ 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))+ END`;+ }return filterParamSchema.parse(parameter);};