Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

added more filters

Steven Tey583219dbd6b5
diff --git a/apps/web/app/api/edge/stats/[endpoint]/route.ts b/apps/web/app/api/edge/stats/[endpoint]/route.ts
index 191c610ed6..0e2b5dba56 100644
--- a/apps/web/app/api/edge/stats/[endpoint]/route.ts
+++ b/apps/web/app/api/edge/stats/[endpoint]/route.ts
@@ -15,7 +15,8 @@ export const GET = async (
{ params }: { params: Record<string, string> },
) => {
const { endpoint } = params;
- const { domain, key, interval } = getSearchParams(req.url);
+ const searchParams = getSearchParams(req.url);
+ const { domain, key, interval } = searchParams;
// demo link (dub.sh/github)
if (domain === "dub.sh" && key === "github") {
@@ -55,6 +56,7 @@ export const GET = async (
key,
endpoint,
interval,
+ ...searchParams,
});
return NextResponse.json(response);
diff --git a/apps/web/app/api/stats/[endpoint]/route.ts b/apps/web/app/api/stats/[endpoint]/route.ts
index 3af2fa1c15..2f14a87af7 100644
--- a/apps/web/app/api/stats/[endpoint]/route.ts
+++ b/apps/web/app/api/stats/[endpoint]/route.ts
@@ -14,6 +14,7 @@ export const GET = withAuth(
key,
endpoint,
interval,
+ ...searchParams,
});
return NextResponse.json(response);
},
diff --git a/apps/web/lib/stats.ts b/apps/web/lib/stats.ts
index 7e4b56c578..e5a9f6fc94 100644
--- a/apps/web/lib/stats.ts
+++ b/apps/web/lib/stats.ts
@@ -61,7 +61,7 @@ export type LocationTabs = "country" | "city" | "region";
export type DeviceTabs = "device" | "browser" | "os" | "ua";
-const VALID_TINYBIRD_ENDPOINTS = new Set([
+const VALID_TINYBIRD_ENDPOINTS = [
"timeseries",
"clicks",
"top_links",
@@ -71,25 +71,37 @@ const VALID_TINYBIRD_ENDPOINTS = new Set([
"browser",
"os",
"referer",
-]);
+];
+
+export const VALID_STATS_FILTERS = [
+ "country",
+ "city",
+ "device",
+ "browser",
+ "os",
+ "referer",
+];
export const getStats = async ({
domain,
key,
endpoint,
interval,
+ ...rest
}: {
domain: string;
key: string;
endpoint: string;
- interval?: string | null;
+ interval?: string;
+} & {
+ [key in typeof VALID_STATS_FILTERS[number]]: string;
}) => {
// Note: we're using decodeURIComponent in this function because that's how we store it in MySQL and Tinybird
if (
!conn ||
!process.env.TINYBIRD_API_KEY ||
- !VALID_TINYBIRD_ENDPOINTS.has(endpoint)
+ !VALID_TINYBIRD_ENDPOINTS.includes(endpoint)
) {
return [];
}
@@ -123,7 +135,6 @@ export const getStats = async ({
if (key) {
url.searchParams.append("key", decodeURIComponent(key));
}
-
if (interval) {
url.searchParams.append(
"start",
@@ -140,6 +151,12 @@ export const getStats = async ({
url.searchParams.append("granularity", intervalData[interval].granularity);
}
+ VALID_STATS_FILTERS.forEach((filter) => {
+ if (rest[filter]) {
+ url.searchParams.append(filter, rest[filter]);
+ }
+ });
+
return await fetch(url.toString(), {
headers: {
Authorization: `Bearer ${process.env.TINYBIRD_API_KEY}`,
diff --git a/apps/web/ui/stats/clicks.tsx b/apps/web/ui/stats/clicks.tsx
index 59fe41cd11..d49091f60e 100644
--- a/apps/web/ui/stats/clicks.tsx
+++ b/apps/web/ui/stats/clicks.tsx
@@ -1,11 +1,12 @@
import { Chart } from "@/ui/shared/icons";
import { NumberTooltip, useRouterStuff } from "@dub/ui";
-import { linkConstructor, nFormatter } from "@dub/utils";
+import { COUNTRIES, capitalize, linkConstructor, nFormatter } from "@dub/utils";
import { useContext } from "react";
import { StatsContext } from ".";
import BarChart from "./bar-chart";
import { useSearchParams, useParams } from "next/navigation";
import { X } from "lucide-react";
+import { VALID_STATS_FILTERS } from "@/lib/stats";
export default function Clicks() {
const { totalClicks } = useContext(StatsContext);
@@ -17,8 +18,8 @@ export default function Clicks() {
return (
<div className="max-w-4xl border border-gray-200 bg-white p-5 sm:rounded-lg sm:border-gray-100 sm:p-10 sm:shadow-lg">
- <div className="mb-5 flex items-start justify-between">
- <div>
+ <div className="mb-5 flex items-start justify-between space-x-4">
+ <div className="flex-none">
<div className="flex items-end space-x-1">
{totalClicks || totalClicks === 0 ? (
<NumberTooltip value={totalClicks}>
@@ -35,37 +36,59 @@ export default function Clicks() {
Total Clicks
</p>
</div>
- {slug &&
- domain &&
- (key ? (
- <button
- onClick={() => {
- editQueryParam({
- del: ["domain", "key"],
- });
- }}
- className="flex items-center space-x-1 rounded-md bg-gray-50 px-2 py-1 text-sm text-gray-500 transition-all duration-75 hover:bg-gray-100 active:scale-[0.98] sm:px-3"
- >
- <p>Link</p>
- <strong className="text-gray-800">
- {linkConstructor({ domain, key, pretty: true })}
- </strong>
- <X className="h-4 w-4" />
- </button>
- ) : (
- <button
- onClick={() => {
- editQueryParam({
- del: "domain",
- });
- }}
- className="flex items-center space-x-1 rounded-md bg-gray-50 px-2 py-1 text-sm text-gray-500 transition-all duration-75 hover:bg-gray-100 active:scale-[0.98] sm:px-3"
- >
- <p>Domain</p>
- <strong className="text-gray-800">{domain}</strong>
- <X className="h-4 w-4" />
- </button>
- ))}
+ <div className="flex flex-wrap items-center justify-end gap-2">
+ {slug &&
+ domain &&
+ (key ? (
+ <button
+ onClick={() => {
+ editQueryParam({
+ del: ["domain", "key"],
+ });
+ }}
+ className="flex items-center space-x-1 rounded-md bg-gray-50 px-2 py-1 text-sm text-gray-500 transition-all duration-75 hover:bg-gray-100 active:scale-[0.98] sm:px-3"
+ >
+ <p>Link</p>
+ <strong className="text-gray-800">
+ {linkConstructor({ domain, key, pretty: true })}
+ </strong>
+ <X className="h-4 w-4" />
+ </button>
+ ) : (
+ <button
+ onClick={() => {
+ editQueryParam({
+ del: "domain",
+ });
+ }}
+ className="flex items-center space-x-1 rounded-md bg-gray-50 px-2 py-1 text-sm text-gray-500 transition-all duration-75 hover:bg-gray-100 active:scale-[0.98] sm:px-3"
+ >
+ <p>Domain</p>
+ <strong className="text-gray-800">{domain}</strong>
+ <X className="h-4 w-4" />
+ </button>
+ ))}
+ {VALID_STATS_FILTERS.map((filter) => {
+ const value = searchParams?.get(filter);
+ if (!value) return null;
+ return (
+ <button
+ onClick={() => {
+ editQueryParam({
+ del: filter,
+ });
+ }}
+ className="flex items-center space-x-1 rounded-md bg-gray-50 px-2 py-1 text-sm text-gray-500 transition-all duration-75 hover:bg-gray-100 active:scale-[0.98] sm:px-3"
+ >
+ <p>{capitalize(filter)}</p>
+ <strong className="text-gray-800">
+ {filter === "country" ? COUNTRIES[value] : value}
+ </strong>
+ <X className="h-4 w-4" />
+ </button>
+ );
+ })}
+ </div>
</div>
<BarChart />
</div>
diff --git a/apps/web/ui/stats/devices.tsx b/apps/web/ui/stats/devices.tsx
index 986a8bff6b..897f985caa 100644
--- a/apps/web/ui/stats/devices.tsx
+++ b/apps/web/ui/stats/devices.tsx
@@ -1,5 +1,5 @@
import { DeviceTabs } from "@/lib/stats";
-import { LoadingSpinner, Modal, TabSelect } from "@dub/ui";
+import { LoadingSpinner, Modal, TabSelect, useRouterStuff } from "@dub/ui";
import { fetcher } from "@dub/utils";
import { Maximize } from "lucide-react";
import { useContext, useState } from "react";
@@ -19,6 +19,7 @@ export default function Devices() {
} & { clicks: number })[]
>(`${baseApiPath}/${tab}?${queryString}`, fetcher);
+ const { editQueryParam } = useRouterStuff();
const [showModal, setShowModal] = useState(false);
const barList = (limit?: number) => (
@@ -28,6 +29,12 @@ export default function Devices() {
data?.map((d) => ({
icon: <DeviceIcon display={d[tab]} tab={tab} className="h-4 w-4" />,
title: d[tab],
+ href: editQueryParam({
+ set: {
+ [tab]: d[tab],
+ },
+ getNewPath: true,
+ }),
clicks: d.clicks,
})) || []
}
diff --git a/apps/web/ui/stats/index.tsx b/apps/web/ui/stats/index.tsx
index 35c3132719..070026bf02 100644
--- a/apps/web/ui/stats/index.tsx
+++ b/apps/web/ui/stats/index.tsx
@@ -62,6 +62,12 @@ export default function Stats({
const domainSlug = searchParams?.get("domain");
// key can be a path param (public stats pages) or a query param (stats pages in app)
key = searchParams?.get("key") || key;
+ const country = searchParams?.get("country");
+ const city = searchParams?.get("city");
+ const device = searchParams?.get("device");
+ const browser = searchParams?.get("browser");
+ const os = searchParams?.get("os");
+ const referer = searchParams?.get("referer");
const interval = searchParams?.get("interval") || "24h";
const { basePath, domain, baseApiPath } = useMemo(() => {
@@ -93,9 +99,15 @@ export default function Stats({
return new URLSearchParams({
...(domain && { domain }),
...(key && { key }),
+ ...(country && { country }),
+ ...(city && { city }),
+ ...(device && { device }),
+ ...(browser && { browser }),
+ ...(os && { os }),
+ ...(referer && { referer }),
...(interval && { interval }),
}).toString();
- }, [slug, domain, key, interval]);
+ }, [domain, key, country, city, device, browser, os, referer, interval]);
const { data: totalClicks } = useSWR<number>(
`${baseApiPath}/clicks?${queryString}`,
diff --git a/apps/web/ui/stats/locations.tsx b/apps/web/ui/stats/locations.tsx
index 5fa96d3c42..7c01786dc9 100644
--- a/apps/web/ui/stats/locations.tsx
+++ b/apps/web/ui/stats/locations.tsx
@@ -1,5 +1,5 @@
import { LocationTabs } from "@/lib/stats";
-import { LoadingSpinner, Modal, TabSelect } from "@dub/ui";
+import { LoadingSpinner, Modal, TabSelect, useRouterStuff } from "@dub/ui";
import { COUNTRIES, fetcher } from "@dub/utils";
import { Maximize } from "lucide-react";
import { useContext, useState } from "react";
@@ -10,14 +10,14 @@ import BarList from "./bar-list";
export default function Locations() {
const [tab, setTab] = useState<LocationTabs>("country");
- const { baseApiPath, queryString, totalClicks, modal } =
- useContext(StatsContext);
+ const { baseApiPath, queryString, modal } = useContext(StatsContext);
const { data } = useSWR<{ country: string; city: string; clicks: number }[]>(
`${baseApiPath}/${tab}?${queryString}`,
fetcher,
);
+ const { editQueryParam } = useRouterStuff();
const [showModal, setShowModal] = useState(false);
const barList = (limit?: number) => (
@@ -33,6 +33,12 @@ export default function Locations() {
/>
),
title: tab === "country" ? COUNTRIES[d.country] : d.city,
+ href: editQueryParam({
+ set: {
+ [tab]: d[tab],
+ },
+ getNewPath: true,
+ }),
clicks: d.clicks,
})) || []
}
diff --git a/apps/web/ui/stats/referer.tsx b/apps/web/ui/stats/referer.tsx
index d38d8dfcbd..c0a0917927 100644
--- a/apps/web/ui/stats/referer.tsx
+++ b/apps/web/ui/stats/referer.tsx
@@ -1,5 +1,5 @@
import { BlurImage } from "@/ui/shared/blur-image";
-import { LoadingSpinner, Modal } from "@dub/ui";
+import { LoadingSpinner, Modal, useRouterStuff } from "@dub/ui";
import { GOOGLE_FAVICON_URL, fetcher } from "@dub/utils";
import { Link2, Maximize } from "lucide-react";
import { useContext, useState } from "react";
@@ -16,6 +16,7 @@ export default function Referer() {
fetcher,
);
+ const { editQueryParam } = useRouterStuff();
const [showModal, setShowModal] = useState(false);
const barList = (limit?: number) => (
@@ -36,6 +37,12 @@ export default function Referer() {
/>
),
title: d.referer,
+ href: editQueryParam({
+ set: {
+ referer: d.referer,
+ },
+ getNewPath: true,
+ }),
clicks: d.clicks,
})) || []
}
diff --git a/apps/web/ui/stats/top-links.tsx b/apps/web/ui/stats/top-links.tsx
index 7c1c4c4e1c..4967dfc161 100644
--- a/apps/web/ui/stats/top-links.tsx
+++ b/apps/web/ui/stats/top-links.tsx
@@ -1,4 +1,4 @@
-import { LoadingSpinner, Modal } from "@dub/ui";
+import { LoadingSpinner, Modal, useRouterStuff } from "@dub/ui";
import { fetcher, linkConstructor } from "@dub/utils";
import { Maximize } from "lucide-react";
import { useContext, useState } from "react";
@@ -15,6 +15,7 @@ export default function TopLinks() {
fetcher,
);
+ const { editQueryParam } = useRouterStuff();
const [showModal, setShowModal] = useState(false);
const barList = (limit?: number) => (
@@ -27,11 +28,13 @@ export default function TopLinks() {
key: d.key,
pretty: true,
}),
- href: `${basePath}?${new URLSearchParams({
- domain: d.domain,
- key: d.key,
- interval,
- }).toString()}`,
+ href: editQueryParam({
+ set: {
+ domain: d.domain,
+ key: d.key,
+ },
+ getNewPath: true,
+ }),
clicks: d.clicks,
})) || []
}
diff --git a/packages/ui/src/hooks/use-router-stuff.ts b/packages/ui/src/hooks/use-router-stuff.ts
index c854a3867a..ae398847e9 100644
--- a/packages/ui/src/hooks/use-router-stuff.ts
+++ b/packages/ui/src/hooks/use-router-stuff.ts
@@ -10,10 +10,12 @@ export default function useRouterStuff() {
set,
del,
replace,
+ getNewPath,
}: {
set?: Record<string, string>;
del?: string | string[];
replace?: boolean;
+ getNewPath?: boolean;
}) => {
const newParams = new URLSearchParams(searchParams);
if (set) {
@@ -30,6 +32,7 @@ export default function useRouterStuff() {
const newPath = `${pathname}${
queryString.length > 0 ? `?${queryString}` : ""
}`;
+ if (getNewPath) return newPath;
if (replace) {
router.replace(newPath);
} else {