🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.
Add session filtering options for enhanced analytics
Bill Yangd2c637c6cc91
diff --git a/client/src/api/analytics/endpoints/sessions.ts b/client/src/api/analytics/endpoints/sessions.tsindex 90ef131c8..969763b33 100644--- a/client/src/api/analytics/endpoints/sessions.ts+++ b/client/src/api/analytics/endpoints/sessions.ts@@ -113,6 +113,9 @@ export type LiveSessionLocation = {export interface SessionsParams extends CommonApiParams, PaginationParams {userId?: string;identifiedOnly?: boolean;+ minPageviews?: number;+ minEvents?: number;+ minDuration?: number;}export interface SessionDetailsParams {@@ -136,6 +139,9 @@ export async function fetchSessions(limit: params.limit,user_id: params.userId,identified_only: params.identifiedOnly,+ min_pageviews: params.minPageviews,+ min_events: params.minEvents,+ min_duration: params.minDuration,};const response = await authedFetch<{ data: GetSessionsResponse }>(diff --git a/client/src/api/analytics/hooks/useGetUserSessions.ts b/client/src/api/analytics/hooks/useGetUserSessions.tsindex 647a78397..2caab81b2 100644--- a/client/src/api/analytics/hooks/useGetUserSessions.ts+++ b/client/src/api/analytics/hooks/useGetUserSessions.ts@@ -18,12 +18,18 @@ export function useGetSessions({limit = 100,identifiedOnly = false,timeOverride,+ minPageviews,+ minEvents,+ minDuration,}: {userId?: string;page?: number;limit?: number;identifiedOnly?: boolean;timeOverride?: Time;+ minPageviews?: number;+ minEvents?: number;+ minDuration?: number;}) {const { time, site, timezone } = useStore();@@ -36,7 +42,7 @@ export function useGetSessions({: buildApiParams(timeOverride || time, { filters: filteredFilters });return useQuery<{ data: GetSessionsResponse }>({- queryKey: ["sessions", timeOverride || time, site, filteredFilters, userId, page, limit, identifiedOnly, timezone],+ queryKey: ["sessions", timeOverride || time, site, filteredFilters, userId, page, limit, identifiedOnly, timezone, minPageviews, minEvents, minDuration],queryFn: () => {return fetchSessions(site, {...params,@@ -44,6 +50,9 @@ export function useGetSessions({limit,userId,identifiedOnly,+ minPageviews,+ minEvents,+ minDuration,});},staleTime: Infinity,diff --git a/client/src/app/[site]/sessions/page.tsx b/client/src/app/[site]/sessions/page.tsxindex 3fe5fc07d..8874f7e9c 100644--- a/client/src/app/[site]/sessions/page.tsx+++ b/client/src/app/[site]/sessions/page.tsx@@ -1,9 +1,15 @@"use client";import { SessionsList } from "@/components/Sessions/SessionsList";+import { Info } from "lucide-react";+import Link from "next/link";import { useState } from "react";import { useGetSessions } from "../../../api/analytics/hooks/useGetUserSessions";import { DisabledOverlay } from "../../../components/DisabledOverlay";+import { Input } from "../../../components/ui/input";+import { Label } from "../../../components/ui/label";+import { Switch } from "../../../components/ui/switch";+import { Tooltip, TooltipContent, TooltipTrigger } from "../../../components/ui/tooltip";import { useSetPageTitle } from "../../../hooks/useSetPageTitle";import { SESSION_PAGE_FILTERS } from "../../../lib/filterGroups";import { SubHeader } from "../components/SubHeader/SubHeader";@@ -14,17 +20,125 @@ export default function SessionsPage() {useSetPageTitle("Rybbit · Sessions");const [page, setPage] = useState(1);const [identifiedOnly, setIdentifiedOnly] = useState(false);+ const [minPageviews, setMinPageviews] = useState<number | undefined>(undefined);+ const [minEvents, setMinEvents] = useState<number | undefined>(undefined);+ const [minDuration, setMinDuration] = useState<number | undefined>(undefined);const { data, isLoading } = useGetSessions({page: page,limit: LIMIT + 1,identifiedOnly: identifiedOnly,+ minPageviews,+ minEvents,+ minDuration,});const allSessions = data?.data || [];const hasNextPage = allSessions.length > LIMIT;const sessions = allSessions.slice(0, LIMIT);const hasPrevPage = page > 1;+ const handleNumberInput = (+ value: string,+ setter: (val: number | undefined) => void+ ) => {+ if (value === "") {+ setter(undefined);+ } else {+ const num = parseInt(value, 10);+ if (!isNaN(num) && num >= 0) {+ setter(num);+ }+ }+ setPage(1);+ };++ const headerElement = (+ <div className="flex items-center gap-4 flex-wrap">+ {/* Identified only toggle */}+ <div className="flex items-center gap-2">+ <Switch+ id="identified-only"+ checked={identifiedOnly}+ onCheckedChange={val => {+ setIdentifiedOnly(val);+ setPage(1);+ }}+ />+ <Label+ htmlFor="identified-only"+ className="text-sm text-neutral-600 dark:text-neutral-400 cursor-pointer"+ >+ Identified only+ </Label>+ <Tooltip>+ <TooltipTrigger asChild>+ <Link href="https://www.rybbit.io/docs/identify-users" target="_blank">+ <Info className="h-4 w-4 text-neutral-400 hover:text-neutral-600 dark:hover:text-neutral-300 cursor-pointer" />+ </Link>+ </TooltipTrigger>+ <TooltipContent>+ <p>Learn how to identify users</p>+ </TooltipContent>+ </Tooltip>+ </div>++ {/* Min filters - hidden on mobile */}+ <div className="hidden md:flex items-center gap-4">+ <div className="flex items-center gap-2">+ <Label+ htmlFor="min-pageviews"+ className="text-sm text-neutral-600 dark:text-neutral-400 whitespace-nowrap"+ >+ Min pageviews+ </Label>+ <Input+ id="min-pageviews"+ type="number"+ min={0}+ placeholder="0"+ value={minPageviews ?? ""}+ onChange={e => handleNumberInput(e.target.value, setMinPageviews)}+ className="w-20 h-8"+ />+ </div>+ <div className="flex items-center gap-2">+ <Label+ htmlFor="min-events"+ className="text-sm text-neutral-600 dark:text-neutral-400 whitespace-nowrap"+ >+ Min events+ </Label>+ <Input+ id="min-events"+ type="number"+ min={0}+ placeholder="0"+ value={minEvents ?? ""}+ onChange={e => handleNumberInput(e.target.value, setMinEvents)}+ className="w-20 h-8"+ />+ </div>+ <div className="flex items-center gap-2">+ <Label+ htmlFor="min-duration"+ className="text-sm text-neutral-600 dark:text-neutral-400 whitespace-nowrap"+ >+ Min duration (s)+ </Label>+ <Input+ id="min-duration"+ type="number"+ min={0}+ placeholder="0"+ value={minDuration ?? ""}+ onChange={e => handleNumberInput(e.target.value, setMinDuration)}+ className="w-20 h-8"+ />+ </div>+ </div>+ </div>+ );+return (<DisabledOverlay message="Sessions" featurePath="sessions"><div className="p-2 md:p-4 max-w-[1300px] mx-auto space-y-3">@@ -36,8 +150,7 @@ export default function SessionsPage() {onPageChange={setPage}hasNextPage={hasNextPage}hasPrevPage={hasPrevPage}- setIdentifiedOnly={setIdentifiedOnly}- identifiedOnly={identifiedOnly}+ headerElement={headerElement}/></div></DisabledOverlay>diff --git a/client/src/components/Sessions/SessionsList.tsx b/client/src/components/Sessions/SessionsList.tsxindex 6456f8b29..1cf1e7ca3 100644--- a/client/src/components/Sessions/SessionsList.tsx+++ b/client/src/components/Sessions/SessionsList.tsx@@ -1,12 +1,8 @@-import { ChevronLeft, ChevronRight, Info, Rewind } from "lucide-react";+import { ChevronLeft, ChevronRight, Rewind } from "lucide-react";import { GetSessionsResponse } from "../../api/analytics/endpoints";import { NothingFound } from "../NothingFound";import { Button } from "../ui/button";import { SessionCard, SessionCardSkeleton } from "./SessionCard";-import { Switch } from "../ui/switch";-import { Label } from "../ui/label";-import { Tooltip, TooltipContent, TooltipTrigger } from "../ui/tooltip";-import Link from "next/link";interface SessionsListProps {sessions: GetSessionsResponse;@@ -17,8 +13,7 @@ interface SessionsListProps {hasPrevPage: boolean;emptyMessage?: string;userId?: string;- identifiedOnly?: boolean;- setIdentifiedOnly?: (identifiedOnly: boolean) => void;+ headerElement?: React.ReactNode;pageSize?: number;}@@ -31,33 +26,15 @@ export function SessionsList({hasPrevPage,emptyMessage = "Try a different date range or filter",userId,- identifiedOnly,- setIdentifiedOnly,+ headerElement,pageSize,}: SessionsListProps) {return (<div className="space-y-3">- {/* Pagination controls */}+ {/* Header and pagination controls */}<div className="flex items-center justify-between gap-2">- {setIdentifiedOnly && (- <div className="flex items-center justify-end gap-2">- <Switch id="identified-only" checked={identifiedOnly} onCheckedChange={setIdentifiedOnly} />- <Label htmlFor="identified-only" className="text-sm text-neutral-600 dark:text-neutral-400 cursor-pointer">- Identified only- </Label>- <Tooltip>- <TooltipTrigger asChild>- <Link href="https://www.rybbit.io/docs/identify-users" target="_blank">- <Info className="h-4 w-4 text-neutral-400 hover:text-neutral-600 dark:hover:text-neutral-300 cursor-pointer" />- </Link>- </TooltipTrigger>- <TooltipContent>- <p>Learn how to identify users</p>- </TooltipContent>- </Tooltip>- </div>- )}- <div className="flex items-center justify-end gap-2">+ {headerElement}+ <div className="flex items-center justify-end gap-2 ml-auto"><Button variant="ghost" size="smIcon" onClick={() => onPageChange(page - 1)} disabled={!hasPrevPage}><ChevronLeft className="h-4 w-4" /></Button>diff --git a/server/src/api/analytics/getSessions.ts b/server/src/api/analytics/getSessions.tsindex e7f97366f..5e22d364c 100644--- a/server/src/api/analytics/getSessions.ts+++ b/server/src/api/analytics/getSessions.ts@@ -54,6 +54,9 @@ export interface GetSessionsRequest {page: number;user_id?: string;identified_only?: string;+ min_pageviews?: string;+ min_events?: string;+ min_duration?: string;}>;}@@ -67,9 +70,21 @@ const SESSION_FIELD_MAPPINGS = {};export async function getSessions(req: FastifyRequest<GetSessionsRequest>, res: FastifyReply) {- const { filters, page = 1, user_id: userId, limit = 100, identified_only: identifiedOnly = "false" } = req.query;+ const {+ filters,+ page = 1,+ user_id: userId,+ limit = 100,+ identified_only: identifiedOnly = "false",+ min_pageviews: minPageviewsStr,+ min_events: minEventsStr,+ min_duration: minDurationStr,+ } = req.query;const site = req.params.siteId;const filterIdentified = identifiedOnly === "true";+ const minPageviews = minPageviewsStr ? parseInt(minPageviewsStr, 10) : undefined;+ const minEvents = minEventsStr ? parseInt(minEventsStr, 10) : undefined;+ const minDuration = minDurationStr ? parseInt(minDurationStr, 10) : undefined;const timeStatement = getTimeStatement(req.query);@@ -141,6 +156,9 @@ export async function getSessions(req: FastifyRequest<GetSessionsRequest>, res:LEFT JOIN ReplaySessions r ON a.session_id = r.session_idWHERE 1 = 1 ${filterStatement}${filterIdentified ? "AND a.identified_user_id != ''" : ""}+ ${minPageviews !== undefined ? "AND a.pageviews >= {minPageviews:Int32}" : ""}+ ${minEvents !== undefined ? "AND a.events >= {minEvents:Int32}" : ""}+ ${minDuration !== undefined ? "AND a.session_duration >= {minDuration:Int32}" : ""}LIMIT {limit:Int32} OFFSET {offset:Int32}`;@@ -153,6 +171,9 @@ export async function getSessions(req: FastifyRequest<GetSessionsRequest>, res:user_id: userId,limit: limit || 100,offset: (page - 1) * (limit || 100),+ minPageviews: minPageviews ?? 0,+ minEvents: minEvents ?? 0,+ minDuration: minDuration ?? 0,},});