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.

type -> groupBy

Kiran Kd6cf874333b1
diff --git a/apps/web/app/api/analytics/edge/route.ts b/apps/web/app/api/analytics/edge/route.ts
index a8beb76188..89f15ce823 100644
--- a/apps/web/app/api/analytics/edge/route.ts
+++ b/apps/web/app/api/analytics/edge/route.ts
@@ -19,7 +19,7 @@ export const GET = async (req: NextRequest) => {
const searchParams = getSearchParams(req.url);
const parsedParams = analyticsQuerySchema.parse(searchParams);
- const { type, domain, key, interval, start, end } = parsedParams;
+ const { groupBy, domain, key, interval, start, end } = parsedParams;
if (!domain || !key) {
throw new DubApiError({
@@ -41,8 +41,8 @@ export const GET = async (req: NextRequest) => {
const ip = ipAddress(req);
const { success } = await ratelimit(
15,
- type === "count" ? "10 s" : "1 m",
- ).limit(`demo-analytics:${demoLink.id}:${ip}:${type}`);
+ groupBy === "count" ? "10 s" : "1 m",
+ ).limit(`demo-analytics:${demoLink.id}:${ip}:${groupBy}`);
if (!success) {
throw new DubApiError({
diff --git a/apps/web/app/api/analytics/export/route.ts b/apps/web/app/api/analytics/export/route.ts
index caf6fbf8eb..c67505eb81 100644
--- a/apps/web/app/api/analytics/export/route.ts
+++ b/apps/web/app/api/analytics/export/route.ts
@@ -53,7 +53,7 @@ export const GET = withWorkspace(
const data = await getAnalytics({
...parsedParams,
event: "clicks",
- type: "top_links",
+ groupBy: "top_links",
workspaceId: workspace.id,
});
@@ -128,7 +128,7 @@ export const GET = withWorkspace(
workspaceId: workspace.id,
...(linkId && { linkId }),
event: "clicks",
- type: endpoint,
+ groupBy: endpoint,
});
if (!response || response.length === 0) return;
diff --git a/apps/web/app/api/analytics/route.ts b/apps/web/app/api/analytics/route.ts
index c581bd3537..790a55a0a2 100644
--- a/apps/web/app/api/analytics/route.ts
+++ b/apps/web/app/api/analytics/route.ts
@@ -15,10 +15,10 @@ export const GET = withWorkspace(
analyticsPathParamsSchema.parse(params);
const parsedParams = analyticsQuerySchema.parse(searchParams);
- let { event, type, domain, key, interval, start, end } = parsedParams;
+ let { event, groupBy, domain, key, interval, start, end } = parsedParams;
event = oldEvent || event;
- type = oldType || type;
+ groupBy = oldType || groupBy;
validDateRangeForPlan({
plan: workspace.plan,
@@ -37,7 +37,7 @@ export const GET = withWorkspace(
const response = await getAnalytics({
...parsedParams,
event,
- type,
+ groupBy,
...(linkId && { linkId }),
workspaceId: workspace.id,
});
diff --git a/apps/web/app/api/cron/usage/utils.ts b/apps/web/app/api/cron/usage/utils.ts
index 064c3e6e03..3cc64b2ac6 100644
--- a/apps/web/app/api/cron/usage/utils.ts
+++ b/apps/web/app/api/cron/usage/utils.ts
@@ -118,7 +118,7 @@ export const updateUsage = async (skip?: number) => {
? await getAnalytics({
workspaceId: workspace.id,
event: "clicks",
- type: "top_links",
+ groupBy: "top_links",
interval: "30d",
root: false,
}).then(async (data) => {
diff --git a/apps/web/app/api/domains/[domain]/transfer/route.ts b/apps/web/app/api/domains/[domain]/transfer/route.ts
index add0c0952d..db49c7bdc3 100644
--- a/apps/web/app/api/domains/[domain]/transfer/route.ts
+++ b/apps/web/app/api/domains/[domain]/transfer/route.ts
@@ -94,7 +94,7 @@ export const POST = withWorkspace(
const totalLinkClicks = await getAnalytics({
domain,
event: "clicks",
- type: "count",
+ groupBy: "count",
workspaceId: workspace.id,
interval: "30d",
root: false,
diff --git a/apps/web/app/api/links/[linkId]/transfer/route.ts b/apps/web/app/api/links/[linkId]/transfer/route.ts
index 2d10c2ebcb..6e08ec0727 100644
--- a/apps/web/app/api/links/[linkId]/transfer/route.ts
+++ b/apps/web/app/api/links/[linkId]/transfer/route.ts
@@ -69,7 +69,7 @@ export const POST = withWorkspace(
const linkClicks = await getAnalytics({
event: "clicks",
- type: "count",
+ groupBy: "count",
linkId: link.id,
interval: "30d",
});
diff --git a/apps/web/app/api/og/analytics/route.tsx b/apps/web/app/api/og/analytics/route.tsx
index 4eb4a193d2..e582803429 100644
--- a/apps/web/app/api/og/analytics/route.tsx
+++ b/apps/web/app/api/og/analytics/route.tsx
@@ -28,7 +28,7 @@ export async function GET(req: NextRequest) {
const data = await getAnalytics({
event: "clicks",
- type: "timeseries",
+ groupBy: "timeseries",
// workspaceId can be undefined (for public links that haven't been claimed/synced to a workspace)
...(link.projectId && { workspaceId: link.projectId }),
linkId: link.id,
diff --git a/apps/web/lib/analytics/get-analytics.ts b/apps/web/lib/analytics/get-analytics.ts
index 1cbbb454c9..1599783329 100644
--- a/apps/web/lib/analytics/get-analytics.ts
+++ b/apps/web/lib/analytics/get-analytics.ts
@@ -30,7 +30,7 @@ const responseSchema = {
export const getAnalytics = async (params: AnalyticsFilters) => {
let {
event,
- type,
+ groupBy,
workspaceId,
linkId,
interval,
@@ -46,7 +46,7 @@ export const getAnalytics = async (params: AnalyticsFilters) => {
// 3. interval is all time
// 4. call is made from dashboard
if (
- type === "count" &&
+ groupBy === "count" &&
linkId &&
interval === "all" &&
headers()?.get("Request-Source") === "app.dub.co"
@@ -95,11 +95,13 @@ export const getAnalytics = async (params: AnalyticsFilters) => {
// Create a Tinybird pipe
const pipe = (isDemo ? tbDemo : tb).buildPipe({
- pipe: `v1_${type}`,
+ pipe: `v1_${groupBy}`,
parameters: analyticsFilterTB,
- data: responseSchema[event][type],
+ data: responseSchema[event][groupBy],
});
+ console.log({groupBy, event})
+
const response = await pipe({
...params,
eventType: event,
@@ -112,7 +114,7 @@ export const getAnalytics = async (params: AnalyticsFilters) => {
// for total clicks|leads, we return just the value;
// everything else we return the full response
- if (type === "count") {
+ if (groupBy === "count") {
if (event === "clicks" || event === "leads") {
return response.data[0][event];
} else {
diff --git a/apps/web/lib/zod/schemas/analytics.ts b/apps/web/lib/zod/schemas/analytics.ts
index 3b77d674c9..29b8a4300a 100644
--- a/apps/web/lib/zod/schemas/analytics.ts
+++ b/apps/web/lib/zod/schemas/analytics.ts
@@ -8,7 +8,7 @@ import { COUNTRY_CODES } from "@dub/utils";
import { booleanQuerySchema } from "./misc";
import { parseDateSchema } from "./utils";
-const analyticsEventParam = z
+const analyticsEvents = z
.enum(EVENT_TYPES, {
errorMap: (_issue, _ctx) => {
return {
@@ -22,7 +22,17 @@ const analyticsEventParam = z
"The type of event to retrieve analytics for. Defaults to 'clicks'.",
);
-const analyticsTypeParam = z
+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
.enum(VALID_ANALYTICS_ENDPOINTS, {
errorMap: (_issue, _ctx) => {
return {
@@ -35,15 +45,16 @@ const analyticsTypeParam = z
"The type of analytics to retrieve. Valid values include: count, timeseries, top_links, etc.",
);
+// For backwards compatibility
export const analyticsPathParamsSchema = z.object({
- eventType: analyticsEventParam.removeDefault().optional(),
- endpoint: analyticsTypeParam.removeDefault().optional(),
+ eventType: analyticsEvents.removeDefault().optional(),
+ endpoint: analyticsTypes.removeDefault().optional(),
});
// Query schema for /api/analytics endpoint
export const analyticsQuerySchema = z.object({
- event: analyticsEventParam,
- type: analyticsTypeParam,
+ event: analyticsEvents,
+ groupBy: analyticsTypes,
domain: z.string().optional().describe("The domain to filter analytics for."),
key: z.string().optional().describe("The short link slug."),
linkId: z
@@ -134,7 +145,7 @@ export const analyticsQuerySchema = z.object({
// Analytics filter params for Tinybird endpoints
export const analyticsFilterTB = z
.object({
- eventType: analyticsEventParam,
+ eventType: analyticsEvents,
workspaceId: z
.string()
.optional()
diff --git a/apps/web/lib/zod/schemas/clicks-analytics.ts b/apps/web/lib/zod/schemas/clicks-analytics.ts
index 8872b099c9..c0ebbfc6aa 100644
--- a/apps/web/lib/zod/schemas/clicks-analytics.ts
+++ b/apps/web/lib/zod/schemas/clicks-analytics.ts
@@ -1,5 +1,6 @@
import z from "@/lib/zod";
import { COUNTRY_CODES } from "@dub/utils";
+import { eventTrigger } from "./analytics";
// Click analytics response schemas
export const clickAnalyticsResponse = {
@@ -53,10 +54,9 @@ export const clickAnalyticsResponse = {
url: z.string().describe("The destination URL"),
clicks: z.number().describe("The number of clicks from this URL"),
}),
+
trigger: z.object({
- trigger: z
- .enum(["link", "qr"])
- .describe("The type of trigger method: link click or QR scan"),
+ trigger: eventTrigger,
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 2218791905..3ecdd44365 100644
--- a/apps/web/lib/zod/schemas/composite-analytics.ts
+++ b/apps/web/lib/zod/schemas/composite-analytics.ts
@@ -1,5 +1,6 @@
import z from "@/lib/zod";
import { COUNTRY_CODES } from "@dub/utils";
+import { eventTrigger } from "./analytics";
export const compositeAnalyticsResponse = {
count: z.object({
@@ -93,4 +94,16 @@ export const compositeAnalyticsResponse = {
sales: z.number().describe("The number of sales from this URL"),
amount: z.number().describe("The total amount of sales from this URL"),
}),
+
+ trigger: z.object({
+ trigger: eventTrigger,
+ clicks: z
+ .number()
+ .describe("The number of clicks from this trigger method."),
+ leads: z.number().describe("The number of leads from this trigger method."),
+ sales: z.number().describe("The number of sales from this trigger method."),
+ amount: z
+ .number()
+ .describe("The total amount of sales from this trigger method."),
+ }),
} as const;
diff --git a/apps/web/lib/zod/schemas/leads-analytics.ts b/apps/web/lib/zod/schemas/leads-analytics.ts
index 4eb66cb97b..e6f00f27aa 100644
--- a/apps/web/lib/zod/schemas/leads-analytics.ts
+++ b/apps/web/lib/zod/schemas/leads-analytics.ts
@@ -1,5 +1,6 @@
import z from "@/lib/zod";
import { COUNTRY_CODES } from "@dub/utils";
+import { eventTrigger } from "./analytics";
export const leadAnalyticsResponse = {
count: z.object({
@@ -61,4 +62,9 @@ export const leadAnalyticsResponse = {
url: z.string().describe("The destination URL"),
leads: z.number().describe("The number of leads from this URL"),
}),
+
+ trigger: z.object({
+ trigger: eventTrigger,
+ leads: z.number().describe("The number of leads from this trigger method."),
+ }),
} as const;
diff --git a/apps/web/lib/zod/schemas/sales-analytics.ts b/apps/web/lib/zod/schemas/sales-analytics.ts
index ef568b8e06..3f5adb923f 100644
--- a/apps/web/lib/zod/schemas/sales-analytics.ts
+++ b/apps/web/lib/zod/schemas/sales-analytics.ts
@@ -1,5 +1,6 @@
import z from "@/lib/zod";
import { COUNTRY_CODES } from "@dub/utils";
+import { eventTrigger } from "./analytics";
export const saleAnalyticsResponse = {
count: z.object({
@@ -73,4 +74,12 @@ export const saleAnalyticsResponse = {
sales: z.number().describe("The number of sales from this URL"),
amount: z.number().describe("The total amount of sales from this URL"),
}),
+
+ trigger: z.object({
+ trigger: eventTrigger,
+ sales: z.number().describe("The number of sales from this trigger method."),
+ amount: z
+ .number()
+ .describe("The total amount of sales from this trigger method."),
+ }),
} as const;
diff --git a/apps/web/scripts/download-top-links.ts b/apps/web/scripts/download-top-links.ts
index fd047b6833..cd2b1331d7 100644
--- a/apps/web/scripts/download-top-links.ts
+++ b/apps/web/scripts/download-top-links.ts
@@ -8,7 +8,7 @@ import * as Papa from "papaparse";
async function main() {
const topLinks = await getAnalytics({
event: "clicks",
- type: "top_links",
+ groupBy: "top_links",
workspaceId: "xxx",
interval: "30d",
root: false,
diff --git a/apps/web/scripts/get-top-links-for-workspace.ts b/apps/web/scripts/get-top-links-for-workspace.ts
index 1be0884b3d..d57c0c106b 100644
--- a/apps/web/scripts/get-top-links-for-workspace.ts
+++ b/apps/web/scripts/get-top-links-for-workspace.ts
@@ -36,7 +36,7 @@ async function main() {
}
const topLinks = await getAnalytics({
event: "clicks",
- type: "top_links",
+ groupBy: "top_links",
workspaceId: workspace.id,
interval: "30d",
root: false,
diff --git a/apps/web/tests/analytics/clicks.test.ts b/apps/web/tests/analytics/clicks.test.ts
index cf33dc2c27..fa401660f9 100644
--- a/apps/web/tests/analytics/clicks.test.ts
+++ b/apps/web/tests/analytics/clicks.test.ts
@@ -11,17 +11,17 @@ describe.runIf(env.CI).sequential("GET /analytics?event=clicks", async () => {
const { workspace, http } = await h.init();
const { workspaceId } = workspace;
- VALID_ANALYTICS_ENDPOINTS.map((type) => {
- test(`by ${type}`, async () => {
+ VALID_ANALYTICS_ENDPOINTS.map((groupBy) => {
+ test(`by ${groupBy}`, async () => {
const { status, data } = await http.get<any[]>({
path: `/analytics`,
- query: { event: "clicks", type, workspaceId, ...filter },
+ query: { event: "clicks", groupBy, workspaceId, ...filter },
});
const responseSchema =
- type === "count"
+ groupBy === "count"
? z.number()
- : z.array(clickAnalyticsResponse[type].strict());
+ : z.array(clickAnalyticsResponse[groupBy].strict());
const parsed = responseSchema.safeParse(data);
diff --git a/apps/web/tests/analytics/leads.test.ts b/apps/web/tests/analytics/leads.test.ts
index 8b202d47fe..ea04bdc6b9 100644
--- a/apps/web/tests/analytics/leads.test.ts
+++ b/apps/web/tests/analytics/leads.test.ts
@@ -2,26 +2,26 @@ import { VALID_ANALYTICS_ENDPOINTS } from "@/lib/analytics/constants";
import z from "@/lib/zod";
import { leadAnalyticsResponse } from "@/lib/zod/schemas/leads-analytics";
import { describe, expect, test } from "vitest";
-import { env } from "../utils/env";
import { IntegrationHarness } from "../utils/integration";
import { filter } from "./utils";
+import { env } from "../utils/env";
describe.runIf(env.CI).sequential("GET /analytics?event=leads", async () => {
const h = new IntegrationHarness();
const { workspace, http } = await h.init();
const { workspaceId } = workspace;
- VALID_ANALYTICS_ENDPOINTS.map((type) => {
- test(`by ${type}`, async () => {
+ VALID_ANALYTICS_ENDPOINTS.map((groupBy) => {
+ test(`by ${groupBy}`, async () => {
const { status, data } = await http.get<any[]>({
path: `/analytics`,
- query: { event: "leads", type, workspaceId, ...filter },
+ query: { event: "leads", groupBy, workspaceId, ...filter },
});
const responseSchema =
- type === "count"
+ groupBy === "count"
? z.number()
- : z.array(leadAnalyticsResponse[type].strict());
+ : z.array(leadAnalyticsResponse[groupBy].strict());
const parsed = responseSchema.safeParse(data);
diff --git a/apps/web/tests/analytics/sales.test.ts b/apps/web/tests/analytics/sales.test.ts
index 0e97c1ab37..c1159ca9a1 100644
--- a/apps/web/tests/analytics/sales.test.ts
+++ b/apps/web/tests/analytics/sales.test.ts
@@ -2,26 +2,26 @@ import { VALID_ANALYTICS_ENDPOINTS } from "@/lib/analytics/constants";
import z from "@/lib/zod";
import { saleAnalyticsResponse } from "@/lib/zod/schemas/sales-analytics";
import { describe, expect, test } from "vitest";
-import { env } from "../utils/env";
import { IntegrationHarness } from "../utils/integration";
import { filter } from "./utils";
+import { env } from "../utils/env";
describe.runIf(env.CI).sequential("GET /analytics?event=sales", async () => {
const h = new IntegrationHarness();
const { workspace, http } = await h.init();
const { workspaceId } = workspace;
- VALID_ANALYTICS_ENDPOINTS.map((type) => {
- test(`by ${type}`, async () => {
+ VALID_ANALYTICS_ENDPOINTS.map((groupBy) => {
+ test(`by ${groupBy}`, async () => {
const { status, data } = await http.get<any[]>({
path: `/analytics`,
- query: { event: "sales", type, workspaceId, ...filter },
+ query: { event: "sales", groupBy, workspaceId, ...filter },
});
const responseSchema =
- type === "count"
- ? saleAnalyticsResponse[type].strict()
- : z.array(saleAnalyticsResponse[type].strict());
+ groupBy === "count"
+ ? saleAnalyticsResponse[groupBy].strict()
+ : z.array(saleAnalyticsResponse[groupBy].strict());
const parsed = responseSchema.safeParse(data);
diff --git a/apps/web/ui/analytics/analytics-area-chart.tsx b/apps/web/ui/analytics/analytics-area-chart.tsx
index 83b179b3af..92e4895fbf 100644
--- a/apps/web/ui/analytics/analytics-area-chart.tsx
+++ b/apps/web/ui/analytics/analytics-area-chart.tsx
@@ -22,7 +22,7 @@ export default function AnalyticsAreaChart({
>(
`${baseApiPath}?${editQueryString(queryString, {
event: "composite",
- type: "timeseries",
+ groupBy: "timeseries",
})}`,
fetcher,
{
diff --git a/apps/web/ui/analytics/devices.tsx b/apps/web/ui/analytics/devices.tsx
index ee6232c52b..efd1e4b5e5 100644
--- a/apps/web/ui/analytics/devices.tsx
+++ b/apps/web/ui/analytics/devices.tsx
@@ -27,7 +27,7 @@ export default function Devices() {
} & { [key in AnalyticsEvents]: number })[]
>(
`${baseApiPath}?${editQueryString(queryString, {
- type: tab,
+ groupBy: tab,
})}`,
fetcher,
{
diff --git a/apps/web/ui/analytics/index.tsx b/apps/web/ui/analytics/index.tsx
index b59c07cdfa..bc98833b49 100644
--- a/apps/web/ui/analytics/index.tsx
+++ b/apps/web/ui/analytics/index.tsx
@@ -150,7 +150,7 @@ export default function Analytics({
useEffect(() => setRequiresUpgrade(false), [queryString]);
const { data: totalClicks } = useSWR<number>(
- `${baseApiPath}?${queryString}&type=count`,
+ `${baseApiPath}?${queryString}&groupBy=count`,
fetcher,
{
onSuccess: () => setRequiresUpgrade(false),
diff --git a/apps/web/ui/analytics/locations.tsx b/apps/web/ui/analytics/locations.tsx
index 4657e278d6..a4c1459654 100644
--- a/apps/web/ui/analytics/locations.tsx
+++ b/apps/web/ui/analytics/locations.tsx
@@ -30,7 +30,7 @@ export default function Locations() {
} & { [key in AnalyticsEvents]: number })[]
>(
`${baseApiPath}?${editQueryString(queryString, {
- type: tab,
+ groupBy: tab,
})}`,
fetcher,
{ shouldRetryOnError: !requiresUpgrade },
diff --git a/apps/web/ui/analytics/main.tsx b/apps/web/ui/analytics/main.tsx
index e4baa25c55..75da0b4e37 100644
--- a/apps/web/ui/analytics/main.tsx
+++ b/apps/web/ui/analytics/main.tsx
@@ -84,7 +84,7 @@ export default function Main() {
}>(
`${baseApiPath}?${editQueryString(queryString, {
event: "composite",
- type: "count",
+ groupBy: "count",
})}`,
fetcher,
{
diff --git a/apps/web/ui/analytics/referer.tsx b/apps/web/ui/analytics/referer.tsx
index 40430d31ff..f0d58aa9c0 100644
--- a/apps/web/ui/analytics/referer.tsx
+++ b/apps/web/ui/analytics/referer.tsx
@@ -19,7 +19,7 @@ export default function Referer() {
})[]
>(
`${baseApiPath}?${editQueryString(queryString, {
- type: "referers",
+ groupBy: "referers",
})}`,
fetcher,
{ shouldRetryOnError: !requiresUpgrade },
diff --git a/apps/web/ui/analytics/top-links.tsx b/apps/web/ui/analytics/top-links.tsx
index be4086a347..e4b15f6a8b 100644
--- a/apps/web/ui/analytics/top-links.tsx
+++ b/apps/web/ui/analytics/top-links.tsx
@@ -38,7 +38,7 @@ export default function TopLinks() {
} & { [key in AnalyticsEvents]: number })[]
>(
`${baseApiPath}?${editQueryString(queryString, {
- type: `top_${tab}s`,
+ groupBy: `top_${tab}s`,
})}`,
fetcher,
{