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.

totalClicks → totalEvents

Steven Tey77d7ad3d0991
diff --git a/apps/web/lib/analytics/get-analytics.ts b/apps/web/lib/analytics/get-analytics.ts
index d8f237f3c1..3a14d11268 100644
--- a/apps/web/lib/analytics/get-analytics.ts
+++ b/apps/web/lib/analytics/get-analytics.ts
@@ -96,14 +96,14 @@ export const getAnalytics = async (params: AnalyticsFilters) => {
timezone,
});
- // Return the count value for deprecated endpoints
- if (isDeprecatedEndpoint && groupBy === "count") {
- return response.data[0][event];
- }
-
- // Return the object for count endpoints
if (groupBy === "count") {
- return response.data[0];
+ // Return the count value for deprecated endpoints
+ if (isDeprecatedEndpoint) {
+ return response.data[0][event];
+ // Return the object for count endpoints
+ } else {
+ return response.data[0];
+ }
}
// Return array for other endpoints
diff --git a/apps/web/lib/zod/schemas/analytics.ts b/apps/web/lib/zod/schemas/analytics.ts
index 32999a97b6..773438745c 100644
--- a/apps/web/lib/zod/schemas/analytics.ts
+++ b/apps/web/lib/zod/schemas/analytics.ts
@@ -22,17 +22,7 @@ const analyticsEvents = z
"The type of event to retrieve analytics for. Defaults to 'clicks'.",
);
-export const eventTrigger = z
- .enum(["link", "qr"], {
- errorMap: (_issue, _ctx) => {
- return {
- message: "Invalid trigger. Valid triggers are 'link' and 'qr'.",
- };
- },
- })
- .describe("The type of trigger method: link click or QR scan");
-
-const analyticsTypes = z
+const analyticsGroupBy = z
.enum(VALID_ANALYTICS_ENDPOINTS, {
errorMap: (_issue, _ctx) => {
return {
@@ -42,19 +32,19 @@ const analyticsTypes = z
})
.default("count")
.describe(
- "The type of analytics to retrieve. Valid values include: count, timeseries, top_links, etc.",
+ "The type of analytics to retrieve. Defaults to 'count' if undefined.",
);
// For backwards compatibility
export const analyticsPathParamsSchema = z.object({
eventType: analyticsEvents.removeDefault().optional(),
- endpoint: analyticsTypes.removeDefault().optional(),
+ endpoint: analyticsGroupBy.removeDefault().optional(),
});
// Query schema for /api/analytics endpoint
export const analyticsQuerySchema = z.object({
event: analyticsEvents,
- groupBy: analyticsTypes,
+ groupBy: analyticsGroupBy,
domain: z.string().optional().describe("The domain to filter analytics for."),
key: z.string().optional().describe("The short link slug."),
linkId: z
diff --git a/apps/web/lib/zod/schemas/clicks-analytics.ts b/apps/web/lib/zod/schemas/clicks-analytics.ts
index 1dd708fd4f..e42d6814c5 100644
--- a/apps/web/lib/zod/schemas/clicks-analytics.ts
+++ b/apps/web/lib/zod/schemas/clicks-analytics.ts
@@ -1,6 +1,5 @@
import z from "@/lib/zod";
import { COUNTRY_CODES } from "@dub/utils";
-import { eventTrigger } from "./analytics";
// Click analytics response schemas
export const clickAnalyticsResponse = {
@@ -84,7 +83,9 @@ export const clickAnalyticsResponse = {
trigger: z
.object({
- trigger: eventTrigger,
+ trigger: z
+ .enum(["link", "qr"])
+ .describe("The type of trigger method: link click or QR scan"),
clicks: z
.number()
.describe("The number of clicks from this trigger method."),
diff --git a/apps/web/lib/zod/schemas/composite-analytics.ts b/apps/web/lib/zod/schemas/composite-analytics.ts
index 3ecdd44365..db47599851 100644
--- a/apps/web/lib/zod/schemas/composite-analytics.ts
+++ b/apps/web/lib/zod/schemas/composite-analytics.ts
@@ -1,6 +1,5 @@
import z from "@/lib/zod";
import { COUNTRY_CODES } from "@dub/utils";
-import { eventTrigger } from "./analytics";
export const compositeAnalyticsResponse = {
count: z.object({
@@ -96,7 +95,9 @@ export const compositeAnalyticsResponse = {
}),
trigger: z.object({
- trigger: eventTrigger,
+ trigger: z
+ .enum(["link", "qr"])
+ .describe("The type of trigger method: link click or QR scan"),
clicks: z
.number()
.describe("The number of clicks from this trigger method."),
diff --git a/apps/web/lib/zod/schemas/leads-analytics.ts b/apps/web/lib/zod/schemas/leads-analytics.ts
index e8e1a5c9f0..1a65abf422 100644
--- a/apps/web/lib/zod/schemas/leads-analytics.ts
+++ b/apps/web/lib/zod/schemas/leads-analytics.ts
@@ -1,6 +1,5 @@
import z from "@/lib/zod";
import { COUNTRY_CODES } from "@dub/utils";
-import { eventTrigger } from "./analytics";
export const leadAnalyticsResponse = {
count: z
@@ -83,7 +82,9 @@ export const leadAnalyticsResponse = {
trigger: z
.object({
- trigger: eventTrigger,
+ trigger: z
+ .enum(["link", "qr"])
+ .describe("The type of trigger method: link click or QR scan"),
leads: z
.number()
.describe("The number of leads from this trigger method."),
diff --git a/apps/web/lib/zod/schemas/sales-analytics.ts b/apps/web/lib/zod/schemas/sales-analytics.ts
index 26c2412129..cef290bd50 100644
--- a/apps/web/lib/zod/schemas/sales-analytics.ts
+++ b/apps/web/lib/zod/schemas/sales-analytics.ts
@@ -1,6 +1,5 @@
import z from "@/lib/zod";
import { COUNTRY_CODES } from "@dub/utils";
-import { eventTrigger } from "./analytics";
export const saleAnalyticsResponse = {
count: z
@@ -99,7 +98,9 @@ export const saleAnalyticsResponse = {
trigger: z
.object({
- trigger: eventTrigger,
+ trigger: z
+ .enum(["link", "qr"])
+ .describe("The type of trigger method: link click or QR scan"),
sales: z
.number()
.describe("The number of sales from this trigger method."),
diff --git a/apps/web/ui/analytics/export-button.tsx b/apps/web/ui/analytics/export-button.tsx
index ffc1127bab..72312306a2 100644
--- a/apps/web/ui/analytics/export-button.tsx
+++ b/apps/web/ui/analytics/export-button.tsx
@@ -6,7 +6,7 @@ import { AnalyticsContext } from ".";
export default function ExportButton() {
const [loading, setLoading] = useState(false);
- const { totalClicks } = useContext(AnalyticsContext);
+ const { totalEvents } = useContext(AnalyticsContext);
const { queryString } = useContext(AnalyticsContext);
async function exportData() {
@@ -37,7 +37,7 @@ export default function ExportButton() {
}
// show a tooltip to make the user aware that there is no data to export if there is no data
- return totalClicks === 0 || !totalClicks ? (
+ return totalEvents ? (
<Tooltip
content={
<TooltipContent
diff --git a/apps/web/ui/analytics/index.tsx b/apps/web/ui/analytics/index.tsx
index e82c3d46fa..189555a6ca 100644
--- a/apps/web/ui/analytics/index.tsx
+++ b/apps/web/ui/analytics/index.tsx
@@ -6,6 +6,7 @@
*/
import { VALID_ANALYTICS_FILTERS } from "@/lib/analytics/constants";
+import { AnalyticsEvents } from "@/lib/analytics/types";
import useWorkspace from "@/lib/swr/use-workspace";
import { fetcher } from "@dub/utils";
import { endOfDay, startOfDay, subDays } from "date-fns";
@@ -34,7 +35,7 @@ export const AnalyticsContext = createContext<{
end?: Date;
interval?: string;
tagId?: string;
- totalClicks?: number;
+ totalEvents?: { [key in AnalyticsEvents]: number };
admin?: boolean;
demo?: boolean;
requiresUpgrade?: boolean;
@@ -156,8 +157,8 @@ export default function Analytics({
// Reset requiresUpgrade when query changes
useEffect(() => setRequiresUpgrade(false), [queryString]);
- const { data: totalClicks } = useSWR<number>(
- `${baseApiPath}?${queryString}&groupBy=count`,
+ const { data: totalEvents } = useSWR<{ [key in AnalyticsEvents]: number }>(
+ `${baseApiPath}?${queryString}`,
fetcher,
{
onSuccess: () => setRequiresUpgrade(false),
@@ -197,7 +198,7 @@ export default function Analytics({
end, // end of time period
interval, /// time period interval
tagId, // id of a single tag
- totalClicks, // total clicks for the link
+ totalEvents, // totalEvents (clicks, leads, sales)
admin, // whether the user is an admin
demo, // whether the user is viewing demo analytics
requiresUpgrade, // whether an upgrade is required to perform the query
diff --git a/apps/web/ui/domains/domain-card.tsx b/apps/web/ui/domains/domain-card.tsx
index 8f2fe4441b..406412f28e 100644
--- a/apps/web/ui/domains/domain-card.tsx
+++ b/apps/web/ui/domains/domain-card.tsx
@@ -68,7 +68,7 @@ export default function DomainCard({ props }: { props: DomainProps }) {
const { data: clicks } = useSWR<number>(
workspaceId &&
- `/api/analytics?event=clicks&type=count&workspaceId=${workspaceId}&domain=${domain}&key=_root&interval=all`,
+ `/api/analytics?event=clicks&type=count&workspaceId=${workspaceId}&domain=${domain}&key=_root&interval=all_unfiltered`,
(url) =>
fetcher(url, {
headers: {
diff --git a/apps/web/ui/links/link-card.tsx b/apps/web/ui/links/link-card.tsx
index 6e3953ec29..eb4a191206 100644
--- a/apps/web/ui/links/link-card.tsx
+++ b/apps/web/ui/links/link-card.tsx
@@ -1,3 +1,4 @@
+import { AnalyticsEvents } 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";
@@ -117,20 +118,19 @@ export default function LinkCard({
const entry = useIntersectionObserver(linkRef, {});
const isVisible = !!entry?.isIntersecting;
- const { data: clicks } = useSWR<number>(
- // only fetch clicks if the link is visible and there's a slug and the usage is not exceeded
+ const { data: totalEvents } = useSWR<{ [key in AnalyticsEvents]?: number }>(
+ // only fetch data if the link is visible and there's a slug and the usage is not exceeded
isVisible &&
workspaceId &&
!exceededClicks &&
- `/api/analytics?event=clicks&type=count&workspaceId=${workspaceId}&linkId=${id}&interval=all&`,
- (url) =>
- fetcher(url, {
- headers: {
- "Request-Source": "app.dub.co",
- },
- }),
+ `/api/analytics?event=composite&type=count&workspaceId=${workspaceId}&linkId=${id}&interval=all_unfiltered`,
+ fetcher,
{
- fallbackData: props.clicks,
+ fallbackData: {
+ clicks: props.clicks,
+ leads: props.leads,
+ sales: props.sales,
+ },
dedupingInterval: 60000,
},
);
@@ -480,27 +480,34 @@ export default function LinkCard({
<div className="flex items-center space-x-1 p-0.5">
<Chart className="h-4 w-4 text-gray-700" />
<p className="whitespace-nowrap text-sm text-gray-500">
- {nFormatter(clicks)}
+ {nFormatter(totalEvents?.clicks)}
</p>
</div>
<div className="flex items-center space-x-1 p-0.5">
<Magic className="h-4 w-4 text-gray-700" />
- <p className="whitespace-nowrap text-sm text-gray-500">0</p>
+ <p className="whitespace-nowrap text-sm text-gray-500">
+ {nFormatter(totalEvents?.leads)}
+ </p>
</div>
<div className="flex items-center space-x-1 p-0.5">
<CreditCard className="h-4 w-4 text-gray-700" />
- <p className="whitespace-nowrap text-sm text-gray-500">0</p>
+ <p className="whitespace-nowrap text-sm text-gray-500">
+ {nFormatter(totalEvents?.sales)}
+ </p>
</div>
</Link>
) : (
- <NumberTooltip value={clicks} lastClicked={lastClicked}>
+ <NumberTooltip
+ value={totalEvents?.clicks}
+ lastClicked={lastClicked}
+ >
<Link
href={`/${slug}/analytics?domain=${domain}&key=${key}`}
className="flex items-center space-x-1 rounded-md bg-gray-100 px-2 py-0.5 hover:bg-gray-200/75"
>
<Chart className="h-4 w-4 text-gray-700" />
<p className="whitespace-nowrap text-sm text-gray-500">
- {nFormatter(clicks)}
+ {nFormatter(totalEvents?.clicks)}
<span className="ml-1 hidden sm:inline-block">clicks</span>
</p>
</Link>