Skip to content

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

Add tags (#831)

Goldflagba2216dd2c56
diff --git a/client/src/api/admin/endpoints/sites.ts b/client/src/api/admin/endpoints/sites.ts
index 80f7b55e7..f0b7907aa 100644
--- a/client/src/api/admin/endpoints/sites.ts
+++ b/client/src/api/admin/endpoints/sites.ts
@@ -22,6 +22,7 @@ export type SiteResponse = {
trackInitialPageView?: boolean;
trackSpaNavigation?: boolean;
trackIp?: boolean;
+ tags?: string[];
};
export type GetSitesFromOrgResponse = {
@@ -50,6 +51,7 @@ export type GetSitesFromOrgResponse = {
blockBots: boolean;
sessionsLast24Hours: number;
isOwner: boolean;
+ tags?: string[] | null;
}>;
subscription: {
monthlyEventCount: number;
@@ -113,6 +115,7 @@ export function updateSiteConfig(
trackUrlParams?: boolean;
trackInitialPageView?: boolean;
trackSpaNavigation?: boolean;
+ tags?: string[];
}
) {
return authedFetch(`/sites/${siteId}/config`, undefined, {
diff --git a/client/src/app/components/AddSite.tsx b/client/src/app/components/AddSite.tsx
index 5bbf71acd..5cc406a3d 100644
--- a/client/src/app/components/AddSite.tsx
+++ b/client/src/app/components/AddSite.tsx
@@ -124,12 +124,12 @@ export function AddSite({ trigger, disabled }: { trigger?: React.ReactNode; disa
return (
<Tooltip>
<TooltipTrigger asChild>
- <span>
+ {trigger || (
<Button disabled title="Upgrade to Pro to add more websites">
<Plus className="h-4 w-4" />
Add Website
</Button>
- </span>
+ )}
</TooltipTrigger>
<TooltipContent>
You have reached the limit of {subscription?.isPro ? STANDARD_SITE_LIMIT : FREE_SITE_LIMIT} websites. Upgrade
diff --git a/client/src/app/page.tsx b/client/src/app/page.tsx
index a80d6ca14..80420677d 100644
--- a/client/src/app/page.tsx
+++ b/client/src/app/page.tsx
@@ -1,7 +1,7 @@
"use client";
import { ChevronLeft, ChevronRight, Plus, Search } from "lucide-react";
-import { useEffect, useState } from "react";
+import { useEffect, useMemo, useState } from "react";
import { useUserOrganizations } from "../api/admin/hooks/useOrganizations";
import { useGetSitesFromOrg } from "../api/admin/hooks/useSites";
import { CreateOrganizationDialog } from "../components/CreateOrganizationDialog";
@@ -13,6 +13,7 @@ import { StandardPage } from "../components/StandardPage";
import { Button } from "../components/ui/button";
import { Card, CardDescription, CardTitle } from "../components/ui/card";
import { Input } from "../components/ui/input";
+import { MultiSelect } from "../components/ui/multi-select";
import { useSetPageTitle } from "../hooks/useSetPageTitle";
import { authClient } from "../lib/auth";
import { canGoForward, goBack, goForward, useStore } from "../lib/store";
@@ -51,15 +52,24 @@ export default function Home() {
const [createOrgDialogOpen, setCreateOrgDialogOpen] = useState(false);
const [domainFilter, setDomainFilter] = useState("");
+ const [selectedTags, setSelectedTags] = useState<string[]>([]);
+
+ // Compute unique tags from all sites
+ const allTags = useMemo(() => {
+ const tags = sites?.sites?.flatMap(s => s.tags || []) || [];
+ return [...new Set(tags)].sort();
+ }, [sites?.sites]);
// Track hydration to avoid mismatch with date-dependent disabled states
const [isMounted, setIsMounted] = useState(false);
useEffect(() => setIsMounted(true), []);
- // Filter sites by domain
- const filteredSites = sites?.sites?.filter(site =>
- site.domain.toLowerCase().includes(domainFilter.toLowerCase())
- );
+ // Filter sites by domain and tags
+ const filteredSites = sites?.sites?.filter(site => {
+ const matchesDomain = site.domain.toLowerCase().includes(domainFilter.toLowerCase());
+ const matchesTags = selectedTags.length === 0 || selectedTags.some(tag => site.tags?.includes(tag));
+ return matchesDomain && matchesTags;
+ });
// Handle successful organization creation
const handleOrganizationCreated = () => {
@@ -67,6 +77,13 @@ export default function Home() {
refetchSites();
};
+ // Handle tag click to toggle filter
+ const handleTagClick = (tag: string) => {
+ setSelectedTags(prev =>
+ prev.includes(tag) ? prev.filter(t => t !== tag) : [...prev, tag]
+ );
+ };
+
return (
<StandardPage>
<div className="flex justify-between items-center my-4">
@@ -99,19 +116,48 @@ export default function Home() {
</div>
{hasNoOrganizations && <NoOrganization />}
{shouldShowSites && sites?.sites && sites.sites.length > 0 && (
- <div className="relative mb-4">
- <Search className="absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-neutral-500" />
- <Input
- placeholder="Filter by domain..."
- value={domainFilter}
- onChange={e => setDomainFilter(e.target.value)}
- className="pl-9"
- />
+ <div className="flex gap-2 mb-4">
+ <div className="relative flex-1">
+ <Search className="absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-neutral-500" />
+ <Input
+ placeholder="Filter by domain..."
+ value={domainFilter}
+ onChange={e => setDomainFilter(e.target.value)}
+ className="pl-9"
+ />
+ </div>
+ {allTags.length > 0 && (
+ <div className="w-[200px]">
+ <MultiSelect
+ options={allTags.map(tag => ({ value: tag, label: tag }))}
+ value={selectedTags}
+ onValueChange={setSelectedTags}
+ placeholder="Filter by tags..."
+ searchPlaceholder="Search tags..."
+ emptyText="No tags found."
+ className="border-neutral-150 dark:border-neutral-800"
+ />
+ </div>
+ )}
+ <div className="hidden md:block">
+ <AddSite disabled={!canAddSites} />
+ </div>
</div>
)}
<div className="flex flex-col gap-2">
{filteredSites?.map(site => {
- return <SiteCard key={site.siteId} siteId={site.siteId} domain={site.domain} />;
+ return (
+ <SiteCard
+ key={site.siteId}
+ siteId={site.siteId}
+ domain={site.domain}
+ tags={site.tags || []}
+ allTags={allTags}
+ onTagsUpdated={refetchSites}
+ selectedTags={selectedTags}
+ onTagClick={handleTagClick}
+ />
+ );
})}
{hasNoSites ? (
<Card className="p-6 flex flex-col items-center text-center">
@@ -126,11 +172,7 @@ export default function Home() {
}
/>
</Card>
- ) : (
- <div className="flex justify-center">
- <AddSite disabled={!canAddSites} />
- </div>
- )}
+ ) : null}
</div>
<CreateOrganizationDialog
open={createOrgDialogOpen}
diff --git a/client/src/components/SiteCard.tsx b/client/src/components/SiteCard.tsx
index aa033cfc9..5ea9e140d 100644
--- a/client/src/components/SiteCard.tsx
+++ b/client/src/components/SiteCard.tsx
@@ -1,4 +1,4 @@
-import { Settings } from "lucide-react";
+import { Tag, Settings } from "lucide-react";
import Link from "next/link";
import { useRef } from "react";
import { useGetOverview } from "../api/analytics/hooks/useGetOverview";
@@ -10,14 +10,22 @@ import { formatter } from "../lib/utils";
import { Favicon } from "./Favicon";
import { SiteSessionChart } from "./SiteSessionChart";
import { SiteSettings } from "./SiteSettings/SiteSettings";
+import { TagEditor } from "./TagEditor";
+import { Badge } from "./ui/badge";
import { Skeleton } from "./ui/skeleton";
+import { Tooltip, TooltipContent, TooltipTrigger } from "./ui/tooltip";
interface SiteCardProps {
siteId: number;
domain: string;
+ tags?: string[];
+ allTags?: string[];
+ onTagsUpdated?: () => void;
+ selectedTags?: string[];
+ onTagClick?: (tag: string) => void;
}
-export function SiteCard({ siteId, domain }: SiteCardProps) {
+export function SiteCard({ siteId, domain, tags = [], allTags = [], onTagsUpdated, selectedTags = [], onTagClick }: SiteCardProps) {
const { ref, isInView } = useInView({
// Start loading slightly before the card comes into view
rootMargin: "200px",
@@ -47,7 +55,7 @@ export function SiteCard({ siteId, domain }: SiteCardProps) {
});
// Previous period - automatically handles both regular time-based and past-minutes queries
- const { data: overviewDataPrevious, isLoading: isOverviewLoadingPrevious } = useGetOverview({
+ const { data: overviewDataPrevious } = useGetOverview({
site: siteId,
periodTime: "previous",
});
@@ -94,14 +102,57 @@ export function SiteCard({ siteId, domain }: SiteCardProps) {
<Favicon domain={domain} className="w-6 h-6" />
<span className="text-lg font-medium truncate group-hover:underline transition-all">{domain}</span>
<div onClick={(e) => e.preventDefault()}>
- <SiteSettings
- siteId={siteId}
- trigger={
- <button className="p-1 rounded hover:bg-neutral-100 dark:hover:bg-neutral-800 transition-colors">
- <Settings className="h-4 w-4 text-neutral-500" />
- </button>
- }
- />
+ <Tooltip>
+ <SiteSettings
+ siteId={siteId}
+ trigger={
+ <TooltipTrigger asChild>
+ <button className="p-1 rounded hover:bg-neutral-100 dark:hover:bg-neutral-800 transition-colors">
+ <Settings className="h-4 w-4 text-neutral-400" />
+ </button>
+ </TooltipTrigger>
+ }
+ />
+ <TooltipContent>Site Settings</TooltipContent>
+ </Tooltip>
+ </div>
+ {/* Tags display */}
+ <div onClick={(e) => e.preventDefault()} className="flex items-center gap-1">
+ {tags.slice(0, 3).map(tag => {
+ return (
+ <Badge
+ key={tag}
+ variant={"default"}
+ className={`text-xs ${onTagClick ? "cursor-pointer" : ""}`}
+ onClick={onTagClick ? () => onTagClick(tag) : undefined}
+ >
+ {tag}
+ </Badge>
+ );
+ })}
+ {tags.length > 3 && (
+ <Badge variant="outline" className="text-xs">
+ +{tags.length - 3}
+ </Badge>
+ )}
+ {onTagsUpdated && (
+ <Tooltip>
+ <TagEditor
+ siteId={siteId}
+ currentTags={tags}
+ allTags={allTags}
+ onTagsUpdated={onTagsUpdated}
+ trigger={
+ <TooltipTrigger asChild>
+ <button className="p-1 rounded hover:bg-neutral-100 dark:hover:bg-neutral-800 transition-colors">
+ <Tag className="h-4 w-4 text-neutral-400" />
+ </button>
+ </TooltipTrigger>
+ }
+ />
+ <TooltipContent>Edit Tags</TooltipContent>
+ </Tooltip>
+ )}
</div>
</div>
<div className="flex flex-col sm:flex-row gap-2 sm:gap-4 items-start sm:items-center justify-between">
diff --git a/client/src/components/TagEditor.tsx b/client/src/components/TagEditor.tsx
new file mode 100644
index 000000000..9477787cc
--- /dev/null
+++ b/client/src/components/TagEditor.tsx
@@ -0,0 +1,162 @@
+"use client";
+
+import { Plus, X } from "lucide-react";
+import { ReactNode, useState } from "react";
+import { updateSiteConfig } from "../api/admin/endpoints/sites";
+import { Badge } from "./ui/badge";
+import { Button } from "./ui/button";
+import {
+ Dialog,
+ DialogContent,
+ DialogDescription,
+ DialogFooter,
+ DialogHeader,
+ DialogTitle,
+ DialogTrigger,
+} from "./ui/dialog";
+import { Input } from "./ui/input";
+
+interface TagEditorProps {
+ siteId: number;
+ currentTags: string[];
+ allTags: string[];
+ onTagsUpdated: () => void;
+ trigger?: ReactNode;
+}
+
+export function TagEditor({ siteId, currentTags, allTags, onTagsUpdated, trigger }: TagEditorProps) {
+ const [open, setOpen] = useState(false);
+ const [tags, setTags] = useState<string[]>(currentTags);
+ const [newTagInput, setNewTagInput] = useState("");
+ const [isSaving, setIsSaving] = useState(false);
+
+ // Get suggestions - existing tags that aren't already selected
+ const suggestions = allTags.filter(tag => !tags.includes(tag));
+
+ const handleAddTag = (tag: string) => {
+ const trimmedTag = tag.trim();
+ if (trimmedTag && !tags.includes(trimmedTag) && trimmedTag.length <= 50) {
+ setTags([...tags, trimmedTag]);
+ }
+ setNewTagInput("");
+ };
+
+ const handleRemoveTag = (tagToRemove: string) => {
+ setTags(tags.filter(tag => tag !== tagToRemove));
+ };
+
+ const handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
+ if (e.key === "Enter") {
+ e.preventDefault();
+ handleAddTag(newTagInput);
+ }
+ };
+
+ const handleSave = async () => {
+ setIsSaving(true);
+ try {
+ await updateSiteConfig(siteId, { tags });
+ onTagsUpdated();
+ setOpen(false);
+ } catch (error) {
+ console.error("Failed to update tags:", error);
+ } finally {
+ setIsSaving(false);
+ }
+ };
+
+ const handleOpenChange = (isOpen: boolean) => {
+ setOpen(isOpen);
+ if (isOpen) {
+ // Reset to current tags when opening
+ setTags(currentTags);
+ setNewTagInput("");
+ }
+ };
+
+ return (
+ <Dialog open={open} onOpenChange={handleOpenChange}>
+ <DialogTrigger asChild>
+ {trigger || (
+ <Button variant="ghost" size="sm" className="h-6 w-6 p-0">
+ <Plus className="h-3 w-3" />
+ </Button>
+ )}
+ </DialogTrigger>
+ <DialogContent className="sm:max-w-[425px]">
+ <DialogHeader>
+ <DialogTitle>Edit Tags</DialogTitle>
+ <DialogDescription>Add tags to organize and filter your sites.</DialogDescription>
+ </DialogHeader>
+
+ <div className="space-y-4 py-4">
+ {/* Current tags */}
+ <div className="flex flex-wrap gap-2 min-h-[32px]">
+ {tags.length === 0 ? (
+ <span className="text-sm text-neutral-500">No tags added yet</span>
+ ) : (
+ tags.map(tag => (
+ <Badge key={tag} variant="secondary" className="gap-1">
+ {tag}
+ <button
+ onClick={() => handleRemoveTag(tag)}
+ className="ml-1 rounded-full hover:bg-neutral-200 dark:hover:bg-neutral-700"
+ >
+ <X className="h-3 w-3" />
+ </button>
+ </Badge>
+ ))
+ )}
+ </div>
+
+ {/* New tag input */}
+ <div className="flex gap-2">
+ <Input
+ placeholder="Type a new tag..."
+ value={newTagInput}
+ onChange={e => setNewTagInput(e.target.value)}
+ onKeyDown={handleKeyDown}
+ maxLength={50}
+ />
+ <Button
+ variant="outline"
+ onClick={() => handleAddTag(newTagInput)}
+ disabled={!newTagInput.trim()}
+ >
+ Add
+ </Button>
+ </div>
+
+ {/* Suggestions from existing tags */}
+ {suggestions.length > 0 && (
+ <div className="space-y-2">
+ <span className="text-sm text-neutral-500">Existing tags:</span>
+ <div className="flex flex-wrap gap-2">
+ {suggestions.slice(0, 10).map(tag => (
+ <Badge
+ key={tag}
+ variant="outline"
+ className="cursor-pointer hover:bg-neutral-100 dark:hover:bg-neutral-800"
+ onClick={() => handleAddTag(tag)}
+ >
+ <Plus className="h-3 w-3 mr-1" />
+ {tag}
+ </Badge>
+ ))}
+ </div>
+ </div>
+ )}
+ </div>
+
+ <DialogFooter>
+ <Button variant="outline" onClick={() => setOpen(false)}>
+ Cancel
+ </Button>
+ <Button onClick={handleSave} disabled={isSaving} variant="success">
+ {isSaving ? "Saving..." : "Save"}
+ </Button>
+ </DialogFooter>
+ </DialogContent>
+ </Dialog>
+ );
+}
diff --git a/client/src/components/ui/multi-select.tsx b/client/src/components/ui/multi-select.tsx
index 46aa3a0ed..0721aa2f0 100644
--- a/client/src/components/ui/multi-select.tsx
+++ b/client/src/components/ui/multi-select.tsx
@@ -65,7 +65,7 @@ export function MultiSelect({
role="combobox"
aria-expanded={open}
disabled={disabled}
- className={cn("w-full justify-between min-h-10 h-auto", selected.length > 0 && "px-3 py-2", className)}
+ className={cn("w-full justify-between min-h-9 h-auto py-1.5", selected.length > 0 && "px-3", className)}
>
<div className="flex gap-1 flex-wrap">
{selected.length > 0 ? (
diff --git a/server/src/api/sites/updateSiteConfig.ts b/server/src/api/sites/updateSiteConfig.ts
index 1061d21ff..b2fda5c4a 100644
--- a/server/src/api/sites/updateSiteConfig.ts
+++ b/server/src/api/sites/updateSiteConfig.ts
@@ -31,6 +31,9 @@ const updateSiteConfigSchema = z.object({
.max(250)
.optional(),
+ // Tags
+ tags: z.array(z.string().trim().min(1).max(50)).max(20).optional(),
+
// Analytics features
sessionReplay: z.boolean().optional(),
webVitals: z.boolean().optional(),
@@ -109,6 +112,7 @@ export async function updateSiteConfig(
"domain",
"excludedIPs",
"excludedCountries",
+ "tags",
"sessionReplay",
"webVitals",
"trackErrors",
diff --git a/server/src/db/postgres/schema.ts b/server/src/db/postgres/schema.ts
index ff3f0a64b..8ead33ed3 100644
--- a/server/src/db/postgres/schema.ts
+++ b/server/src/db/postgres/schema.ts
@@ -82,6 +82,7 @@ export const sites = pgTable("sites", {
trackIp: boolean().default(false),
apiKey: text("api_key"), // Format: rb_{64_hex_chars} = 67 chars total
privateLinkKey: text("private_link_key"),
+ tags: jsonb("tags").default([]).$type<string[]>(),
});
// Active sessions table
diff --git a/server/src/lib/siteConfig.ts b/server/src/lib/siteConfig.ts
index 17307b929..4fab05dd5 100644
--- a/server/src/lib/siteConfig.ts
+++ b/server/src/lib/siteConfig.ts
@@ -23,6 +23,7 @@ export interface SiteConfigData {
trackInitialPageView: boolean;
trackSpaNavigation: boolean;
trackIp: boolean;
+ tags: string[];
}
class SiteConfig {
@@ -72,6 +73,7 @@ class SiteConfig {
trackInitialPageView: sites.trackInitialPageView,
trackSpaNavigation: sites.trackSpaNavigation,
trackIp: sites.trackIp,
+ tags: sites.tags,
})
.from(sites)
.where(isNumeric ? eq(sites.siteId, Number(siteIdOrId)) : eq(sites.id, String(siteIdOrId)))
@@ -99,6 +101,7 @@ class SiteConfig {
trackInitialPageView: site.trackInitialPageView ?? true,
trackSpaNavigation: site.trackSpaNavigation ?? true,
trackIp: site.trackIp || false,
+ tags: Array.isArray(site.tags) ? site.tags : [],
};
this.cache.set(cacheKey, {