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.

display stats

Kiran K9dcc14ee2eb2
diff --git a/apps/web/app/api/analytics/client/route.ts b/apps/web/app/api/analytics/client/route.ts
index 3335215dd6..357d4f4a86 100644
--- a/apps/web/app/api/analytics/client/route.ts
+++ b/apps/web/app/api/analytics/client/route.ts
@@ -9,8 +9,8 @@ export const GET = withAuth(async ({ workspace, link, searchParams }) => {
const response = await getAnalytics({
...parsedParams,
- linkId: link.id,
workspaceId: workspace.id,
+ linkId: link.id,
});
return NextResponse.json(response);
diff --git a/apps/web/app/api/analytics/route.ts b/apps/web/app/api/analytics/route.ts
index 6c7672c8c3..012c7014c2 100644
--- a/apps/web/app/api/analytics/route.ts
+++ b/apps/web/app/api/analytics/route.ts
@@ -1,7 +1,6 @@
import { VALID_ANALYTICS_ENDPOINTS } from "@/lib/analytics/constants";
import { getAnalytics } from "@/lib/analytics/get-analytics";
import { validDateRangeForPlan } from "@/lib/analytics/utils";
-import { getDomainOrThrow } from "@/lib/api/domains/get-domain-or-throw";
import { getLinkOrThrow } from "@/lib/api/links/get-link-or-throw";
import { throwIfClicksUsageExceeded } from "@/lib/api/links/usage-checks";
import { withWorkspace } from "@/lib/auth";
diff --git a/apps/web/app/api/events/route.ts b/apps/web/app/api/events/route.ts
index 807b184587..5f1e38fb5c 100644
--- a/apps/web/app/api/events/route.ts
+++ b/apps/web/app/api/events/route.ts
@@ -1,6 +1,5 @@
import { getEvents } from "@/lib/analytics/get-events";
import { validDateRangeForPlan } from "@/lib/analytics/utils";
-import { getDomainOrThrow } from "@/lib/api/domains/get-domain-or-throw";
import { getLinkOrThrow } from "@/lib/api/links/get-link-or-throw";
import { throwIfClicksUsageExceeded } from "@/lib/api/links/usage-checks";
import { withWorkspace } from "@/lib/auth";
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/referrals/page.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/referrals/page.tsx
index 74dbd0eaaa..6f8534e57e 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/referrals/page.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/referrals/page.tsx
@@ -13,6 +13,7 @@ import { Events } from "./events";
import { HeroBackground } from "./hero-background";
import ReferralsPageClient from "./page-client";
import ReferralLink, { ReferralLinkSkeleton } from "./referral-link";
+import { Stats } from "./stats";
export const revalidate = 0;
@@ -88,9 +89,10 @@ export default function ReferralsPage({
</a>
</div>
- {/* <div className="mt-8">
+ {/* Stats */}
+ <div className="mt-8">
<Stats />
- </div> */}
+ </div>
{/* Events */}
<div className="mt-12">
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/referrals/stats.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/referrals/stats.tsx
index 01ff1387e6..50f919fc35 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/referrals/stats.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/referrals/stats.tsx
@@ -1,7 +1,5 @@
"use client";
-import { getTotalEvents } from "@/lib/actions/get-total-events";
-import { dub } from "@/lib/dub";
import {
REFERRAL_CLICKS_QUOTA_BONUS,
REFERRAL_CLICKS_QUOTA_BONUS_MAX,
@@ -16,33 +14,65 @@ import {
} from "@dub/blocks";
import { CountingNumbers } from "@dub/ui";
import { User } from "@dub/ui/src/icons";
-import { AnalyticsTimeseries } from "dub/dist/commonjs/models/components";
+import {
+ AnalyticsCount,
+ AnalyticsTimeseries,
+} from "dub/dist/commonjs/models/components";
-export function Stats() {
- const { analytics, isLoading } = useAnalytics();
+interface StatsInnerProps {
+ totalEvents: AnalyticsCount;
+ sales: AnalyticsTimeseries[];
+}
- return <StatsInner />;
+export const Stats = () => {
+ const { analytics: sales, isLoading: isLoadingSales } = useAnalytics({
+ event: "sales",
+ interval: "90d",
+ groupBy: "timeseries",
+ });
- // return (
- // <div className="grid grid-cols-1 gap-x-4 gap-y-4 md:grid-cols-2 lg:gap-x-6">
- // {isLoading ? (
- // [...Array(2)].map(() => <StatsCardSkeleton />)
- // ) : (
- // <StatsInner />
- // )}
- // </div>
- // );
-}
+ const { analytics: totalEvents, isLoading: isLoadingTotalEvents } =
+ useAnalytics({
+ event: "composite",
+ interval: "all_unfiltered",
+ groupBy: "count",
+ });
-const StatsInner = () => {
- const { totalSales, sales, referredSignups, clicksQuotaBonus } =
- await loadData(link.id);
+ const loading = isLoadingSales || isLoadingTotalEvents;
+
+ return (
+ <div className="grid grid-cols-1 gap-x-4 gap-y-4 md:grid-cols-2 lg:gap-x-6">
+ {loading ? (
+ [...Array(2)].map(() => <StatsCardSkeleton />)
+ ) : (
+ <StatsInner
+ totalEvents={totalEvents as AnalyticsCount}
+ sales={sales as AnalyticsTimeseries[]}
+ />
+ )}
+ </div>
+ );
+};
+
+const StatsInner = ({ totalEvents, sales }: StatsInnerProps) => {
+ const totalLeads = Math.min(totalEvents.leads ?? 0, 32);
+ const totalSales = totalEvents.saleAmount ?? 0;
+
+ const clicksQuotaBonus = Math.min(
+ totalLeads * REFERRAL_CLICKS_QUOTA_BONUS,
+ REFERRAL_CLICKS_QUOTA_BONUS_MAX,
+ );
+
+ const salesData = sales.map((sale) => ({
+ date: new Date(sale.start),
+ value: sale.saleAmount ?? 0,
+ }));
return (
<>
<StatsCard
label="Affiliate Earnings"
- graphic={<MiniAreaChart data={sales} />}
+ graphic={<MiniAreaChart data={salesData} />}
>
<CountingNumbers prefix="$" variant="full">
{(totalSales / 100) * REFERRAL_REVENUE_SHARE}
@@ -55,7 +85,7 @@ const StatsInner = () => {
<Gauge value={clicksQuotaBonus} max={REFERRAL_CLICKS_QUOTA_BONUS_MAX}>
<div className="flex items-end gap-1 text-xs font-medium text-gray-500">
<User className="size-4" />
- <CountingNumbers>{referredSignups}</CountingNumbers>
+ <CountingNumbers>{totalLeads}</CountingNumbers>
</div>
</Gauge>
}
@@ -64,47 +94,4 @@ const StatsInner = () => {
</StatsCard>
</>
);
-
- return [...Array(2)].map(() => <StatsCardSkeleton error />);
};
-
-async function loadData(linkId: string) {
- const [clicks, sales, totalEvents] = await Promise.all([
- // Clicks timeseries
- dub.analytics.retrieve({
- linkId,
- event: "clicks",
- interval: "30d",
- groupBy: "timeseries",
- }) as Promise<AnalyticsTimeseries[]>,
-
- // Sales timeseries
- dub.analytics.retrieve({
- linkId,
- event: "sales",
- interval: "30d",
- groupBy: "timeseries",
- }) as Promise<AnalyticsTimeseries[]>,
-
- // Total events
- getTotalEvents(linkId),
- ]);
-
- return {
- totalClicks: totalEvents.clicks,
- clicks: clicks.map((d) => ({
- date: new Date(d.start),
- value: d.clicks,
- })),
- totalSales: totalEvents.saleAmount ?? 0,
- sales: sales.map((d) => ({
- date: new Date(d.start),
- value: d.saleAmount ?? 0,
- })),
- referredSignups: Math.min(totalEvents.leads ?? 0, 32),
- clicksQuotaBonus: Math.min(
- (totalEvents.leads ?? 0) * REFERRAL_CLICKS_QUOTA_BONUS,
- REFERRAL_CLICKS_QUOTA_BONUS_MAX,
- ),
- };
-}
diff --git a/apps/web/lib/analytics/get-analytics.ts b/apps/web/lib/analytics/get-analytics.ts
index c2e4600731..0357c1da75 100644
--- a/apps/web/lib/analytics/get-analytics.ts
+++ b/apps/web/lib/analytics/get-analytics.ts
@@ -87,8 +87,6 @@ export const getAnalytics = async (params: AnalyticsFilters) => {
timezone,
});
- console.log("getAnalytics", response);
-
if (groupBy === "count") {
// Return the count value for deprecated endpoints
if (isDeprecatedClicksEndpoint) {
diff --git a/packages/blocks/src/hooks/use-analytics.ts b/packages/blocks/src/hooks/use-analytics.ts
index 9bd91503ed..daa44ed860 100644
--- a/packages/blocks/src/hooks/use-analytics.ts
+++ b/packages/blocks/src/hooks/use-analytics.ts
@@ -1,8 +1,28 @@
import { fetcher } from "@dub/utils";
import useSWR from "swr";
+import { EventType } from "../types";
-export const useAnalytics = () => {
- const { error, data, isLoading } = useSWR(`/api/analytics/client`, fetcher);
+interface UseAnalyticsParams {
+ event: EventType;
+ interval: string;
+ groupBy: "timeseries" | "top_links" | "devices" | "count";
+}
+
+export const useAnalytics = ({
+ event,
+ interval,
+ groupBy,
+}: UseAnalyticsParams) => {
+ const searchParams = new URLSearchParams({
+ event,
+ interval,
+ groupBy,
+ });
+
+ const { error, data, isLoading } = useSWR(
+ `/api/analytics/client?${searchParams.toString()}`,
+ fetcher,
+ );
return {
analytics: data,
diff --git a/packages/blocks/src/hooks/use-events.ts b/packages/blocks/src/hooks/use-events.ts
index e33fbbe00e..5ea3bfbd96 100644
--- a/packages/blocks/src/hooks/use-events.ts
+++ b/packages/blocks/src/hooks/use-events.ts
@@ -1,20 +1,19 @@
import { fetcher } from "@dub/utils";
import useSWR from "swr";
+import { EventType } from "../types";
-const EVENT_TYPES = ["clicks", "leads", "sales"] as const;
-
-interface Props {
- event: (typeof EVENT_TYPES)[number];
+interface UseEventsParams {
+ event: EventType;
interval: string;
page: string;
}
-export const useEvents = ({ event, interval, page }: Props) => {
- const searchParams = new URLSearchParams();
-
- searchParams.set("event", event);
- searchParams.set("interval", interval);
- searchParams.set("page", page);
+export const useEvents = ({ event, interval, page }: UseEventsParams) => {
+ const searchParams = new URLSearchParams({
+ event,
+ interval,
+ page,
+ });
const { error, data, isLoading } = useSWR<[]>(
`/api/events/client?${searchParams.toString()}`,
diff --git a/packages/blocks/src/index.ts b/packages/blocks/src/index.ts
index 5137ee0e1f..3a7736b51e 100644
--- a/packages/blocks/src/index.ts
+++ b/packages/blocks/src/index.ts
@@ -8,4 +8,4 @@ export * from "./hooks/use-analytics";
export * from "./hooks/use-events";
export * from "./mini-area-chart";
export * from "./pagination-controls";
-export * from "./stats-card";
\ No newline at end of file
+export * from "./stats-card";
diff --git a/packages/blocks/src/types.ts b/packages/blocks/src/types.ts
new file mode 100644
index 0000000000..510cddc5ce
--- /dev/null
+++ b/packages/blocks/src/types.ts
@@ -0,0 +1 @@
+export type EventType = "clicks" | "leads" | "sales" | "composite";