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.

Add support for `top_base_urls`

Steven Tey967af6a571b2
diff --git a/apps/web/lib/analytics/constants.ts b/apps/web/lib/analytics/constants.ts
index 017f205afc..1f061b8f50 100644
--- a/apps/web/lib/analytics/constants.ts
+++ b/apps/web/lib/analytics/constants.ts
@@ -80,6 +80,7 @@ export const VALID_ANALYTICS_ENDPOINTS = [
"top_domains",
"top_links",
"top_urls",
+ "top_base_urls",
"top_partners",
"top_groups",
"utm_sources",
@@ -111,6 +112,7 @@ export const SINGULAR_ANALYTICS_ENDPOINTS = {
top_domains: "domain",
top_links: "link",
top_urls: "url",
+ top_base_urls: "url",
top_groups: "groupId",
timeseries: "start",
};
diff --git a/apps/web/lib/zod/schemas/analytics-response.ts b/apps/web/lib/zod/schemas/analytics-response.ts
index 3b2aae6271..f8d342a4eb 100644
--- a/apps/web/lib/zod/schemas/analytics-response.ts
+++ b/apps/web/lib/zod/schemas/analytics-response.ts
@@ -337,7 +337,9 @@ export const analyticsResponse = {
top_urls: z
.object({
- url: z.string().describe("The destination URL"),
+ url: z
+ .string()
+ .describe("The full destination URL (including query parameters)"),
clicks: z
.number()
.describe("The number of clicks from this URL")
@@ -357,6 +359,30 @@ export const analyticsResponse = {
})
.openapi({ ref: "AnalyticsTopUrls" }),
+ top_base_urls: z
+ .object({
+ url: z
+ .string()
+ .describe("The base URL (destination URL without query parameters)"),
+ clicks: z
+ .number()
+ .describe("The number of clicks from this base URL")
+ .default(0),
+ leads: z
+ .number()
+ .describe("The number of leads from this base URL")
+ .default(0),
+ sales: z
+ .number()
+ .describe("The number of sales from this base URL")
+ .default(0),
+ saleAmount: z
+ .number()
+ .describe("The total amount of sales from this base URL, in cents")
+ .default(0),
+ })
+ .openapi({ ref: "AnalyticsTopBaseUrls" }),
+
utm_sources: z
.object({
utm_source: z.string().describe("The UTM source"),
diff --git a/apps/web/ui/analytics/referer.tsx b/apps/web/ui/analytics/referer.tsx
index d5f4ac1521..534a72c103 100644
--- a/apps/web/ui/analytics/referer.tsx
+++ b/apps/web/ui/analytics/referer.tsx
@@ -11,51 +11,75 @@ import { AnalyticsContext } from "./analytics-provider";
import BarList from "./bar-list";
import { useAnalyticsFilterOption } from "./utils";
+type TabId = "referers" | "utms";
+type RefererSubtab = "referers" | "referer_urls";
+type Subtab = UTM_TAGS_PLURAL | RefererSubtab;
+
+const TAB_CONFIG: Record<
+ TabId,
+ {
+ subtabs: Subtab[];
+ defaultSubtab: Subtab;
+ getSubtabLabel: (subtab: Subtab) => string;
+ }
+> = {
+ referers: {
+ subtabs: ["referers", "referer_urls"],
+ defaultSubtab: "referers",
+ getSubtabLabel: (subtab) => (subtab === "referers" ? "Domain" : "URL"),
+ },
+ utms: {
+ subtabs: [...UTM_TAGS_PLURAL_LIST] as Subtab[],
+ defaultSubtab: "utm_sources" as Subtab,
+ getSubtabLabel: (subtab) =>
+ SINGULAR_ANALYTICS_ENDPOINTS[subtab as UTM_TAGS_PLURAL].replace(
+ "utm_",
+ "",
+ ),
+ },
+};
+
export default function Referer() {
const { queryParams, searchParams } = useRouterStuff();
const { selectedTab, saleUnit } = useContext(AnalyticsContext);
const dataKey = selectedTab === "sales" ? saleUnit : "count";
- const [tab, setTab] = useState<"referers" | "utms">("referers");
- const [utmTag, setUtmTag] = useState<UTM_TAGS_PLURAL>("utm_sources");
- const [refererType, setRefererType] = useState<"referers" | "referer_urls">(
- "referers",
- );
+ const [tab, setTab] = useState<TabId>("referers");
+ const [subtab, setSubtab] = useState<Subtab>(TAB_CONFIG[tab].defaultSubtab);
+
+ // Reset subtab when tab changes to ensure it's valid for the new tab
+ const handleTabChange = (newTab: TabId) => {
+ setTab(newTab);
+ setSubtab(TAB_CONFIG[newTab].defaultSubtab);
+ };
const { data } = useAnalyticsFilterOption({
- groupBy: tab === "utms" ? utmTag : refererType,
+ groupBy: subtab,
});
- const singularTabName =
- SINGULAR_ANALYTICS_ENDPOINTS[tab === "utms" ? utmTag : refererType];
+ const singularTabName = SINGULAR_ANALYTICS_ENDPOINTS[subtab];
- const { icon: UTMTagIcon } = UTM_PARAMETERS.find(
- (p) => p.key === utmTag.slice(0, -1),
- )!;
+ const UTMTagIcon = useMemo(() => {
+ if (tab === "utms") {
+ return UTM_PARAMETERS.find(
+ (p) => p.key === (subtab as UTM_TAGS_PLURAL).slice(0, -1),
+ )?.icon;
+ }
+ return null;
+ }, [tab, subtab]);
const subTabProps = useMemo(() => {
- return (
- {
- utms: {
- subTabs: UTM_TAGS_PLURAL_LIST.map((u) => ({
- id: u,
- label: SINGULAR_ANALYTICS_ENDPOINTS[u].replace("utm_", ""),
- })),
- selectedSubTabId: utmTag,
- onSelectSubTab: setUtmTag,
- },
- referers: {
- subTabs: [
- { id: "referers", label: "Domain" },
- { id: "referer_urls", label: "URL" },
- ],
- selectedSubTabId: refererType,
- onSelectSubTab: setRefererType,
- },
- }[tab] ?? {}
- );
- }, [tab, utmTag, refererType]);
+ const config = TAB_CONFIG[tab];
+ return {
+ subTabs: config.subtabs.map((s) => ({
+ id: s,
+ label: config.getSubtabLabel(s),
+ })),
+ selectedSubTabId: subtab,
+ onSelectSubTab: setSubtab,
+ };
+ }, [tab, subtab]);
return (
<AnalyticsCard
@@ -64,7 +88,7 @@ export default function Referer() {
{ id: "utms", label: "UTM Parameters", icon: Note },
]}
selectedTabId={tab}
- onSelectTab={setTab}
+ onSelectTab={handleTabChange}
{...subTabProps}
expandLimit={8}
hasMore={(data?.length ?? 0) > 8}
@@ -77,38 +101,44 @@ export default function Referer() {
tab={tab === "referers" ? "Referrer" : "UTM Parameter"}
data={
data
- ?.map((d) => ({
- icon:
- tab === "utms" ? (
- <UTMTagIcon />
- ) : d[singularTabName] === "(direct)" ? (
- <Link2 className="h-4 w-4" />
- ) : (
- <BlurImage
- src={`${GOOGLE_FAVICON_URL}${
- tab === "referers"
- ? d[singularTabName]
- : getApexDomain(d[singularTabName])
- }`}
- alt={d[singularTabName]}
- width={20}
- height={20}
- className="h-4 w-4 rounded-full"
- />
- ),
- title: d[singularTabName],
- href: queryParams({
- ...(searchParams.has(singularTabName)
- ? { del: singularTabName }
- : {
- set: {
- [singularTabName]: d[singularTabName],
- },
- }),
- getNewPath: true,
- }) as string,
- value: d[dataKey] || 0,
- }))
+ ?.map((d) => {
+ const isUtmTab = tab === "utms";
+ const isDirect = d[singularTabName] === "(direct)";
+ const isRefererUrl = subtab === "referer_urls";
+
+ return {
+ icon:
+ isUtmTab && UTMTagIcon ? (
+ <UTMTagIcon />
+ ) : isDirect ? (
+ <Link2 className="h-4 w-4" />
+ ) : (
+ <BlurImage
+ src={`${GOOGLE_FAVICON_URL}${
+ isRefererUrl
+ ? getApexDomain(d[singularTabName])
+ : d[singularTabName]
+ }`}
+ alt={d[singularTabName]}
+ width={20}
+ height={20}
+ className="h-4 w-4 rounded-full"
+ />
+ ),
+ title: d[singularTabName],
+ href: queryParams({
+ ...(searchParams.has(singularTabName)
+ ? { del: singularTabName }
+ : {
+ set: {
+ [singularTabName]: d[singularTabName],
+ },
+ }),
+ getNewPath: true,
+ }) as string,
+ value: d[dataKey] || 0,
+ };
+ })
?.sort((a, b) => b.value - a.value) || []
}
unit={selectedTab}
diff --git a/apps/web/ui/analytics/top-links.tsx b/apps/web/ui/analytics/top-links.tsx
index 80c2df500b..b4351f5ef0 100644
--- a/apps/web/ui/analytics/top-links.tsx
+++ b/apps/web/ui/analytics/top-links.tsx
@@ -1,14 +1,58 @@
+import { AnalyticsGroupByOptions } from "@/lib/analytics/types";
import { useWorkspacePreferences } from "@/lib/swr/use-workspace-preferences";
import { LinkLogo, useRouterStuff } from "@dub/ui";
import { Globe, Hyperlink } from "@dub/ui/icons";
import { getApexDomain } from "@dub/utils";
-import { useCallback, useContext, useState } from "react";
+import { useCallback, useContext, useMemo, useState } from "react";
+import { FolderIcon } from "../folders/folder-icon";
+import TagBadge from "../links/tag-badge";
import { AnalyticsCard } from "./analytics-card";
import { AnalyticsLoadingSpinner } from "./analytics-loading-spinner";
import { AnalyticsContext } from "./analytics-provider";
import BarList from "./bar-list";
import { useAnalyticsFilterOption } from "./utils";
+type TabId = "links" | "urls";
+type LinksSubtab = "links" | "folders" | "tags";
+type UrlsSubtab = "full_urls" | "base_urls";
+type Subtab = LinksSubtab | UrlsSubtab;
+
+const TAB_CONFIG: Record<
+ TabId,
+ {
+ subtabs: Subtab[];
+ defaultSubtab: Subtab;
+ getSubtabLabel: (subtab: Subtab) => string;
+ getGroupBy: (subtab: Subtab) => {
+ groupBy: AnalyticsGroupByOptions;
+ };
+ }
+> = {
+ links: {
+ subtabs: ["links", "folders", "tags"],
+ defaultSubtab: "links",
+ getSubtabLabel: (subtab) => {
+ if (subtab === "links") return "Links";
+ if (subtab === "folders") return "Folders";
+ return "Tags";
+ },
+ getGroupBy: (subtab) => {
+ if (subtab === "links") return { groupBy: "top_links" };
+ if (subtab === "folders") return { groupBy: "top_folders" };
+ return { groupBy: "top_link_tags" };
+ },
+ },
+ urls: {
+ subtabs: ["full_urls", "base_urls"],
+ defaultSubtab: "full_urls",
+ getSubtabLabel: (subtab) =>
+ subtab === "full_urls" ? "Full URLs" : "Base URLs",
+ getGroupBy: (subtab) => ({
+ groupBy: subtab === "full_urls" ? "top_urls" : "top_base_urls",
+ }),
+ },
+};
+
export default function TopLinks({
filterLinks = true,
}: {
@@ -19,19 +63,39 @@ export default function TopLinks({
const { selectedTab, saleUnit } = useContext(AnalyticsContext);
const dataKey = selectedTab === "sales" ? saleUnit : "count";
- const [tab, setTab] = useState<"links" | "urls">("links");
- const { data } = useAnalyticsFilterOption({
- groupBy: `top_${tab}`,
- });
+ const [tab, setTab] = useState<TabId>("links");
+ const [subtab, setSubtab] = useState<Subtab>(TAB_CONFIG[tab].defaultSubtab);
+
+ // Reset subtab when tab changes to ensure it's valid for the new tab
+ const handleTabChange = (newTab: TabId) => {
+ setTab(newTab);
+ setSubtab(TAB_CONFIG[newTab].defaultSubtab);
+ };
+
+ const groupByParams = useMemo(
+ () => TAB_CONFIG[tab].getGroupBy(subtab),
+ [tab, subtab],
+ );
+
+ const { data } = useAnalyticsFilterOption(groupByParams);
const [persisted] = useWorkspacePreferences("linksDisplay");
- const shortLinkTitle = useCallback(
- (d: { url?: string; title?: string; shortLink?: string }) => {
+ const getItemTitle = useCallback(
+ (d: Record<string, any>) => {
if (tab === "urls") {
return d.url || "Unknown";
}
+ // For links tab with different subtabs
+ if (subtab === "folders") {
+ return d.folder?.name || "Unknown";
+ }
+ if (subtab === "tags") {
+ return d.tag?.name || "Unknown";
+ }
+
+ // For links subtab
const displayProperties = persisted?.displayProperties;
if (displayProperties?.includes("title") && d.title) {
@@ -40,9 +104,21 @@ export default function TopLinks({
return d.shortLink || "Unknown";
},
- [persisted, tab],
+ [persisted, tab, subtab],
);
+ const subTabProps = useMemo(() => {
+ const config = TAB_CONFIG[tab];
+ return {
+ subTabs: config.subtabs.map((s) => ({
+ id: s,
+ label: config.getSubtabLabel(s),
+ })),
+ selectedSubTabId: subtab,
+ onSelectSubTab: setSubtab,
+ };
+ }, [tab, subtab]);
+
return (
<AnalyticsCard
tabs={[
@@ -52,7 +128,8 @@ export default function TopLinks({
expandLimit={8}
hasMore={(data?.length ?? 0) > 8}
selectedTabId={tab}
- onSelectTab={setTab}
+ onSelectTab={handleTabChange}
+ {...subTabProps}
>
{({ limit, setShowModal }) =>
data ? (
@@ -61,43 +138,104 @@ export default function TopLinks({
tab={tab}
data={
data
- ?.map((d) => ({
- icon: (
- <LinkLogo
- apexDomain={getApexDomain(d.url)}
- className="size-5 sm:size-5"
- />
- ),
- title: shortLinkTitle(d as any),
- // TODO: simplify this once we switch from domain+key to linkId
- href: filterLinks
- ? (queryParams({
- ...((tab === "links" &&
- searchParams.has("domain") &&
- searchParams.has("key")) ||
- (tab === "urls" && searchParams.has("url"))
- ? {
- del:
- tab === "links" ? ["domain", "key"] : "url",
- }
+ ?.map((d) => {
+ const isLinksTab = tab === "links";
+ const isUrlsTab = tab === "urls";
+ const isFoldersSubtab = isLinksTab && subtab === "folders";
+ const isTagsSubtab = isLinksTab && subtab === "tags";
+ const isLinksSubtab = isLinksTab && subtab === "links";
+
+ // Determine icon
+ let icon;
+ if (isFoldersSubtab) {
+ icon = d.folder ? (
+ <FolderIcon
+ folder={d.folder}
+ shape="square"
+ iconClassName="size-3"
+ />
+ ) : null;
+ } else if (isTagsSubtab) {
+ icon = d.tag ? (
+ <TagBadge
+ color={d.tag.color}
+ withIcon
+ className="sm:p-1"
+ />
+ ) : null;
+ } else {
+ icon = (
+ <LinkLogo
+ apexDomain={getApexDomain(d.url || "")}
+ className="size-5 sm:size-5"
+ />
+ );
+ }
+
+ // Determine href
+ let href: string | undefined;
+ if (filterLinks) {
+ if (isLinksSubtab) {
+ const hasLinkFilter =
+ searchParams.has("domain") && searchParams.has("key");
+ href = queryParams({
+ ...(hasLinkFilter
+ ? { del: ["domain", "key"] }
+ : {
+ set: {
+ domain: d.domain,
+ key: d.key || "_root",
+ },
+ }),
+ getNewPath: true,
+ }) as string;
+ } else if (isUrlsTab) {
+ const hasUrlFilter = searchParams.has("url");
+ href = queryParams({
+ ...(hasUrlFilter
+ ? { del: "url" }
: {
set: {
- ...(tab === "links"
- ? {
- domain: d.domain,
- key: d.key || "_root",
- }
- : {
- url: d.url,
- }),
+ url: d.url,
},
}),
getNewPath: true,
- }) as string)
- : undefined,
- value: d[dataKey] || 0,
- ...(tab === "links" && { linkData: d }),
- }))
+ }) as string;
+ } else if (isFoldersSubtab) {
+ const hasFolderFilter = searchParams.has("folderId");
+ href = queryParams({
+ ...(hasFolderFilter
+ ? { del: "folderId" }
+ : {
+ set: {
+ folderId: d.folderId,
+ },
+ }),
+ getNewPath: true,
+ }) as string;
+ } else if (isTagsSubtab) {
+ const hasTagFilter = searchParams.has("tagIds");
+ href = queryParams({
+ ...(hasTagFilter
+ ? { del: "tagIds" }
+ : {
+ set: {
+ tagIds: d.tagId,
+ },
+ }),
+ getNewPath: true,
+ }) as string;
+ }
+ }
+
+ return {
+ icon,
+ title: getItemTitle(d),
+ href,
+ value: d[dataKey] || 0,
+ ...(isLinksSubtab && { linkData: d }),
+ };
+ })
?.sort((a, b) => b.value - a.value) || []
}
unit={selectedTab}
diff --git a/apps/web/ui/analytics/utils.ts b/apps/web/ui/analytics/utils.ts
index f7de747c21..cb2e5e8794 100644
--- a/apps/web/ui/analytics/utils.ts
+++ b/apps/web/ui/analytics/utils.ts
@@ -40,12 +40,21 @@ export function useAnalyticsFilterOption(
? groupByOrParams
: groupByOrParams?.groupBy;
+ // Extract additional params (like root) from the params object
+ const additionalParams =
+ typeof groupByOrParams === "object" && groupByOrParams !== null
+ ? Object.fromEntries(
+ Object.entries(groupByOrParams).filter(([key]) => key !== "groupBy"),
+ )
+ : {};
+
const { data, isLoading } = useSWR<Record<string, any>[]>(
!options?.disabled &&
`${baseApiPath}?${editQueryString(
queryString,
{
...(groupBy && { groupBy }),
+ ...additionalParams,
},
// if theres no groupBy or we're not omitting the groupBy filter, skip
// else, we need to remove the filter for that groupBy param
diff --git a/packages/tinybird/pipes/v3_group_by.pipe b/packages/tinybird/pipes/v3_group_by.pipe
index 3a294c2821..cccf1744b0 100644
--- a/packages/tinybird/pipes/v3_group_by.pipe
+++ b/packages/tinybird/pipes/v3_group_by.pipe
@@ -47,6 +47,8 @@ SQL >
link_id,
{{ String(groupBy) }} = 'top_urls',
url,
+ {{ String(groupBy) }} = 'top_base_urls',
+ splitByString('?', url)[1],
{{ String(groupBy) }} = 'referers',
referer,
{{ String(groupBy) }} = 'referer_urls',
@@ -155,6 +157,8 @@ SQL >
link_id,
{{ String(groupBy) }} = 'top_urls',
url,
+ {{ String(groupBy) }} = 'top_base_urls',
+ splitByString('?', url)[1],
{{ String(groupBy) }} = 'referers',
referer,
{{ String(groupBy) }} = 'referer_urls',
@@ -275,6 +279,8 @@ SQL >
link_id,
{{ String(groupBy) }} = 'top_urls',
url,
+ {{ String(groupBy) }} = 'top_base_urls',
+ splitByString('?', url)[1],
{{ String(groupBy) }} = 'referers',
referer,
{{ String(groupBy) }} = 'referer_urls',