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.

getTotalEvents

Steven Teyc57ffa4c08a5
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 7292084978..bdac492367 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
@@ -1,14 +1,10 @@
import { getReferralLink } from "@/lib/actions/get-referral-link";
+import { getTotalEvents } from "@/lib/actions/get-total-events";
import { EventType } from "@/lib/analytics/types";
import { dub } from "@/lib/dub";
import { EventListSkeleton } from "@dub/blocks";
import { Wordmark } from "@dub/ui";
import { Check } from "@dub/ui/src/icons";
-import {
- ClicksCount,
- LeadsCount,
- SalesCount,
-} from "dub/dist/commonjs/models/components";
import { Suspense } from "react";
import { ActivityList } from "./activity-list";
import { EventTabs } from "./event-tabs";
@@ -135,10 +131,7 @@ async function ActivityListRSC({
page,
});
- const totalEvents = (await dub.analytics.retrieve({
- ...eventsParams,
- interval: "all_unfiltered",
- })) as ClicksCount | LeadsCount | SalesCount;
+ const totalEvents = await getTotalEvents(link.id);
return (
<ActivityList events={events as any} totalEvents={totalEvents[event]} />
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 d39f99d1f4..847236240f 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,11 +1,10 @@
import { getReferralLink } from "@/lib/actions/get-referral-link";
+import { getTotalEvents } from "@/lib/actions/get-total-events";
import { dub } from "@/lib/dub";
import { StatCard, StatCardSkeleton } from "@dub/blocks";
import { CountingNumbers } from "@dub/ui";
import {
- ClicksCount,
ClicksTimeseries,
- SalesCount,
SalesTimeseries,
} from "dub/dist/commonjs/models/components";
import { Suspense } from "react";
@@ -53,14 +52,7 @@ async function StatsInner({ slug }: { slug: string }) {
}
async function loadData(linkId: string) {
- const [totalClicks, clicks, totalSales, sales] = await Promise.all([
- // Total clicks
- dub.analytics.retrieve({
- linkId,
- event: "clicks",
- interval: "all_unfiltered",
- }) as Promise<ClicksCount>,
-
+ const [clicks, sales, totalEvents] = await Promise.all([
// Clicks timeseries
dub.analytics.retrieve({
linkId,
@@ -69,13 +61,6 @@ async function loadData(linkId: string) {
groupBy: "timeseries",
}) as Promise<ClicksTimeseries[]>,
- // Total sales
- dub.analytics.retrieve({
- linkId,
- event: "sales",
- interval: "all_unfiltered",
- }) as Promise<SalesCount>,
-
// Sales timeseries
dub.analytics.retrieve({
linkId,
@@ -83,15 +68,18 @@ async function loadData(linkId: string) {
interval: "30d",
groupBy: "timeseries",
}) as Promise<SalesTimeseries[]>,
+
+ // Total events
+ getTotalEvents(linkId),
]);
return {
- totalClicks: totalClicks.clicks,
+ totalClicks: totalEvents.clicks,
clicks: clicks.map((d) => ({
date: new Date(d.start),
value: d.clicks,
})),
- totalSales: totalSales.amount,
+ totalSales: totalEvents.amount,
sales: sales.map((d) => ({
date: new Date(d.start),
value: d.amount,
diff --git a/apps/web/lib/actions/get-total-events.ts b/apps/web/lib/actions/get-total-events.ts
new file mode 100644
index 0000000000..90bad021a3
--- /dev/null
+++ b/apps/web/lib/actions/get-total-events.ts
@@ -0,0 +1,18 @@
+import { unstable_cache } from "next/cache";
+
+export const getTotalEvents = unstable_cache(async (linkId: string) => {
+ // return await dub.analytics.retrieve({
+ // linkId,
+ // event: "composite",
+ // interval: "all_unfiltered",
+ // })
+
+ return await fetch(
+ `https://api.dub.co/analytics?event=composite&groupBy=count&interval=all_unfiltered&linkId=${linkId}`,
+ {
+ headers: {
+ Authorization: `Bearer ${process.env.DUB_API_KEY}`,
+ },
+ },
+ ).then((res) => res.json());
+});