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.

update analytics types

Steven Teydc77222584cf
diff --git a/apps/web/lib/analytics/types.ts b/apps/web/lib/analytics/types.ts
index 6e2f327a51..8aa8dd9fdd 100644
--- a/apps/web/lib/analytics/types.ts
+++ b/apps/web/lib/analytics/types.ts
@@ -1,12 +1,16 @@
import z from "../zod";
import { analyticsQuerySchema } from "../zod/schemas/analytics";
-import { EVENT_TYPES, VALID_ANALYTICS_ENDPOINTS, intervals } from "./constants";
+import { VALID_ANALYTICS_ENDPOINTS, intervals } from "./constants";
-export type IntervalProps = (typeof intervals)[number];
-export type AnalyticsEvents = (typeof EVENT_TYPES)[number];
-export type AnalyticsEndpoints = (typeof VALID_ANALYTICS_ENDPOINTS)[number];
+export type IntervalOptions = (typeof intervals)[number];
+export type AnalyticsGroupByOptions =
+ (typeof VALID_ANALYTICS_ENDPOINTS)[number];
+export type CompositeAnalyticsResponseOptions =
+ | "clicks"
+ | "leads"
+ | "sales"
+ | "amount";
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/analytics-area-chart.tsx b/apps/web/ui/analytics/analytics-area-chart.tsx
index 479d606490..904a86ab23 100644
--- a/apps/web/ui/analytics/analytics-area-chart.tsx
+++ b/apps/web/ui/analytics/analytics-area-chart.tsx
@@ -14,14 +14,21 @@ export default function AnalyticsAreaChart({
}: {
show: ("clicks" | "leads" | "sales")[];
}) {
- const { baseApiPath, queryString, start, end, interval, requiresUpgrade } =
- useContext(AnalyticsContext);
+ const {
+ selectedTab,
+ baseApiPath,
+ queryString,
+ start,
+ end,
+ interval,
+ requiresUpgrade,
+ } = useContext(AnalyticsContext);
const { data } = useSWR<
{ start: Date; clicks: number; leads: number; sales: number }[]
>(
`${baseApiPath}?${editQueryString(queryString, {
- event: "composite",
+ event: selectedTab,
groupBy: "timeseries",
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
})}`,
diff --git a/apps/web/ui/analytics/index.tsx b/apps/web/ui/analytics/index.tsx
index 32f1dbe3f1..33204f72aa 100644
--- a/apps/web/ui/analytics/index.tsx
+++ b/apps/web/ui/analytics/index.tsx
@@ -6,7 +6,7 @@
*/
import { VALID_ANALYTICS_FILTERS } from "@/lib/analytics/constants";
-import { AnalyticsEvents } from "@/lib/analytics/types";
+import { CompositeAnalyticsResponseOptions } from "@/lib/analytics/types";
import useWorkspace from "@/lib/swr/use-workspace";
import { fetcher } from "@dub/utils";
import { endOfDay, startOfDay, subDays } from "date-fns";
@@ -35,7 +35,7 @@ export const AnalyticsContext = createContext<{
end?: Date;
interval?: string;
tagId?: string;
- totalEvents?: { [key in AnalyticsEvents]: number };
+ totalEvents?: { [key in CompositeAnalyticsResponseOptions]: number };
admin?: boolean;
demo?: boolean;
requiresUpgrade?: boolean;
@@ -156,30 +156,28 @@ export default function Analytics({
// Reset requiresUpgrade when query changes
useEffect(() => setRequiresUpgrade(false), [queryString]);
- const { data: totalEvents } = useSWR<{ [key in AnalyticsEvents]: number }>(
- `${baseApiPath}?${queryString}`,
- fetcher,
- {
- onSuccess: () => setRequiresUpgrade(false),
- onError: (error) => {
- if (error.status === 403) {
- toast.custom(() => (
- <UpgradeRequiredToast
- title="Upgrade for more analytics"
- message={JSON.parse(error.message)?.error.message}
- />
- ));
- setRequiresUpgrade(true);
- } else {
- toast.error(error.message);
- }
- },
- onErrorRetry: (error, ...args) => {
- if (error.message.includes("Upgrade to Pro")) return;
- defaultConfig.onErrorRetry(error, ...args);
- },
+ const { data: totalEvents } = useSWR<{
+ [key in CompositeAnalyticsResponseOptions]: number;
+ }>(`${baseApiPath}?${queryString}`, fetcher, {
+ onSuccess: () => setRequiresUpgrade(false),
+ onError: (error) => {
+ if (error.status === 403) {
+ toast.custom(() => (
+ <UpgradeRequiredToast
+ title="Upgrade for more analytics"
+ message={JSON.parse(error.message)?.error.message}
+ />
+ ));
+ setRequiresUpgrade(true);
+ } else {
+ toast.error(error.message);
+ }
},
- );
+ onErrorRetry: (error, ...args) => {
+ if (error.message.includes("Upgrade to Pro")) return;
+ defaultConfig.onErrorRetry(error, ...args);
+ },
+ });
const isPublicStatsPage = basePath.startsWith("/stats");
diff --git a/apps/web/ui/analytics/main.tsx b/apps/web/ui/analytics/main.tsx
index fb035d1f65..a737e3ff80 100644
--- a/apps/web/ui/analytics/main.tsx
+++ b/apps/web/ui/analytics/main.tsx
@@ -1,13 +1,10 @@
-import { editQueryString } from "@/lib/analytics/utils";
-import useTags from "@/lib/swr/use-tags";
import useWorkspace from "@/lib/swr/use-workspace";
import { CountingNumbers, NumberTooltip, useRouterStuff } from "@dub/ui";
-import { cn, fetcher } from "@dub/utils";
+import { cn } from "@dub/utils";
import { ChevronRight, Lock, X } from "lucide-react";
import Link from "next/link";
import { useSearchParams } from "next/navigation";
import { useContext, useMemo } from "react";
-import useSWR from "swr";
import { AnalyticsContext } from ".";
import AnalyticsAreaChart from "./analytics-area-chart";
@@ -20,17 +17,11 @@ type Tab = {
export default function Main() {
const { betaTester } = useWorkspace();
- const { baseApiPath, queryString, requiresUpgrade, demo, selectedTab } =
+ const { totalEvents, requiresUpgrade, demo, selectedTab } =
useContext(AnalyticsContext);
const searchParams = useSearchParams();
- const domain = searchParams.get("domain");
- const key = searchParams.get("key");
const { queryParams } = useRouterStuff();
- // Tag related
- const tagId = searchParams.get("tagId");
- const { tags } = useTags();
-
// Root domain related
const root = searchParams.get("root");
@@ -68,30 +59,14 @@ export default function Main() {
show: ["clicks", "leads", "sales"],
};
- const { data } = useSWR<{
- clicks: number;
- leads: number;
- sales: number;
- amount: number;
- }>(
- `${baseApiPath}?${editQueryString(queryString, {
- event: "composite",
- groupBy: "count",
- })}`,
- fetcher,
- {
- shouldRetryOnError: !requiresUpgrade,
- },
- );
-
return (
<div className="w-full overflow-hidden border border-gray-200 bg-white sm:rounded-xl">
<div className="scrollbar-hide mb-5 flex w-full divide-x overflow-y-hidden overflow-x-scroll border-b border-gray-200">
{tabs.map(({ id, label, colorClassName }, idx) => {
const total = {
- clicks: data?.clicks,
- leads: data?.leads,
- sales: data ? data.amount / 100 : undefined,
+ clicks: totalEvents?.clicks,
+ leads: totalEvents?.leads,
+ sales: totalEvents ? totalEvents.amount / 100 : undefined,
}[id];
return (
@@ -145,7 +120,7 @@ export default function Main() {
<div className="mt-1 flex">
{total || total === 0 ? (
<NumberTooltip
- value={id === "sales" ? data?.sales : total}
+ value={id === "sales" ? totalEvents?.sales : total}
unit={label.toLowerCase()}
>
<CountingNumbers
diff --git a/apps/web/ui/analytics/referer.tsx b/apps/web/ui/analytics/referer.tsx
index f0d58aa9c0..43c7474ba5 100644
--- a/apps/web/ui/analytics/referer.tsx
+++ b/apps/web/ui/analytics/referer.tsx
@@ -1,4 +1,4 @@
-import { AnalyticsEvents } from "@/lib/analytics/types";
+import { CompositeAnalyticsResponseOptions } 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";
@@ -15,7 +15,7 @@ export default function Referer() {
const { data } = useSWR<
({ referer: string } & {
- [key in AnalyticsEvents]: number;
+ [key in CompositeAnalyticsResponseOptions]: number;
})[]
>(
`${baseApiPath}?${editQueryString(queryString, {
diff --git a/apps/web/ui/analytics/top-links.tsx b/apps/web/ui/analytics/top-links.tsx
index 98d000f9aa..f5a411349c 100644
--- a/apps/web/ui/analytics/top-links.tsx
+++ b/apps/web/ui/analytics/top-links.tsx
@@ -1,4 +1,7 @@
-import { AnalyticsEvents, TopLinksTabs } from "@/lib/analytics/types";
+import {
+ CompositeAnalyticsResponseOptions,
+ TopLinksTabs,
+} from "@/lib/analytics/types";
import { editQueryString } from "@/lib/analytics/utils";
import { Modal, TabSelect, useRouterStuff } from "@dub/ui";
import { fetcher, getApexDomain, linkConstructor, truncate } from "@dub/utils";
@@ -36,7 +39,7 @@ export default function TopLinks() {
const { data } = useSWR<
({ domain: string; key: string } & {
[key in TopLinksTabs]: string;
- } & { [key in AnalyticsEvents]: number })[]
+ } & { [key in CompositeAnalyticsResponseOptions]: number })[]
>(
`${baseApiPath}?${editQueryString(queryString, {
groupBy: `top_${tab}s`,
diff --git a/apps/web/ui/analytics/utils.ts b/apps/web/ui/analytics/utils.ts
index f12898ec01..7bc58329cb 100644
--- a/apps/web/ui/analytics/utils.ts
+++ b/apps/web/ui/analytics/utils.ts
@@ -1,4 +1,4 @@
-import { AnalyticsEndpoints } from "@/lib/analytics/types";
+import { AnalyticsGroupByOptions } from "@/lib/analytics/types";
import { editQueryString } from "@/lib/analytics/utils";
import { fetcher } from "@dub/utils";
import { useContext } from "react";
@@ -6,7 +6,7 @@ import useSWR from "swr";
import { AnalyticsContext } from ".";
export function useAnalyticsFilterOption(
- groupBy: AnalyticsEndpoints,
+ groupBy: AnalyticsGroupByOptions,
): ({ count?: number } & Record<string, any>) | null {
const { baseApiPath, queryString, selectedTab, requiresUpgrade } =
useContext(AnalyticsContext);
diff --git a/apps/web/ui/links/link-card.tsx b/apps/web/ui/links/link-card.tsx
index 6c943e604e..a87c6990b3 100644
--- a/apps/web/ui/links/link-card.tsx
+++ b/apps/web/ui/links/link-card.tsx
@@ -1,4 +1,4 @@
-import { AnalyticsEvents } from "@/lib/analytics/types";
+import { CompositeAnalyticsResponseOptions } from "@/lib/analytics/types";
import useDomains from "@/lib/swr/use-domains";
import useWorkspace from "@/lib/swr/use-workspace";
import { LinkWithTagsProps, TagProps, UserProps } from "@/lib/types";
@@ -118,7 +118,9 @@ export default function LinkCard({
const entry = useIntersectionObserver(linkRef, {});
const isVisible = !!entry?.isIntersecting;
- const { data: totalEvents } = useSWR<{ [key in AnalyticsEvents]?: number }>(
+ const { data: totalEvents } = useSWR<{
+ [key in CompositeAnalyticsResponseOptions]?: number;
+ }>(
// only fetch data if the link is visible and there's a slug and the usage is not exceeded
isVisible &&
workspaceId &&