Skip to content

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

Enhance analytics and filters with UTM parameters

Bill Yang41c4f63742df
diff --git a/client/src/api/analytics/useGetGoals.ts b/client/src/api/analytics/useGetGoals.ts
index cb7df0596..a8d202463 100644
--- a/client/src/api/analytics/useGetGoals.ts
+++ b/client/src/api/analytics/useGetGoals.ts
@@ -1,7 +1,11 @@
import { useQuery } from "@tanstack/react-query";
import { BACKEND_URL } from "../../lib/const";
+import {
+ getFilteredFilters,
+ GOALS_PAGE_FILTERS,
+ useStore,
+} from "../../lib/store";
import { authedFetch } from "../utils";
-import { useStore, Filter } from "../../lib/store";
export interface Goal {
goalId: number;
@@ -34,7 +38,6 @@ interface GoalsResponse {
export function useGetGoals({
startDate,
endDate,
- filters,
page = 1,
pageSize = 10,
sort = "createdAt",
@@ -43,7 +46,6 @@ export function useGetGoals({
}: {
startDate: string;
endDate: string;
- filters?: Filter[];
page?: number;
pageSize?: number;
sort?: "goalId" | "name" | "goalType" | "createdAt";
@@ -53,6 +55,8 @@ export function useGetGoals({
const { site } = useStore();
const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
+ const filteredFilters = getFilteredFilters(GOALS_PAGE_FILTERS);
+
return useQuery({
queryKey: [
"goals",
@@ -60,7 +64,7 @@ export function useGetGoals({
startDate,
endDate,
timezone,
- filters,
+ filteredFilters,
page,
pageSize,
sort,
@@ -71,7 +75,7 @@ export function useGetGoals({
startDate,
endDate,
timezone,
- filters,
+ filteredFilters,
page,
pageSize,
sort,
diff --git a/client/src/api/analytics/userSessions.ts b/client/src/api/analytics/userSessions.ts
index 9874f1925..a6e736cd2 100644
--- a/client/src/api/analytics/userSessions.ts
+++ b/client/src/api/analytics/userSessions.ts
@@ -49,21 +49,26 @@ export type GetSessionsResponse = {
session_id: string;
user_id: string;
country: string;
- city: string;
region: string;
+ city: string;
language: string;
device_type: string;
browser: string;
operating_system: string;
referrer: string;
channel: string;
+ utm_source: string;
+ utm_medium: string;
+ utm_campaign: string;
+ utm_term: string;
+ utm_content: string;
session_end: string;
session_start: string;
session_duration: number;
- pageviews: number;
- events: number;
entry_page: string;
exit_page: string;
+ pageviews: number;
+ events: number;
}[];
export function useGetSessionsInfinite(userId?: string) {
diff --git a/client/src/app/[site]/components/shared/Filters/const.tsx b/client/src/app/[site]/components/shared/Filters/const.tsx
index 582914698..cb960cd5b 100644
--- a/client/src/app/[site]/components/shared/Filters/const.tsx
+++ b/client/src/app/[site]/components/shared/Filters/const.tsx
@@ -2,19 +2,26 @@ import {
AppWindow,
Brain,
FileText,
+ Flag,
FolderInput,
Globe,
Languages,
+ Link,
LogIn,
LogOut,
+ MapPin,
MapPinHouse,
MapPinned,
Maximize,
+ Megaphone,
MousePointerClick,
+ Puzzle,
Radio,
Search,
Share2,
TabletSmartphone,
+ Tag,
+ Target,
} from "lucide-react";
import React from "react";
import { FilterParameter } from "../../../../../lib/store";
@@ -47,7 +54,7 @@ export const FilterOptions: {
{
label: "Referrer",
value: "referrer",
- icon: <Share2 className="h-4 w-4" />,
+ icon: <Link className="h-4 w-4" />,
},
{
label: "Channel",
@@ -104,6 +111,31 @@ export const FilterOptions: {
value: "dimensions",
icon: <Maximize className="h-4 w-4" />,
},
+ {
+ label: "UTM Source",
+ value: "utm_source",
+ icon: <Target className="h-4 w-4" />,
+ },
+ {
+ label: "UTM Medium",
+ value: "utm_medium",
+ icon: <Megaphone className="h-4 w-4" />,
+ },
+ {
+ label: "UTM Campaign",
+ value: "utm_campaign",
+ icon: <Flag className="h-4 w-4" />,
+ },
+ {
+ label: "UTM Content",
+ value: "utm_content",
+ icon: <Puzzle className="h-4 w-4" />,
+ },
+ {
+ label: "UTM Term",
+ value: "utm_term",
+ icon: <Tag className="h-4 w-4" />,
+ },
];
export const OperatorOptions = [
diff --git a/client/src/app/[site]/components/shared/Filters/utils.ts b/client/src/app/[site]/components/shared/Filters/utils.ts
index 3d998794d..1da2dd6cd 100644
--- a/client/src/app/[site]/components/shared/Filters/utils.ts
+++ b/client/src/app/[site]/components/shared/Filters/utils.ts
@@ -33,6 +33,16 @@ export function getParameterNameLabel(parameter: FilterParameter) {
return "Dimension";
case "event_name":
return "Event Name";
+ case "utm_source":
+ return "UTM Source";
+ case "utm_medium":
+ return "UTM Medium";
+ case "utm_campaign":
+ return "UTM Campaign";
+ case "utm_term":
+ return "UTM Term";
+ case "utm_content":
+ return "UTM Content";
default:
return parameter;
}
diff --git a/client/src/app/[site]/goals/page.tsx b/client/src/app/[site]/goals/page.tsx
index 1d2ffdfaa..4f903ceaa 100644
--- a/client/src/app/[site]/goals/page.tsx
+++ b/client/src/app/[site]/goals/page.tsx
@@ -3,13 +3,13 @@
import { useState } from "react";
import { useGetGoals } from "../../../api/analytics/useGetGoals";
import { getStartAndEndDate } from "../../../api/utils";
-import { SESSION_PAGE_FILTERS, useStore } from "../../../lib/store";
+import { GOALS_PAGE_FILTERS, useStore } from "../../../lib/store";
import { SubHeader } from "../components/SubHeader/SubHeader";
import CreateGoalButton from "./components/CreateGoalButton";
import GoalsList from "./components/GoalsList";
export default function GoalsPage() {
- const { time, filters, site, setTime } = useStore();
+ const { time, site } = useStore();
const { startDate, endDate } = getStartAndEndDate(time);
const [currentPage, setCurrentPage] = useState(1);
const pageSize = 9; // Show 9 cards (3x3 grid)
@@ -22,7 +22,6 @@ export default function GoalsPage() {
const { data: goalsData, isLoading } = useGetGoals({
startDate: queryStartDate,
endDate: queryEndDate,
- filters,
page: currentPage,
pageSize,
});
@@ -79,7 +78,7 @@ export default function GoalsPage() {
return (
<div className="p-2 md:p-4 max-w-[1400px] mx-auto space-y-3">
- <SubHeader availableFilters={SESSION_PAGE_FILTERS} />
+ <SubHeader availableFilters={GOALS_PAGE_FILTERS} />
<div className="flex items-center justify-between">
<div />
<CreateGoalButton siteId={Number(site)} />
diff --git a/client/src/app/[site]/main/components/MainSection/MainSection.tsx b/client/src/app/[site]/main/components/MainSection/MainSection.tsx
index a4199351d..5ffbdada0 100644
--- a/client/src/app/[site]/main/components/MainSection/MainSection.tsx
+++ b/client/src/app/[site]/main/components/MainSection/MainSection.tsx
@@ -7,6 +7,10 @@ import { PreviousChart } from "./PreviousChart";
import { useStore } from "../../../../../lib/store";
import { useGetOverviewBucketed } from "../../../../../api/analytics/useGetOverviewBucketed";
import { useGetOverview } from "../../../../../api/analytics/useGetOverview";
+import { Tilt_Warp } from "next/font/google";
+import Link from "next/link";
+import Image from "next/image";
+import { authClient } from "../../../../../lib/auth";
const SELECTED_STAT_MAP = {
pageviews: "Pageviews",
@@ -17,7 +21,14 @@ const SELECTED_STAT_MAP = {
users: "Users",
};
+const tilt_wrap = Tilt_Warp({
+ subsets: ["latin"],
+ weight: "400",
+});
+
export function MainSection() {
+ const session = authClient.useSession();
+
const { selectedStat, time, site, bucket } = useStore();
const { data, isFetching, error } = useGetOverviewBucketed({ site, bucket });
@@ -49,7 +60,15 @@ export function MainSection() {
{(isFetching || isPreviousFetching) && <CardLoader />}
<CardContent className="p-2 md:p-4 py-3 w-full">
<div className="flex items-center justify-between px-2 md:px-0">
- <span className="w-24" />
+ <div className="flex items-center space-x-4">
+ <Link
+ href={session.data ? "/" : "https://rybbit.io"}
+ className={`text-lg font-semibold flex items-center gap-1.5 ${tilt_wrap.className} opacity-75`}
+ >
+ <Image src="/rybbit.png" alt="Rybbit" width={20} height={20} />
+ rybbit.io
+ </Link>
+ </div>
<span className="text-sm text-neutral-200">
{SELECTED_STAT_MAP[selectedStat]}
</span>
diff --git a/client/src/app/[site]/main/components/sections/Referrers.tsx b/client/src/app/[site]/main/components/sections/Referrers.tsx
index ca690049c..c2e1fe84c 100644
--- a/client/src/app/[site]/main/components/sections/Referrers.tsx
+++ b/client/src/app/[site]/main/components/sections/Referrers.tsx
@@ -10,7 +10,14 @@ import { Card, CardContent } from "../../../../../components/ui/card";
import { StandardSection } from "../../../components/shared/StandardSection/StandardSection";
import { StandardSectionRealtime } from "../../../components/shared/StandardSection/StandardSectionRealtime";
-type Tab = "referrers" | "channels";
+type Tab =
+ | "referrers"
+ | "channels"
+ | "utm_source"
+ | "utm_medium"
+ | "utm_campaign"
+ | "utm_term"
+ | "utm_content";
export function Referrers({ isRealtime = false }: { isRealtime?: boolean }) {
const [tab, setTab] = useState<Tab>("referrers");
@@ -28,6 +35,11 @@ export function Referrers({ isRealtime = false }: { isRealtime?: boolean }) {
<TabsList>
<TabsTrigger value="referrers">Referrers</TabsTrigger>
<TabsTrigger value="channels">Channels</TabsTrigger>
+ <TabsTrigger value="utm_source">Source</TabsTrigger>
+ <TabsTrigger value="utm_medium">Medium</TabsTrigger>
+ <TabsTrigger value="utm_campaign">Campaign</TabsTrigger>
+ <TabsTrigger value="utm_term">Term</TabsTrigger>
+ <TabsTrigger value="utm_content">Content</TabsTrigger>
</TabsList>
<TabsContent value="referrers">
<ComponentToUse
@@ -59,6 +71,51 @@ export function Referrers({ isRealtime = false }: { isRealtime?: boolean }) {
)}
/>
</TabsContent>
+ <TabsContent value="utm_source">
+ <ComponentToUse
+ filterParameter="utm_source"
+ title="UTM Source"
+ getKey={(e) => e.value}
+ getLabel={(e) => e.value}
+ getValue={(e) => e.value}
+ />
+ </TabsContent>
+ <TabsContent value="utm_medium">
+ <ComponentToUse
+ filterParameter="utm_medium"
+ title="UTM Medium"
+ getKey={(e) => e.value}
+ getLabel={(e) => e.value}
+ getValue={(e) => e.value}
+ />
+ </TabsContent>
+ <TabsContent value="utm_campaign">
+ <ComponentToUse
+ filterParameter="utm_campaign"
+ title="UTM Campaign"
+ getKey={(e) => e.value}
+ getLabel={(e) => e.value}
+ getValue={(e) => e.value}
+ />
+ </TabsContent>
+ <TabsContent value="utm_content">
+ <ComponentToUse
+ filterParameter="utm_content"
+ title="UTM Content"
+ getKey={(e) => e.value}
+ getLabel={(e) => e.value}
+ getValue={(e) => e.value}
+ />
+ </TabsContent>
+ <TabsContent value="utm_term">
+ <ComponentToUse
+ filterParameter="utm_term"
+ title="UTM Term"
+ getKey={(e) => e.value}
+ getLabel={(e) => e.value}
+ getValue={(e) => e.value}
+ />
+ </TabsContent>
</Tabs>
</CardContent>
</Card>
diff --git a/client/src/lib/store.ts b/client/src/lib/store.ts
index 469192b90..2d301a722 100644
--- a/client/src/lib/store.ts
+++ b/client/src/lib/store.ts
@@ -37,6 +37,11 @@ export type FilterParameter =
| "querystring"
| "event_name"
| "channel"
+ | "utm_source"
+ | "utm_medium"
+ | "utm_campaign"
+ | "utm_term"
+ | "utm_content"
// derivative parameters
| "entry_page"
| "exit_page"
@@ -55,6 +60,26 @@ export const SESSION_PAGE_FILTERS: FilterParameter[] = [
"channel",
"entry_page",
"exit_page",
+ "utm_source",
+ "utm_medium",
+ "utm_campaign",
+ "utm_term",
+ "utm_content",
+];
+
+export const GOALS_PAGE_FILTERS: FilterParameter[] = [
+ "browser",
+ "operating_system",
+ "language",
+ "country",
+ "region",
+ "city",
+ "device_type",
+ "referrer",
+ "event_name",
+ "channel",
+ "entry_page",
+ "exit_page",
];
export const USER_PAGE_FILTERS: FilterParameter[] = [
diff --git a/docs/src/app/components/Cards/AdvancedFilters.jsx b/docs/src/app/components/Cards/AdvancedFilters.jsx
index 2b5d03746..4817f171c 100644
--- a/docs/src/app/components/Cards/AdvancedFilters.jsx
+++ b/docs/src/app/components/Cards/AdvancedFilters.jsx
@@ -5,6 +5,7 @@ import {
FolderInput,
Globe,
Languages,
+ Link,
LogIn,
LogOut,
MapPinHouse,
@@ -13,8 +14,11 @@ import {
MousePointerClick,
Radio,
Search,
- Share2,
+ Target,
TabletSmartphone,
+ Megaphone,
+ Flag,
+ Puzzle,
} from "lucide-react";
const FilterOptions = [
@@ -41,7 +45,7 @@ const FilterOptions = [
{
label: "Referrer",
value: "referrer",
- icon: <Share2 className="h-4 w-4" />,
+ icon: <Link className="h-4 w-4" />,
},
{
label: "Channel",
@@ -98,14 +102,24 @@ const FilterOptions = [
value: "dimensions",
icon: <Maximize className="h-4 w-4" />,
},
+ {
+ label: "UTM Source",
+ value: "utm_source",
+ icon: <Target className="h-4 w-4" />,
+ },
+ {
+ label: "UTM Medium",
+ value: "utm_medium",
+ icon: <Megaphone className="h-4 w-4" />,
+ },
];
export function AdvancedFilters() {
return (
<div className="bg-neutral-800/50 p-6 rounded-xl border border-neutral-700">
<h3 className="text-xl font-semibold mb-3">Advanced Filters</h3>
- <p className="text-neutral-300">Drill down into your data with advanced filters across over a dozen dimensions.</p>
- <div className="flex flex-wrap justify-center gap-3.5 mt-8">
+ <p className="text-neutral-300">Drill down into your data with advanced filters across over numerous dimensions.</p>
+ <div className="flex flex-wrap justify-center gap-y-3 gap-x-3.5 mt-4">
{FilterOptions.map((option) => (
<div key={option.value} className="flex items-center gap-2 text-base bg-neutral-800 border border-neutral-700 py-1 px-2 rounded-md">
{option.icon}
diff --git a/server/src/api/analytics/getSessions.ts b/server/src/api/analytics/getSessions.ts
index 4e9ca207d..99175c445 100644
--- a/server/src/api/analytics/getSessions.ts
+++ b/server/src/api/analytics/getSessions.ts
@@ -19,6 +19,11 @@ export type GetSessionsResponse = {
operating_system: string;
referrer: string;
channel: string;
+ utm_source: string;
+ utm_medium: string;
+ utm_campaign: string;
+ utm_term: string;
+ utm_content: string;
session_end: string;
session_start: string;
session_duration: number;
@@ -74,6 +79,11 @@ export async function getSessions(
argMax(screen_height, timestamp) AS screen_height,
argMin(referrer, timestamp) AS referrer,
argMin(channel, timestamp) AS channel,
+ argMin(utm_source, timestamp) AS utm_source,
+ argMin(utm_medium, timestamp) AS utm_medium,
+ argMin(utm_campaign, timestamp) AS utm_campaign,
+ argMin(utm_term, timestamp) AS utm_term,
+ argMin(utm_content, timestamp) AS utm_content,
MAX(timestamp) AS session_end,
MIN(timestamp) AS session_start,
dateDiff('second', MIN(timestamp), MAX(timestamp)) AS session_duration,
diff --git a/server/src/api/analytics/query-validation.ts b/server/src/api/analytics/query-validation.ts
index 0828b22dc..8fb008c33 100644
--- a/server/src/api/analytics/query-validation.ts
+++ b/server/src/api/analytics/query-validation.ts
@@ -162,6 +162,11 @@ export const filterParamSchema = z.enum([
"querystring",
"event_name",
"channel",
+ "utm_source",
+ "utm_medium",
+ "utm_campaign",
+ "utm_term",
+ "utm_content",
"entry_page",
"exit_page",
"dimensions",
diff --git a/server/src/api/analytics/types.ts b/server/src/api/analytics/types.ts
index 577e9fae0..9839dc524 100644
--- a/server/src/api/analytics/types.ts
+++ b/server/src/api/analytics/types.ts
@@ -14,6 +14,11 @@ export type FilterParameter =
| "querystring"
| "event_name"
| "channel"
+ | "utm_source"
+ | "utm_medium"
+ | "utm_campaign"
+ | "utm_term"
+ | "utm_content"
// derivative parameters
| "entry_page"
| "exit_page"