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.

add remaining endpoints

Kiran K8c80ccd3cca6
diff --git a/apps/web/app/api/analytics/admin/clicks/[endpoint]/route.ts b/apps/web/app/api/analytics/admin/clicks/[endpoint]/route.ts
index 380379c7cf..11e6586f8f 100644
--- a/apps/web/app/api/analytics/admin/clicks/[endpoint]/route.ts
+++ b/apps/web/app/api/analytics/admin/clicks/[endpoint]/route.ts
@@ -3,7 +3,7 @@ import { withAdmin } from "@/lib/auth";
import {
analyticsEndpointSchema,
clickAnalyticsQuerySchema,
-} from "@/lib/zod/schemas/click-analytics";
+} from "@/lib/zod/schemas/clicks-analytics";
import { NextResponse } from "next/server";
// GET /api/admin/analytics/clicks – get click analytics for admin
diff --git a/apps/web/app/api/analytics/clicks/[endpoint]/route.ts b/apps/web/app/api/analytics/clicks/[endpoint]/route.ts
index fb35ef21d6..c0232d0ca2 100644
--- a/apps/web/app/api/analytics/clicks/[endpoint]/route.ts
+++ b/apps/web/app/api/analytics/clicks/[endpoint]/route.ts
@@ -5,7 +5,7 @@ import { getDomainViaEdge } from "@/lib/planetscale";
import {
analyticsEndpointSchema,
clickAnalyticsQuerySchema,
-} from "@/lib/zod/schemas/click-analytics";
+} from "@/lib/zod/schemas/clicks-analytics";
import { NextResponse } from "next/server";
// GET /api/analytics/clicks/[endpoint] – get click analytics
diff --git a/apps/web/app/api/analytics/edge/clicks/[endpoint]/route.ts b/apps/web/app/api/analytics/edge/clicks/[endpoint]/route.ts
index 3dc93848c1..5c1cd3a145 100644
--- a/apps/web/app/api/analytics/edge/clicks/[endpoint]/route.ts
+++ b/apps/web/app/api/analytics/edge/clicks/[endpoint]/route.ts
@@ -10,7 +10,7 @@ import { ratelimit } from "@/lib/upstash";
import {
analyticsEndpointSchema,
clickAnalyticsQuerySchema,
-} from "@/lib/zod/schemas/click-analytics";
+} from "@/lib/zod/schemas/clicks-analytics";
import { DUB_DEMO_LINKS, DUB_WORKSPACE_ID, getSearchParams } from "@dub/utils";
import { ipAddress } from "@vercel/edge";
import { NextResponse, type NextRequest } from "next/server";
diff --git a/apps/web/app/api/analytics/export/route.ts b/apps/web/app/api/analytics/export/route.ts
index 6db391eab1..87bf008b6d 100644
--- a/apps/web/app/api/analytics/export/route.ts
+++ b/apps/web/app/api/analytics/export/route.ts
@@ -8,7 +8,7 @@ import { DubApiError } from "@/lib/api/errors";
import { withWorkspace } from "@/lib/auth";
import { getDomainViaEdge } from "@/lib/planetscale";
import { prisma } from "@/lib/prisma";
-import { clickAnalyticsQuerySchema } from "@/lib/zod/schemas/click-analytics";
+import { clickAnalyticsQuerySchema } from "@/lib/zod/schemas/clicks-analytics";
import { linkConstructor } from "@dub/utils";
import { json2csv } from "json-2-csv";
import JSZip from "jszip";
diff --git a/apps/web/app/api/analytics/leads/[endpoint]/route.ts b/apps/web/app/api/analytics/leads/[endpoint]/route.ts
index 72370e09a2..ae07dc7160 100644
--- a/apps/web/app/api/analytics/leads/[endpoint]/route.ts
+++ b/apps/web/app/api/analytics/leads/[endpoint]/route.ts
@@ -5,7 +5,7 @@ import { getDomainViaEdge } from "@/lib/planetscale";
import {
analyticsEndpointSchema,
clickAnalyticsQuerySchema,
-} from "@/lib/zod/schemas/click-analytics";
+} from "@/lib/zod/schemas/clicks-analytics";
import { NextResponse } from "next/server";
// GET /api/analytics/leads/[endpoint] – get leads analytics
diff --git a/apps/web/app/api/analytics/sales/[endpoint]/route.ts b/apps/web/app/api/analytics/sales/[endpoint]/route.ts
new file mode 100644
index 0000000000..40481b3425
--- /dev/null
+++ b/apps/web/app/api/analytics/sales/[endpoint]/route.ts
@@ -0,0 +1,45 @@
+import { getLeads } from "@/lib/analytics/leads";
+import { validDateRangeForPlan } from "@/lib/analytics/utils";
+import { withWorkspace } from "@/lib/auth";
+import { getDomainViaEdge } from "@/lib/planetscale";
+import {
+ analyticsEndpointSchema,
+ clickAnalyticsQuerySchema,
+} from "@/lib/zod/schemas/clicks-analytics";
+import { NextResponse } from "next/server";
+
+// GET /api/analytics/sales/[endpoint] – get sales analytics
+export const GET = withWorkspace(
+ async ({ params, searchParams, workspace, link }) => {
+ const { endpoint } = analyticsEndpointSchema.parse(params);
+ const parsedParams = clickAnalyticsQuerySchema.parse(searchParams);
+
+ const { domain, key, interval, start, end } = parsedParams;
+
+ validDateRangeForPlan({
+ plan: workspace.plan,
+ interval,
+ start,
+ end,
+ throwError: true,
+ });
+
+ const linkId = link
+ ? link.id
+ : domain && key === "_root"
+ ? await getDomainViaEdge(domain).then((d) => d?.id)
+ : null;
+
+ const response = await getLeads({
+ ...parsedParams,
+ endpoint,
+ ...(linkId && { linkId }),
+ workspaceId: workspace.id,
+ });
+
+ return NextResponse.json(response);
+ },
+ {
+ needNotExceededClicks: true,
+ },
+);
diff --git a/apps/web/lib/analytics/clicks.ts b/apps/web/lib/analytics/clicks.ts
index ebe7f5e057..d25eabc03d 100644
--- a/apps/web/lib/analytics/clicks.ts
+++ b/apps/web/lib/analytics/clicks.ts
@@ -7,7 +7,7 @@ import {
clickAnalyticsQuerySchema,
getClickAnalytics,
getClickAnalyticsResponse,
-} from "../zod/schemas/click-analytics";
+} from "../zod/schemas/clicks-analytics";
import { INTERVAL_DATA } from "./constants";
import { AnalyticsEndpoints } from "./types";
diff --git a/apps/web/lib/analytics/leads.ts b/apps/web/lib/analytics/leads.ts
index 8c0ef1daf4..615e1a143a 100644
--- a/apps/web/lib/analytics/leads.ts
+++ b/apps/web/lib/analytics/leads.ts
@@ -4,8 +4,8 @@ import { getDaysDifference } from "@dub/utils";
import {
clickAnalyticsQuerySchema,
getClickAnalytics,
-} from "../zod/schemas/click-analytics";
-import { leadAnalyticsResponse } from "../zod/schemas/lead-analytics";
+} from "../zod/schemas/clicks-analytics";
+import { leadAnalyticsResponse } from "../zod/schemas/leads-analytics";
import { INTERVAL_DATA } from "./constants";
import { AnalyticsEndpoints } from "./types";
diff --git a/apps/web/lib/analytics/sales.ts b/apps/web/lib/analytics/sales.ts
new file mode 100644
index 0000000000..70d1fbc37e
--- /dev/null
+++ b/apps/web/lib/analytics/sales.ts
@@ -0,0 +1,84 @@
+import { tb } from "@/lib/tinybird";
+import z from "@/lib/zod";
+import { getDaysDifference } from "@dub/utils";
+import {
+ clickAnalyticsQuerySchema,
+ getClickAnalytics,
+} from "../zod/schemas/clicks-analytics";
+import { saleAnalyticsResponse } from "../zod/schemas/sales-analytics";
+import { INTERVAL_DATA } from "./constants";
+import { AnalyticsEndpoints } from "./types";
+
+export const getSales = async (
+ props: z.infer<typeof clickAnalyticsQuerySchema> & {
+ workspaceId?: string;
+ endpoint: AnalyticsEndpoints;
+ },
+) => {
+ let { workspaceId, endpoint, interval, start, end } = props;
+
+ // get all-time clicks count if:
+ // 1. linkId is defined
+ // 2. endpoint is not defined
+ // 3. interval is all time
+ // 4. call is made from dashboard
+ // if (
+ // linkId &&
+ // endpoint === "count" &&
+ // interval === "all" &&
+ // headers()?.get("Request-Source") === "app.dub.co"
+ // ) {
+ // let response = await conn.execute(
+ // "SELECT clicks FROM Link WHERE `id` = ?",
+ // [linkId],
+ // );
+
+ // if (response.rows.length === 0) {
+ // response = await conn.execute(
+ // "SELECT clicks FROM Domain WHERE `id` = ?",
+ // [linkId],
+ // );
+ // if (response.rows.length === 0) {
+ // return 0;
+ // }
+ // }
+ // return response.rows[0]["clicks"];
+ // }
+
+ const pipe = tb.buildPipe({
+ pipe: `sales_${endpoint}`,
+ parameters: getClickAnalytics,
+ data: saleAnalyticsResponse[endpoint],
+ });
+
+ let granularity: "minute" | "hour" | "day" | "month" = "day";
+
+ if (interval) {
+ start = INTERVAL_DATA[interval].startDate;
+ end = new Date(Date.now());
+ granularity = INTERVAL_DATA[interval].granularity;
+ } else {
+ start = new Date(start!);
+ end = end ? new Date(end) : new Date(Date.now());
+ if (getDaysDifference(start, end) > 180) {
+ granularity = "month";
+ }
+
+ // swap start and end if start is greater than end
+ if (start > end) {
+ [start, end] = [end, start];
+ }
+ }
+
+ const response = await pipe({
+ ...props,
+ workspaceId,
+ start: start.toISOString().split(".")[0],
+ end: end.toISOString().split(".")[0],
+ granularity,
+ });
+
+ // for total sales, we return just the value;
+ // everything else we return an array of values
+ return endpoint === "count" ? response.data[0].sales : response.data;
+};
diff --git a/apps/web/lib/openapi/analytics/clicks/browsers.ts b/apps/web/lib/openapi/analytics/clicks/browsers.ts
index d3aff1c6b8..8b39eefad4 100644
--- a/apps/web/lib/openapi/analytics/clicks/browsers.ts
+++ b/apps/web/lib/openapi/analytics/clicks/browsers.ts
@@ -3,7 +3,7 @@ import z from "@/lib/zod";
import {
clickAnalyticsQuerySchema,
getClickAnalyticsResponse,
-} from "@/lib/zod/schemas/click-analytics";
+} from "@/lib/zod/schemas/clicks-analytics";
import { ZodOpenApiOperationObject } from "zod-openapi";
import { workspaceParamsSchema } from "../../request";
diff --git a/apps/web/lib/openapi/analytics/clicks/cities.ts b/apps/web/lib/openapi/analytics/clicks/cities.ts
index a6a9ba1df3..4c4f568589 100644
--- a/apps/web/lib/openapi/analytics/clicks/cities.ts
+++ b/apps/web/lib/openapi/analytics/clicks/cities.ts
@@ -3,7 +3,7 @@ import z from "@/lib/zod";
import {
clickAnalyticsQuerySchema,
getClickAnalyticsResponse,
-} from "@/lib/zod/schemas/click-analytics";
+} from "@/lib/zod/schemas/clicks-analytics";
import { ZodOpenApiOperationObject } from "zod-openapi";
import { workspaceParamsSchema } from "../../request";
diff --git a/apps/web/lib/openapi/analytics/clicks/count.ts b/apps/web/lib/openapi/analytics/clicks/count.ts
index 9ffaae57ca..50198dca6b 100644
--- a/apps/web/lib/openapi/analytics/clicks/count.ts
+++ b/apps/web/lib/openapi/analytics/clicks/count.ts
@@ -1,5 +1,5 @@
import { openApiErrorResponses } from "@/lib/openapi/responses";
-import { clickAnalyticsQuerySchema } from "@/lib/zod/schemas/click-analytics";
+import { clickAnalyticsQuerySchema } from "@/lib/zod/schemas/clicks-analytics";
import { ZodOpenApiOperationObject } from "zod-openapi";
import { workspaceParamsSchema } from "../../request";
diff --git a/apps/web/lib/openapi/analytics/clicks/countries.ts b/apps/web/lib/openapi/analytics/clicks/countries.ts
index 0f4d672f30..b0d4eca2df 100644
--- a/apps/web/lib/openapi/analytics/clicks/countries.ts
+++ b/apps/web/lib/openapi/analytics/clicks/countries.ts
@@ -3,7 +3,7 @@ import z from "@/lib/zod";
import {
clickAnalyticsQuerySchema,
getClickAnalyticsResponse,
-} from "@/lib/zod/schemas/click-analytics";
+} from "@/lib/zod/schemas/clicks-analytics";
import { ZodOpenApiOperationObject } from "zod-openapi";
import { workspaceParamsSchema } from "../../request";
diff --git a/apps/web/lib/openapi/analytics/clicks/devices.ts b/apps/web/lib/openapi/analytics/clicks/devices.ts
index e72ede7e43..2072b71fec 100644
--- a/apps/web/lib/openapi/analytics/clicks/devices.ts
+++ b/apps/web/lib/openapi/analytics/clicks/devices.ts
@@ -3,7 +3,7 @@ import z from "@/lib/zod";
import {
clickAnalyticsQuerySchema,
getClickAnalyticsResponse,
-} from "@/lib/zod/schemas/click-analytics";
+} from "@/lib/zod/schemas/clicks-analytics";
import { ZodOpenApiOperationObject } from "zod-openapi";
import { workspaceParamsSchema } from "../../request";
diff --git a/apps/web/lib/openapi/analytics/clicks/os.ts b/apps/web/lib/openapi/analytics/clicks/os.ts
index d10b047985..641bd17829 100644
--- a/apps/web/lib/openapi/analytics/clicks/os.ts
+++ b/apps/web/lib/openapi/analytics/clicks/os.ts
@@ -3,7 +3,7 @@ import z from "@/lib/zod";
import {
clickAnalyticsQuerySchema,
getClickAnalyticsResponse,
-} from "@/lib/zod/schemas/click-analytics";
+} from "@/lib/zod/schemas/clicks-analytics";
import { ZodOpenApiOperationObject } from "zod-openapi";
import { workspaceParamsSchema } from "../../request";
diff --git a/apps/web/lib/openapi/analytics/clicks/referers.ts b/apps/web/lib/openapi/analytics/clicks/referers.ts
index 27fe88df5c..eeba82421a 100644
--- a/apps/web/lib/openapi/analytics/clicks/referers.ts
+++ b/apps/web/lib/openapi/analytics/clicks/referers.ts
@@ -3,7 +3,7 @@ import z from "@/lib/zod";
import {
clickAnalyticsQuerySchema,
getClickAnalyticsResponse,
-} from "@/lib/zod/schemas/click-analytics";
+} from "@/lib/zod/schemas/clicks-analytics";
import { ZodOpenApiOperationObject } from "zod-openapi";
import { workspaceParamsSchema } from "../../request";
diff --git a/apps/web/lib/openapi/analytics/clicks/timeseries.ts b/apps/web/lib/openapi/analytics/clicks/timeseries.ts
index 8a2cf3cbcc..e16ffccc4c 100644
--- a/apps/web/lib/openapi/analytics/clicks/timeseries.ts
+++ b/apps/web/lib/openapi/analytics/clicks/timeseries.ts
@@ -3,7 +3,7 @@ import z from "@/lib/zod";
import {
clickAnalyticsQuerySchema,
getClickAnalyticsResponse,
-} from "@/lib/zod/schemas/click-analytics";
+} from "@/lib/zod/schemas/clicks-analytics";
import { ZodOpenApiOperationObject } from "zod-openapi";
import { workspaceParamsSchema } from "../../request";
diff --git a/apps/web/lib/openapi/analytics/clicks/top-links.ts b/apps/web/lib/openapi/analytics/clicks/top-links.ts
index 0214e933a0..41e7a4fc8f 100644
--- a/apps/web/lib/openapi/analytics/clicks/top-links.ts
+++ b/apps/web/lib/openapi/analytics/clicks/top-links.ts
@@ -3,7 +3,7 @@ import z from "@/lib/zod";
import {
clickAnalyticsQuerySchema,
getClickAnalyticsResponse,
-} from "@/lib/zod/schemas/click-analytics";
+} from "@/lib/zod/schemas/clicks-analytics";
import { ZodOpenApiOperationObject } from "zod-openapi";
import { workspaceParamsSchema } from "../../request";
diff --git a/apps/web/lib/openapi/analytics/clicks/top-urls.ts b/apps/web/lib/openapi/analytics/clicks/top-urls.ts
index dda96f42d1..bf97dd9d70 100644
--- a/apps/web/lib/openapi/analytics/clicks/top-urls.ts
+++ b/apps/web/lib/openapi/analytics/clicks/top-urls.ts
@@ -3,7 +3,7 @@ import z from "@/lib/zod";
import {
clickAnalyticsQuerySchema,
getClickAnalyticsResponse,
-} from "@/lib/zod/schemas/click-analytics";
+} from "@/lib/zod/schemas/clicks-analytics";
import { ZodOpenApiOperationObject } from "zod-openapi";
import { workspaceParamsSchema } from "../../request";
diff --git a/apps/web/lib/zod/schemas/click-analytics.ts b/apps/web/lib/zod/schemas/clicks-analytics.ts
similarity index 100%
rename from apps/web/lib/zod/schemas/click-analytics.ts
rename to apps/web/lib/zod/schemas/clicks-analytics.ts
diff --git a/apps/web/lib/zod/schemas/lead-analytics.ts b/apps/web/lib/zod/schemas/leads-analytics.ts
similarity index 100%
rename from apps/web/lib/zod/schemas/lead-analytics.ts
rename to apps/web/lib/zod/schemas/leads-analytics.ts
diff --git a/apps/web/lib/zod/schemas/sales-analytics.ts b/apps/web/lib/zod/schemas/sales-analytics.ts
new file mode 100644
index 0000000000..3824278182
--- /dev/null
+++ b/apps/web/lib/zod/schemas/sales-analytics.ts
@@ -0,0 +1,64 @@
+import z from "@/lib/zod";
+import { COUNTRY_CODES } from "@dub/utils";
+
+export const saleAnalyticsResponse = {
+ count: z.object({
+ sales: z.number().describe("The total number of sales"),
+ }),
+
+ timeseries: z.object({
+ start: z.string().describe("The starting timestamp of the interval"),
+ sales: z.number().describe("The number of sales in the interval"),
+ }),
+
+ countries: z
+ .object({
+ country: z
+ .enum(COUNTRY_CODES)
+ .describe("The 2-letter country code: https://d.to/geo"),
+ sales: z.number().describe("The number of sales from this country"),
+ })
+ .openapi({ ref: "salesByCountry" }),
+
+ cities: z
+ .object({
+ city: z.string().describe("The name of the city"),
+ country: z
+ .enum(COUNTRY_CODES)
+ .describe("The 2-letter country code of the city: https://d.to/geo"),
+ sales: z.number().describe("The number of sales from this city"),
+ })
+ .openapi({ ref: "salesByCities" }),
+
+ devices: z.object({
+ device: z.string().describe("The name of the device"),
+ sales: z.number().describe("The number of sales from this device"),
+ }),
+
+ browsers: z.object({
+ browser: z.string().describe("The name of the browser"),
+ sales: z.number().describe("The number of sales from this browser"),
+ }),
+
+ os: z.object({
+ os: z.string().describe("The name of the OS"),
+ sales: z.number().describe("The number of sales from this OS"),
+ }),
+
+ referers: z.object({
+ referer: z
+ .string()
+ .describe("The name of the referer. If unknown, this will be `(direct)`"),
+ sales: z.number().describe("The number of sales from this referer"),
+ }),
+
+ top_links: z.object({
+ link: z.string().describe("The unique ID of the short link"),
+ sales: z.number().describe("The number of sales from this link"),
+ }),
+
+ top_urls: z.object({
+ url: z.string().describe("The destination URL"),
+ sales: z.number().describe("The number of sales from this URL"),
+ }),
+} as const;
diff --git a/packages/tinybird/endpoints/sales_browsers.pipe b/packages/tinybird/endpoints/sales_browsers.pipe
new file mode 100644
index 0000000000..295eecc49e
--- /dev/null
+++ b/packages/tinybird/endpoints/sales_browsers.pipe
@@ -0,0 +1,60 @@
+DESCRIPTION >
+ Top browsers by sales
+
+NODE endpoint
+
+SQL >
+
+ %
+ SELECT browser, COUNT(browser) as sales
+ FROM
+ dub_sale_events_mv
+ {% if not defined(linkId) and defined(workspaceId) %}
+ PREWHERE link_id in (
+ SELECT link_id
+ from dub_links_metadata_latest FINAL
+ WHERE
+ workspace_id
+ = {{
+ String(
+ workspaceId,
+ 'cl7wsy2836920mjrb352g5wfx',
+ description="The ID of the workspace",
+ required=True,
+ )
+ }}
+ AND deleted == 0
+ {% if defined(domain) %} AND domain IN {{ Array(domain, 'String') }} {% end %}
+ {% if defined(tagId) %} AND has(tag_ids, {{ tagId }}) {% end %}
+ {% if defined(root) %}
+ {% if Boolean(root) == 1 %} AND key = '_root' {% else %} AND key != '_root' {% end %}
+ {% end %}
+ )
+ {% end %}
+ WHERE
+ true
+ {% if defined(linkId) %}
+ AND link_id
+ = {{
+ String(
+ linkId,
+ 'clsdzhhlq0003vskwcaz6nm39',
+ description="The ID of the link",
+ )
+ }}
+ {% end %}
+ {% if defined(qr) %} AND qr = {{ Boolean(qr)}} {% end %}
+ AND browser != 'Unknown'
+ {% if defined(country) %} AND country = {{ country }} {% end %}
+ {% if defined(city) %} AND city = {{ city }} {% end %}
+ {% if defined(device) %} AND device = {{ device }} {% end %}
+ {% if defined(browser) %} AND browser = {{ browser }} {% end %}
+ {% if defined(os) %} AND os = {{ os }} {% end %}
+ {% if defined(referer) %} AND referer = {{ referer }} {% end %}
+ {% if defined(url) %} AND url = {{ url }} {% end %}
+ {% if defined(start) %} AND timestamp >= {{ DateTime(start) }} {% end %}
+ {% if defined(end) %} AND timestamp <= {{ DateTime(end) }} {% end %}
+ GROUP BY browser
+ ORDER BY sales DESC
+
+
diff --git a/packages/tinybird/endpoints/sales_cities.pipe b/packages/tinybird/endpoints/sales_cities.pipe
new file mode 100644
index 0000000000..7a767f00b8
--- /dev/null
+++ b/packages/tinybird/endpoints/sales_cities.pipe
@@ -0,0 +1,60 @@
+DESCRIPTION >
+ Top cities by sales
+
+NODE endpoint
+
+SQL >
+
+ %
+ SELECT city, country, COUNT(city) as sales
+ FROM
+ dub_sale_events_mv
+ {% if not defined(linkId) and defined(workspaceId) %}
+ PREWHERE link_id in (
+ SELECT link_id
+ from dub_links_metadata_latest FINAL
+ WHERE
+ workspace_id
+ = {{
+ String(
+ workspaceId,
+ 'cl7wsy2836920mjrb352g5wfx',
+ description="The ID of the workspace",
+ required=True,
+ )
+ }}
+ AND deleted == 0
+ {% if defined(domain) %} AND domain IN {{ Array(domain, 'String') }} {% end %}
+ {% if defined(tagId) %} AND has(tag_ids, {{ tagId }}) {% end %}
+ {% if defined(root) %}
+ {% if Boolean(root) == 1 %} AND key = '_root' {% else %} AND key != '_root' {% end %}
+ {% end %}
+ )
+ {% end %}
+ WHERE
+ true
+ {% if defined(linkId) %}
+ AND link_id
+ = {{
+ String(
+ linkId,
+ 'clsdzhhlq0003vskwcaz6nm39',
+ description="The ID of the link",
+ )
+ }}
+ {% end %}
+ {% if defined(qr) %} AND qr = {{ Boolean(qr)}} {% end %}
+ AND city != 'Unknown'
+ {% if defined(country) %} AND country = {{ country }} {% end %}
+ {% if defined(city) %} AND city = {{ city }} {% end %}
+ {% if defined(device) %} AND device = {{ device }} {% end %}
+ {% if defined(browser) %} AND browser = {{ browser }} {% end %}
+ {% if defined(os) %} AND os = {{ os }} {% end %}
+ {% if defined(referer) %} AND referer = {{ referer }} {% end %}
+ {% if defined(url) %} AND url = {{ url }} {% end %}
+ {% if defined(start) %} AND timestamp >= {{ DateTime(start) }} {% end %}
+ {% if defined(end) %} AND timestamp <= {{ DateTime(end) }} {% end %}
+ GROUP BY city, country
+ ORDER BY sales DESC
+
+
diff --git a/packages/tinybird/endpoints/sales_count.pipe b/packages/tinybird/endpoints/sales_count.pipe
new file mode 100644
index 0000000000..7bb63b28b3
--- /dev/null
+++ b/packages/tinybird/endpoints/sales_count.pipe
@@ -0,0 +1,56 @@
+DESCRIPTION >
+ Total sales for a given project or link
+
+NODE endpoint
+SQL >
+
+ %
+ SELECT COUNT(*) as sales
+ FROM
+ dub_sale_events_mv
+ {% if not defined(linkId) and defined(workspaceId) %}
+ PREWHERE link_id in (
+ SELECT link_id
+ from dub_links_metadata_latest FINAL
+ WHERE
+ workspace_id
+ = {{
+ String(
+ workspaceId,
+ 'cl7wsy2836920mjrb352g5wfx',
+ description="The ID of the workspace",
+ required=True,
+ )
+ }}
+ AND deleted == 0
+ {% if defined(domain) %} AND domain IN {{ Array(domain, 'String') }} {% end %}
+ {% if defined(tagId) %} AND has(tag_ids, {{ tagId }}) {% end %}
+ {% if defined(root) %}
+ {% if Boolean(root) == 1 %} AND key = '_root' {% else %} AND key != '_root' {% end %}
+ {% end %}
+ )
+ {% end %}
+ WHERE
+ true
+ {% if defined(linkId) %}
+ AND link_id
+ = {{
+ String(
+ linkId,
+ 'clsdzhhlq0003vskwcaz6nm39',
+ description="The ID of the link",
+ )
+ }}
+ {% end %}
+ {% if defined(qr) %} AND qr = {{ Boolean(qr)}} {% end %}
+ {% if defined(country) %} AND country = {{ country }} {% end %}
+ {% if defined(city) %} AND city = {{ city }} {% end %}
+ {% if defined(device) %} AND device = {{ device }} {% end %}
+ {% if defined(browser) %} AND browser = {{ browser }} {% end %}
+ {% if defined(os) %} AND os = {{ os }} {% end %}
+ {% if defined(referer) %} AND referer = {{ referer }} {% end %}
+ {% if defined(url) %} AND url = {{ url }} {% end %}
+ {% if defined(start) %} AND timestamp >= {{ DateTime(start) }} {% end %}
+ {% if defined(end) %} AND timestamp <= {{ DateTime(end) }} {% end %}
+
+
diff --git a/packages/tinybird/endpoints/sales_countries.pipe b/packages/tinybird/endpoints/sales_countries.pipe
new file mode 100644
index 0000000000..847fff2e8e
--- /dev/null
+++ b/packages/tinybird/endpoints/sales_countries.pipe
@@ -0,0 +1,60 @@
+DESCRIPTION >
+ Top countries by sales
+
+NODE endpoint
+
+SQL >
+
+ %
+ SELECT country, COUNT(country) as sales
+ FROM
+ dub_sale_events_mv
+ {% if not defined(linkId) and defined(workspaceId) %}
+ PREWHERE link_id in (
+ SELECT link_id
+ from dub_links_metadata_latest FINAL
+ WHERE
+ workspace_id
+ = {{
+ String(
+ workspaceId,
+ 'cl7wsy2836920mjrb352g5wfx',
+ description="The ID of the workspace",
+ required=True,
+ )
+ }}
+ AND deleted == 0
+ {% if defined(domain) %} AND domain IN {{ Array(domain, 'String') }} {% end %}
+ {% if defined(tagId) %} AND has(tag_ids, {{ tagId }}) {% end %}
+ {% if defined(root) %}
+ {% if Boolean(root) == 1 %} AND key = '_root' {% else %} AND key != '_root' {% end %}
+ {% end %}
+ )
+ {% end %}
+ WHERE
+ true
+ {% if defined(linkId) %}
+ AND link_id
+ = {{
+ String(
+ linkId,
+ 'clsdzhhlq0003vskwcaz6nm39',
+ description="The ID of the link",
+ )
+ }}
+ {% end %}
+ {% if defined(qr) %} AND qr = {{ Boolean(qr)}} {% end %}
+ AND country != 'Unknown'
+ {% if defined(country) %} AND country = {{ country }} {% end %}
+ {% if defined(city) %} AND city = {{ city }} {% end %}
+ {% if defined(device) %} AND device = {{ device }} {% end %}
+ {% if defined(browser) %} AND browser = {{ browser }} {% end %}
+ {% if defined(os) %} AND os = {{ os }} {% end %}
+ {% if defined(referer) %} AND referer = {{ referer }} {% end %}
+ {% if defined(url) %} AND url = {{ url }} {% end %}
+ {% if defined(start) %} AND timestamp >= {{ DateTime(start) }} {% end %}
+ {% if defined(end) %} AND timestamp <= {{ DateTime(end) }} {% end %}
+ GROUP BY country
+ ORDER BY sales DESC
+
+
diff --git a/packages/tinybird/endpoints/sales_devices.pipe b/packages/tinybird/endpoints/sales_devices.pipe
new file mode 100644
index 0000000000..09c3c3739c
--- /dev/null
+++ b/packages/tinybird/endpoints/sales_devices.pipe
@@ -0,0 +1,60 @@
+DESCRIPTION >
+ Top devices by sales
+
+NODE endpoint
+
+SQL >
+
+ %
+ SELECT device, COUNT(device) as sales
+ FROM
+ dub_sale_events_mv
+ {% if not defined(linkId) and defined(workspaceId) %}
+ PREWHERE link_id in (
+ SELECT link_id
+ from dub_links_metadata_latest FINAL
+ WHERE
+ workspace_id
+ = {{
+ String(
+ workspaceId,
+ 'cl7wsy2836920mjrb352g5wfx',
+ description="The ID of the workspace",
+ required=True,
+ )
+ }}
+ AND deleted == 0
+ {% if defined(domain) %} AND domain IN {{ Array(domain, 'String') }} {% end %}
+ {% if defined(tagId) %} AND has(tag_ids, {{ tagId }}) {% end %}
+ {% if defined(root) %}
+ {% if Boolean(root) == 1 %} AND key = '_root' {% else %} AND key != '_root' {% end %}
+ {% end %}
+ )
+ {% end %}
+ WHERE
+ true
+ {% if defined(linkId) %}
+ AND link_id
+ = {{
+ String(
+ linkId,
+ 'clsdzhhlq0003vskwcaz6nm39',
+ description="The ID of the link",
+ )
+ }}
+ {% end %}
+ {% if defined(qr) %} AND qr = {{ Boolean(qr)}} {% end %}
+ AND device != 'Unknown'
+ {% if defined(country) %} AND country = {{ country }} {% end %}
+ {% if defined(city) %} AND city = {{ city }} {% end %}
+ {% if defined(device) %} AND device = {{ device }} {% end %}
+ {% if defined(browser) %} AND browser = {{ browser }} {% end %}
+ {% if defined(os) %} AND os = {{ os }} {% end %}
+ {% if defined(referer) %} AND referer = {{ referer }} {% end %}
+ {% if defined(url) %} AND url = {{ url }} {% end %}
+ {% if defined(start) %} AND timestamp >= {{ DateTime(start) }} {% end %}
+ {% if defined(end) %} AND timestamp <= {{ DateTime(end) }} {% end %}
+ GROUP BY device
+ ORDER BY sales DESC
+
+
diff --git a/packages/tinybird/endpoints/sales_os.pipe b/packages/tinybird/endpoints/sales_os.pipe
new file mode 100644
index 0000000000..64683e3c59
--- /dev/null
+++ b/packages/tinybird/endpoints/sales_os.pipe
@@ -0,0 +1,60 @@
+DESCRIPTION >
+ Top OS by sales
+
+NODE endpoint
+
+SQL >
+
+ %
+ SELECT os, COUNT(os) as sales
+ FROM
+ dub_sale_events_mv
+ {% if not defined(linkId) and defined(workspaceId) %}
+ PREWHERE link_id in (
+ SELECT link_id
+ from dub_links_metadata_latest FINAL
+ WHERE
+ workspace_id
+ = {{
+ String(
+ workspaceId,
+ 'cl7wsy2836920mjrb352g5wfx',
+ description="The ID of the workspace",
+ required=True,
+ )
+ }}
+ AND deleted == 0
+ {% if defined(domain) %} AND domain IN {{ Array(domain, 'String') }} {% end %}
+ {% if defined(tagId) %} AND has(tag_ids, {{ tagId }}) {% end %}
+ {% if defined(root) %}
+ {% if Boolean(root) == 1 %} AND key = '_root' {% else %} AND key != '_root' {% end %}
+ {% end %}
+ )
+ {% end %}
+ WHERE
+ true
+ {% if defined(linkId) %}
+ AND link_id
+ = {{
+ String(
+ linkId,
+ 'clsdzhhlq0003vskwcaz6nm39',
+ description="The ID of the link",
+ )
+ }}
+ {% end %}
+ {% if defined(qr) %} AND qr = {{ Boolean(qr)}} {% end %}
+ AND os != 'Unknown'
+ {% if defined(country) %} AND country = {{ country }} {% end %}
+ {% if defined(city) %} AND city = {{ city }} {% end %}
+ {% if defined(device) %} AND device = {{ device }} {% end %}
+ {% if defined(browser) %} AND browser = {{ browser }} {% end %}
+ {% if defined(os) %} AND os = {{ os }} {% end %}
+ {% if defined(referer) %} AND referer = {{ referer }} {% end %}
+ {% if defined(url) %} AND url = {{ url }} {% end %}
+ {% if defined(start) %} AND timestamp >= {{ DateTime(start) }} {% end %}
+ {% if defined(end) %} AND timestamp <= {{ DateTime(end) }} {% end %}
+ GROUP BY os
+ ORDER BY sales DESC
+
+
diff --git a/packages/tinybird/endpoints/sales_referers.pipe b/packages/tinybird/endpoints/sales_referers.pipe
new file mode 100644
index 0000000000..bf9cd04a4f
--- /dev/null
+++ b/packages/tinybird/endpoints/sales_referers.pipe
@@ -0,0 +1,61 @@
+DESCRIPTION >
+ Top referers by sales
+
+NODE endpoint
+
+SQL >
+
+ %
+ SELECT referer, COUNT(referer) as sales
+ FROM
+ dub_sale_events_mv
+ {% if not defined(linkId) and defined(workspaceId) %}
+ PREWHERE link_id in (
+ SELECT link_id
+ from dub_links_metadata_latest FINAL
+ WHERE
+ workspace_id
+ = {{
+ String(
+ workspaceId,
+ 'cl7wsy2836920mjrb352g5wfx',
+ description="The ID of the workspace",
+ required=True,
+ )
+ }}
+ AND deleted == 0
+ {% if defined(domain) %} AND domain IN {{ Array(domain, 'String') }} {% end %}
+ {% if defined(tagId) %} AND has(tag_ids, {{ tagId }}) {% end %}
+ {% if defined(root) %}
+ {% if Boolean(root) == 1 %} AND key = '_root' {% else %} AND key != '_root' {% end %}
+ {% end %}
+ )
+ {% end %}
+ WHERE
+ true
+ {% if defined(linkId) %}
+ AND link_id
+ = {{
+ String(
+ linkId,
+ 'clsdzhhlq0003vskwcaz6nm39',
+ description="The ID of the link",
+ )
+ }}
+ {% end %}
+ {% if defined(qr) %} AND qr = {{ Boolean(qr)}} {% end %}
+ {% if defined(country) %} AND country = {{ country }} {% end %}
+ {% if defined(city) %} AND city = {{ city }} {% end %}
+ {% if defined(url) %} AND url = {{ url }} {% end %}
+ {% if defined(alias) %} AND alias = {{ alias }} {% end %}
+ {% if defined(device) %} AND device = {{ device }} {% end %}
+ {% if defined(browser) %} AND browser = {{ browser }} {% end %}
+ {% if defined(os) %} AND os = {{ os }} {% end %}
+ {% if defined(referer) %} AND referer = {{ referer }} {% end %}
+ {% if defined(url) %} AND url = {{ url }} {% end %}
+ {% if defined(start) %} AND timestamp >= {{ DateTime(start) }} {% end %}
+ {% if defined(end) %} AND timestamp <= {{ DateTime(end) }} {% end %}
+ GROUP BY referer
+ ORDER BY sales DESC
+
+
diff --git a/packages/tinybird/endpoints/sales_timeseries.pipe b/packages/tinybird/endpoints/sales_timeseries.pipe
new file mode 100644
index 0000000000..c6fc8e6c19
--- /dev/null
+++ b/packages/tinybird/endpoints/sales_timeseries.pipe
@@ -0,0 +1,131 @@
+DESCRIPTION >
+ Timeseries sales data
+
+NODE day_intervals
+SQL >
+
+ %
+ WITH
+ toStartOfDay(toDateTime64({{ DateTime64(start, '2022-09-25 00:00:00.000') }}, 3)) AS start,
+ toStartOfDay(toDateTime64({{ DateTime64(end, '2022-12-20 00:00:00.000') }}, 3)) AS end
+ SELECT arrayJoin(arrayMap(x -> toDateTime64(x, 3), range(toUInt32(start + 86400), toUInt32(end + 86400), 86400))) as interval
+
+
+
+NODE hour_intervals
+SQL >
+
+ %
+ WITH
+ toStartOfHour(toDateTime64({{ DateTime64(start, '2022-12-19 00:00:00.000') }}, 3)) AS start,
+ toStartOfHour(toDateTime64({{ DateTime64(end, '2022-12-20 00:00:00.000') }}, 3)) AS end
+ SELECT arrayJoin(arrayMap(x -> toDateTime64(x, 3), range(toUInt32(start + 3600), toUInt32(end + 3600), 3600))) as interval
+
+
+
+NODE minute_intervals
+SQL >
+
+ %
+ WITH
+ toStartOfMinute(toDateTime64({{ DateTime64(start, '2022-12-20 15:08:57.000') }}, 3)) AS start,
+ toStartOfMinute(toDateTime64({{ DateTime64(end, '2022-12-20 16:08:57.000') }}, 3)) AS end
+ SELECT arrayJoin(arrayMap(x -> toDateTime64(x, 3), range(toUInt32(start + 60), toUInt32(end + 60), 60))) as interval
+
+
+
+NODE month_intervals
+SQL >
+
+ %
+ SELECT
+ toStartOfMonth(toDateTime64({{ DateTime64(start, '2023-02-23 20:45:02.959') }}, 3)) as interval
+ ORDER BY interval ASC
+ WITH FILL
+ FROM
+ toStartOfMonth(toDateTime64({{ DateTime64(start, '2023-02-23 20:45:02.959') }}, 3))
+ TO
+ toStartOfMonth(addMonths(toDateTime64({{ DateTime64(end, '2023-06-06 20:45:02.959') }}, 3), 1))
+ STEP interval 1 month
+
+
+
+NODE timeseries_data
+DESCRIPTION >
+ undefined
+
+SQL >
+
+ %
+ SELECT
+ {% if granularity == "minute" %} toStartOfMinute(timestamp)
+ {% elif granularity == "hour" %} toStartOfHour(timestamp)
+ {% elif granularity == "month" %} toDateTime(toStartOfMonth(timestamp))
+ {% else %} toDateTime(toStartOfDay(timestamp))
+ {% end %} AS interval,
+ uniq(*) as sales
+ FROM
+ dub_sale_events_mv
+ {% if not defined(linkId) and defined(workspaceId) %}
+ PREWHERE link_id in (
+ SELECT link_id
+ from dub_links_metadata_latest FINAL
+ WHERE
+ workspace_id
+ = {{
+ String(
+ workspaceId,
+ 'cl7wsy2836920mjrb352g5wfx',
+ description="The ID of the workspace",
+ required=True,
+ )
+ }}
+ AND deleted == 0
+ {% if defined(domain) %} AND domain IN {{ Array(domain, 'String') }} {% end %}
+ {% if defined(tagId) %} AND has(tag_ids, {{ tagId }}) {% end %}
+ {% if defined(root) %}
+ {% if Boolean(root) == 1 %} AND key = '_root' {% else %} AND key != '_root' {% end %}
+ {% end %}
+ )
+ {% end %}
+ WHERE
+ true
+ {% if defined(linkId) %}
+ AND link_id
+ = {{
+ String(
+ linkId,
+ 'clsdzhhlq0003vskwcaz6nm39',
+ description="The ID of the link",
+ )
+ }}
+ {% end %}
+ {% if defined(qr) %} AND qr = {{ Boolean(qr)}} {% end %}
+ {% if defined(country) %} AND country = {{ country }} {% end %}
+ {% if defined(city) %} AND city = {{ city }} {% end %}
+ {% if defined(device) %} AND device = {{ device }} {% end %}
+ {% if defined(browser) %} AND browser = {{ browser }} {% end %}
+ {% if defined(os) %} AND os = {{ os }} {% end %}
+ {% if defined(referer) %} AND referer = {{ referer }} {% end %}
+ {% if defined(url) %} AND url = {{ url }} {% end %}
+ AND timestamp >= {{ DateTime(start, '2023-06-25 00:00:00') }}
+ AND timestamp < {{ DateTime(end, '2023-10-20 00:00:00') }}
+ GROUP BY interval
+ ORDER BY interval
+
+
+
+NODE endpoint
+SQL >
+
+ %
+ SELECT formatDateTime(interval, '%FT%T.000Z') as start, sales
+ FROM
+ {% if granularity == "minute" %} minute_intervals
+ {% elif granularity == "hour" %} hour_intervals
+ {% elif granularity == "month" %} month_intervals
+ {% else %} day_intervals
+ {% end %}
+ LEFT JOIN timeseries_data USING interval
+
+
diff --git a/packages/tinybird/endpoints/sales_top_links.pipe b/packages/tinybird/endpoints/sales_top_links.pipe
new file mode 100644
index 0000000000..98e344ff20
--- /dev/null
+++ b/packages/tinybird/endpoints/sales_top_links.pipe
@@ -0,0 +1,49 @@
+DESCRIPTION >
+ Top links by sales
+
+NODE endpoint
+
+SQL >
+
+ %
+ SELECT link_id as link, COUNT(*) AS sales
+ FROM
+ dub_sale_events_mv
+ {% if defined(workspaceId) %}
+ PREWHERE link_id in (
+ SELECT link_id
+ from dub_links_metadata_latest FINAL
+ WHERE
+ workspace_id
+ = {{
+ String(
+ workspaceId,
+ 'cl7wsy2836920mjrb352g5wfx',
+ description="The ID of the workspace",
+ required=True,
+ )
+ }}
+ AND deleted == 0
+ {% if defined(domain) %} AND domain IN {{ Array(domain, 'String') }} {% end %}
+ {% if defined(tagId) %} AND has(tag_ids, {{ tagId }}) {% end %}
+ {% if defined(root) %}
+ {% if Boolean(root) == 1 %} AND key = '_root' {% else %} AND key != '_root' {% end %}
+ {% end %}
+ )
+ {% end %}
+ WHERE
+ true
+ {% if defined(qr) %} AND qr = {{ Boolean(qr)}} {% end %}
+ {% if defined(country) %} AND country = {{ country }} {% end %}
+ {% if defined(city) %} AND city = {{ city }} {% end %}
+ {% if defined(device) %} AND device = {{ device }} {% end %}
+ {% if defined(browser) %} AND browser = {{ browser }} {% end %}
+ {% if defined(os) %} AND os = {{ os }} {% end %}
+ {% if defined(referer) %} AND referer = {{ referer }} {% end %}
+ {% if defined(url) %} AND url = {{ url }} {% end %}
+ {% if defined(start) %} AND timestamp >= {{ DateTime(start) }} {% end %}
+ {% if defined(end) %} AND timestamp <= {{ DateTime(end) }} {% end %}
+ GROUP BY link_id
+ ORDER BY sales DESC
+
+