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.

fix issues with card analytics

Steven Tey01c76d9b8353
diff --git a/apps/web/lib/analytics/constants.ts b/apps/web/lib/analytics/constants.ts
index f8ab974278..91d7fe7b81 100644
--- a/apps/web/lib/analytics/constants.ts
+++ b/apps/web/lib/analytics/constants.ts
@@ -101,6 +101,7 @@ export const SINGULAR_ANALYTICS_ENDPOINTS = {
devices: "device",
browsers: "browser",
referers: "referer",
+ os: "os",
};
export const VALID_ANALYTICS_FILTERS = [
diff --git a/apps/web/lib/analytics/types.ts b/apps/web/lib/analytics/types.ts
index 62453bf5f4..5e2dddd52c 100644
--- a/apps/web/lib/analytics/types.ts
+++ b/apps/web/lib/analytics/types.ts
@@ -1,6 +1,7 @@
import { intervals } from "./constants";
export type IntervalProps = (typeof intervals)[number];
+export type AnalyticsEvents = "clicks" | "leads" | "sales";
export type AnalyticsEndpoints =
| "count"
| "timeseries"
@@ -13,5 +14,6 @@ export type AnalyticsEndpoints =
| "top_links"
| "top_urls";
export type LocationTabs = "countries" | "cities";
+export type LocationTabsSingular = "country" | "city";
export type TopLinksTabs = "link" | "url";
export type DeviceTabs = "devices" | "browsers" | "os";
diff --git a/apps/web/ui/analytics/bar-list.tsx b/apps/web/ui/analytics/bar-list.tsx
index e62da53943..d37d1caf29 100644
--- a/apps/web/ui/analytics/bar-list.tsx
+++ b/apps/web/ui/analytics/bar-list.tsx
@@ -11,6 +11,7 @@ import {
} from "@dub/ui";
import { LinkifyTooltipContent } from "@dub/ui/src/tooltip";
import {
+ capitalize,
cn,
fetcher,
getApexDomain,
@@ -39,7 +40,7 @@ export default function BarList({
tab,
data,
barBackground,
- maxClicks,
+ maxValue,
setShowModal,
limit,
}: {
@@ -48,15 +49,17 @@ export default function BarList({
icon: ReactNode;
title: string;
href: string;
- clicks: number;
+ value: number;
}[];
- maxClicks: number;
+ maxValue: number;
barBackground: string;
setShowModal: Dispatch<SetStateAction<boolean>>;
limit?: number;
}) {
const [search, setSearch] = useState("");
+ const { selectedTab } = useContext(AnalyticsContext);
+
// TODO: mock pagination for better perf in React
// TODO: fix for top links since it's technically link IDs
const filteredData = useMemo(() => {
@@ -79,7 +82,7 @@ export default function BarList({
<LineItem
key={idx}
{...data}
- maxClicks={maxClicks}
+ maxValue={maxValue}
tab={tab}
setShowModal={setShowModal}
barBackground={barBackground}
@@ -111,7 +114,7 @@ export default function BarList({
{tab}
</p>
<p className="text-xs font-semibold uppercase text-gray-600">
- Clicks
+ {capitalize(selectedTab)}
</p>
</div>
<div className="h-[50vh] overflow-auto p-4 md:h-[40vh]">{bars}</div>
@@ -125,8 +128,8 @@ export function LineItem({
icon,
title,
href,
- clicks,
- maxClicks,
+ value,
+ maxValue,
tab,
setShowModal,
barBackground,
@@ -134,8 +137,8 @@ export function LineItem({
icon?: ReactNode;
title: string;
href: string;
- clicks: number;
- maxClicks: number;
+ value: number;
+ maxValue: number;
tab: string;
setShowModal: Dispatch<SetStateAction<boolean>>;
barBackground: string;
@@ -250,7 +253,7 @@ export function LineItem({
)}
<motion.div
style={{
- width: `${(clicks / (maxClicks || 0)) * 100}%`,
+ width: `${(value / (maxValue || 0)) * 100}%`,
}}
className={cn(
"absolute h-full origin-left rounded-sm",
@@ -261,10 +264,8 @@ export function LineItem({
animate={{ transform: "scaleX(1)" }}
/>
</div>
- <NumberTooltip value={clicks}>
- <p className="z-10 px-2 text-sm text-gray-600">
- {nFormatter(clicks)}
- </p>
+ <NumberTooltip value={value}>
+ <p className="z-10 px-2 text-sm text-gray-600">{nFormatter(value)}</p>
</NumberTooltip>
</div>
</Link>
diff --git a/apps/web/ui/analytics/devices.tsx b/apps/web/ui/analytics/devices.tsx
index 11c56f569e..ee6232c52b 100644
--- a/apps/web/ui/analytics/devices.tsx
+++ b/apps/web/ui/analytics/devices.tsx
@@ -1,5 +1,5 @@
import { SINGULAR_ANALYTICS_ENDPOINTS } from "@/lib/analytics/constants";
-import { DeviceTabs } from "@/lib/analytics/types";
+import { AnalyticsEvents, DeviceTabs } from "@/lib/analytics/types";
import { editQueryString } from "@/lib/analytics/utils";
import { Modal, TabSelect, useRouterStuff } from "@dub/ui";
import { fetcher } from "@dub/utils";
@@ -24,7 +24,7 @@ export default function Devices() {
const { data } = useSWR<
({
[key in DeviceTabs]: string;
- } & { clicks: number })[]
+ } & { [key in AnalyticsEvents]: number })[]
>(
`${baseApiPath}?${editQueryString(queryString, {
type: tab,
@@ -57,10 +57,10 @@ export default function Devices() {
},
getNewPath: true,
}) as string,
- clicks: d[selectedTab] ?? d["clicks"],
+ value: d[selectedTab] ?? d["clicks"],
})) || []
}
- maxClicks={(data?.[0]?.[selectedTab] ?? data?.[0]?.["clicks"]) || 0}
+ maxValue={(data?.[0]?.[selectedTab] ?? data?.[0]?.["clicks"]) || 0}
barBackground="bg-green-100"
setShowModal={setShowModal}
{...(limit && { limit })}
diff --git a/apps/web/ui/analytics/locations.tsx b/apps/web/ui/analytics/locations.tsx
index 6f46076d9c..88c88ec90d 100644
--- a/apps/web/ui/analytics/locations.tsx
+++ b/apps/web/ui/analytics/locations.tsx
@@ -1,5 +1,9 @@
import { SINGULAR_ANALYTICS_ENDPOINTS } from "@/lib/analytics/constants";
-import { LocationTabs } from "@/lib/analytics/types";
+import {
+ AnalyticsEvents,
+ LocationTabs,
+ LocationTabsSingular,
+} from "@/lib/analytics/types";
import { editQueryString } from "@/lib/analytics/utils";
import { Modal, TabSelect, useRouterStuff } from "@dub/ui";
import { COUNTRIES, fetcher } from "@dub/utils";
@@ -20,13 +24,18 @@ export default function Locations() {
const { selectedTab, baseApiPath, queryString, requiresUpgrade } =
useContext(AnalyticsContext);
- const { data } = useSWR<{ country: string; city: string; clicks: number }[]>(
+ const { data } = useSWR<
+ ({
+ [key in LocationTabsSingular]: string;
+ } & { [key in AnalyticsEvents]: number })[]
+ >(
`${baseApiPath}?${editQueryString(queryString, {
type: tab,
})}`,
fetcher,
{ shouldRetryOnError: !requiresUpgrade },
);
+ console.log({ data });
const { queryParams } = useRouterStuff();
const [showModal, setShowModal] = useState(false);
@@ -50,10 +59,10 @@ export default function Locations() {
},
getNewPath: true,
}) as string,
- clicks: d[selectedTab] ?? d["clicks"],
+ value: d[selectedTab] ?? d["clicks"],
})) || []
}
- maxClicks={(data?.[0]?.[selectedTab] ?? data?.[0]?.["clicks"]) || 0}
+ maxValue={(data?.[0]?.[selectedTab] ?? data?.[0]?.["clicks"]) || 0}
barBackground="bg-orange-100"
setShowModal={setShowModal}
{...(limit && { limit })}
diff --git a/apps/web/ui/analytics/referer.tsx b/apps/web/ui/analytics/referer.tsx
index 91899ef8ec..40430d31ff 100644
--- a/apps/web/ui/analytics/referer.tsx
+++ b/apps/web/ui/analytics/referer.tsx
@@ -1,3 +1,4 @@
+import { AnalyticsEvents } from "@/lib/analytics/types";
import { editQueryString } from "@/lib/analytics/utils";
import { BlurImage, Modal, useRouterStuff } from "@dub/ui";
import { GOOGLE_FAVICON_URL, fetcher } from "@dub/utils";
@@ -12,7 +13,11 @@ export default function Referer() {
const { selectedTab, baseApiPath, queryString, requiresUpgrade } =
useContext(AnalyticsContext);
- const { data } = useSWR<{ referer: string; clicks: number }[]>(
+ const { data } = useSWR<
+ ({ referer: string } & {
+ [key in AnalyticsEvents]: number;
+ })[]
+ >(
`${baseApiPath}?${editQueryString(queryString, {
type: "referers",
})}`,
@@ -47,10 +52,10 @@ export default function Referer() {
},
getNewPath: true,
}) as string,
- clicks: d[selectedTab] ?? d["clicks"],
+ value: d[selectedTab] ?? d["clicks"],
})) || []
}
- maxClicks={(data?.[0]?.[selectedTab] ?? data?.[0]?.["clicks"]) || 0}
+ maxValue={(data?.[0]?.[selectedTab] ?? data?.[0]?.["clicks"]) || 0}
barBackground="bg-red-100"
setShowModal={setShowModal}
{...(limit && { limit })}
diff --git a/apps/web/ui/analytics/top-links.tsx b/apps/web/ui/analytics/top-links.tsx
index c49cb7fed5..be4086a347 100644
--- a/apps/web/ui/analytics/top-links.tsx
+++ b/apps/web/ui/analytics/top-links.tsx
@@ -1,4 +1,4 @@
-import { TopLinksTabs } from "@/lib/analytics/types";
+import { AnalyticsEvents, TopLinksTabs } from "@/lib/analytics/types";
import { editQueryString } from "@/lib/analytics/utils";
import { Modal, TabSelect, useRouterStuff } from "@dub/ui";
import { fetcher, linkConstructor, truncate } from "@dub/utils";
@@ -35,7 +35,7 @@ export default function TopLinks() {
const { data } = useSWR<
({ domain: string; key: string } & {
[key in TopLinksTabs]: string;
- } & { clicks: number })[]
+ } & { [key in AnalyticsEvents]: number })[]
>(
`${baseApiPath}?${editQueryString(queryString, {
type: `top_${tab}s`,
@@ -64,10 +64,10 @@ export default function TopLinks() {
},
getNewPath: true,
}) as string,
- clicks: d[selectedTab] ?? d["clicks"],
+ value: d[selectedTab] ?? d["clicks"],
})) || []
}
- maxClicks={(data?.[0]?.[selectedTab] ?? data?.[0]?.["clicks"]) || 0}
+ maxValue={(data?.[0]?.[selectedTab] ?? data?.[0]?.["clicks"]) || 0}
barBackground="bg-blue-100"
setShowModal={setShowModal}
{...(limit && { limit })}