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.

AnalyticsTriggers

Steven Teyca406c5a9dce
diff --git a/apps/web/lib/analytics/constants.ts b/apps/web/lib/analytics/constants.ts
index 3922c3fe63..e8a0822648 100644
--- a/apps/web/lib/analytics/constants.ts
+++ b/apps/web/lib/analytics/constants.ts
@@ -106,11 +106,11 @@ export const VALID_ANALYTICS_ENDPOINTS = [
"devices",
"browsers",
"os",
+ "triggers",
"referers",
"referer_urls",
"top_links",
"top_urls",
- "trigger",
] as const;
export const SINGULAR_ANALYTICS_ENDPOINTS = {
@@ -122,7 +122,7 @@ export const SINGULAR_ANALYTICS_ENDPOINTS = {
referers: "referer",
referer_urls: "refererUrl",
os: "os",
- trigger: "qr",
+ triggers: "trigger",
};
export const TRIGGER_DISPLAY = {
@@ -142,6 +142,7 @@ export const VALID_ANALYTICS_FILTERS = [
"device",
"browser",
"os",
+ "trigger",
"referer",
"refererUrl",
"url",
@@ -150,6 +151,7 @@ export const VALID_ANALYTICS_FILTERS = [
"root",
];
+export const TRIGGER_TYPES = ["qr", "link"] as const;
export const EVENT_TYPES = ["clicks", "leads", "sales"] as const;
export const ANALYTICS_VIEWS = ["default", "funnel"] as const;
@@ -167,6 +169,8 @@ export const OLD_ANALYTICS_ENDPOINTS = [
"browsers",
"browser",
"os",
+ "triggers",
+ "trigger",
"referers",
"referer",
"top_links",
@@ -181,6 +185,7 @@ export const OLD_TO_NEW_ANALYTICS_ENDPOINTS = {
device: "devices",
browser: "browsers",
os: "os",
+ trigger: "triggers",
referer: "referers",
top_links: "top_links",
top_urls: "top_urls",
diff --git a/apps/web/lib/analytics/types.ts b/apps/web/lib/analytics/types.ts
index 0bf28ac8af..5c2069ef89 100644
--- a/apps/web/lib/analytics/types.ts
+++ b/apps/web/lib/analytics/types.ts
@@ -27,7 +27,7 @@ export type AnalyticsView = (typeof ANALYTICS_VIEWS)[number];
export type LocationTabs = "countries" | "cities";
export type TopLinksTabs = "link" | "url";
-export type DeviceTabs = "devices" | "browsers" | "os" | "trigger";
+export type DeviceTabs = "devices" | "browsers" | "os" | "triggers";
export type RefererTabs = "referers" | "referer_urls";
export type AnalyticsFilters = z.infer<typeof analyticsQuerySchema> & {
diff --git a/apps/web/lib/zod/schemas/analytics-response.ts b/apps/web/lib/zod/schemas/analytics-response.ts
index 910c596795..ce395edf2b 100644
--- a/apps/web/lib/zod/schemas/analytics-response.ts
+++ b/apps/web/lib/zod/schemas/analytics-response.ts
@@ -1,3 +1,4 @@
+import { TRIGGER_TYPES } from "@/lib/analytics/constants";
import z from "@/lib/zod";
import { CONTINENT_CODES, COUNTRY_CODES } from "@dub/utils";
@@ -173,6 +174,32 @@ export const analyticsResponse = {
})
.openapi({ ref: "AnalyticsOS" }),
+ triggers: z
+ .object({
+ trigger: z
+ .enum(TRIGGER_TYPES)
+ .describe("The type of trigger method: link click or QR scan"),
+ clicks: z
+ .number()
+ .describe("The number of clicks from this trigger method")
+ .default(0),
+ leads: z
+ .number()
+ .describe("The number of leads from this trigger method")
+ .default(0),
+ sales: z
+ .number()
+ .describe("The number of sales from this trigger method")
+ .default(0),
+ saleAmount: z
+ .number()
+ .describe(
+ "The total amount of sales from this trigger method, in cents",
+ )
+ .default(0),
+ })
+ .openapi({ ref: "AnalyticsTriggers" }),
+
referers: z
.object({
referer: z
diff --git a/apps/web/lib/zod/schemas/analytics.ts b/apps/web/lib/zod/schemas/analytics.ts
index 87b184c3c8..5f989bbf30 100644
--- a/apps/web/lib/zod/schemas/analytics.ts
+++ b/apps/web/lib/zod/schemas/analytics.ts
@@ -2,6 +2,7 @@ import {
EVENT_TYPES,
OLD_ANALYTICS_ENDPOINTS,
OLD_TO_NEW_ANALYTICS_ENDPOINTS,
+ TRIGGER_TYPES,
VALID_ANALYTICS_ENDPOINTS,
eventIntervals,
intervals,
@@ -137,6 +138,10 @@ export const analyticsQuerySchema = z.object({
.transform((v) => capitalize(v) as string | undefined)
.describe("The OS to retrieve analytics for.")
.openapi({ example: "Windows" }),
+ trigger: z
+ .enum(TRIGGER_TYPES)
+ .optional()
+ .describe("The trigger to retrieve analytics for."),
referer: z
.string()
.optional()
diff --git a/apps/web/scripts/dub-sdk.ts b/apps/web/scripts/dub-sdk.ts
new file mode 100644
index 0000000000..13037605fc
--- /dev/null
+++ b/apps/web/scripts/dub-sdk.ts
@@ -0,0 +1,13 @@
+import { dub } from "@/lib/dub";
+import "dotenv-flow/config";
+
+async function main() {
+ const data = await dub.analytics.retrieve({
+ domain: "dub.link",
+ event: "clicks",
+ groupBy: "trigger",
+ });
+ console.log({ data });
+}
+
+main();
diff --git a/apps/web/ui/analytics/device-icon.tsx b/apps/web/ui/analytics/device-icon.tsx
index 2e0b94039e..411900ffaf 100644
--- a/apps/web/ui/analytics/device-icon.tsx
+++ b/apps/web/ui/analytics/device-icon.tsx
@@ -78,7 +78,7 @@ export default function DeviceIcon({
/>
);
}
- } else if (tab === "trigger") {
+ } else if (tab === "triggers") {
if (display === "qr") {
return <QRCode className={className} />;
} else {
diff --git a/apps/web/ui/analytics/devices.tsx b/apps/web/ui/analytics/devices.tsx
index 54210f0723..6390e560f4 100644
--- a/apps/web/ui/analytics/devices.tsx
+++ b/apps/web/ui/analytics/devices.tsx
@@ -28,7 +28,7 @@ export default function Devices() {
{ id: "devices", label: "Devices" },
{ id: "browsers", label: "Browsers" },
{ id: "os", label: "OS" },
- { id: "trigger", label: "Trigger" },
+ { id: "triggers", label: "Triggers" },
]}
selectedTabId={tab}
onSelectTab={setTab}
@@ -51,12 +51,12 @@ export default function Devices() {
/>
),
title:
- tab === "trigger"
+ tab === "triggers"
? TRIGGER_DISPLAY[d.trigger]
: d[singularTabName],
href: queryParams({
set:
- tab === "trigger"
+ tab === "triggers"
? {
qr: d.trigger === "link" ? "false" : "true",
}