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.

Start and end date optional params added to analytics endpoint

UsmanDev09c5dc21ead766
diff --git a/apps/web/app/api/analytics/[endpoint]/route.ts b/apps/web/app/api/analytics/[endpoint]/route.ts
index ca6153283e..bbe189181b 100644
--- a/apps/web/app/api/analytics/[endpoint]/route.ts
+++ b/apps/web/app/api/analytics/[endpoint]/route.ts
@@ -13,9 +13,18 @@ export const GET = withWorkspace(
async ({ params, searchParams, workspace, link }) => {
const { endpoint } = analyticsEndpointSchema.parse(params);
const parsedParams = getAnalyticsQuerySchema.parse(searchParams);
- const { domain, key, interval } = parsedParams;
- // return 403 if workspace is on the free plan and interval is 90d or all
+ const { domain, key, interval, startDate, endDate } = parsedParams;
+
+ // Either interval can be provided, or both start and end date
+ if((interval && (startDate && endDate)) || (startDate && !endDate) || (!startDate && endDate)) {
+ throw new DubApiError({
+ code: "unprocessable_entity",
+ message: "Either provide interval or start and end date, not both"
+ })
+ }
+
+ // return 403 if workspace is on the free plan and interval is 90d or allI do
if (
workspace?.plan === "free" &&
(interval === "all" || interval === "90d")
@@ -38,6 +47,7 @@ export const GET = withWorkspace(
endpoint,
...parsedParams,
});
+
return NextResponse.json(response);
},
{
diff --git a/apps/web/lib/analytics.ts b/apps/web/lib/analytics.ts
index fe97933e0a..fe1a115c9c 100644
--- a/apps/web/lib/analytics.ts
+++ b/apps/web/lib/analytics.ts
@@ -99,6 +99,8 @@ export const getAnalytics = async ({
domain,
endpoint,
interval,
+ startDate,
+ endDate,
...rest
}: z.infer<typeof getAnalyticsQuerySchema> & {
workspaceId?: string;
@@ -106,7 +108,6 @@ export const getAnalytics = async ({
endpoint: (typeof VALID_TINYBIRD_ENDPOINTS)[number];
}) => {
// Note: we're using decodeURIComponent in this function because that's how we store it in MySQL and Tinybird
-
if (
!DATABASE_URL ||
!process.env.TINYBIRD_API_KEY ||
@@ -124,6 +125,7 @@ export const getAnalytics = async ({
"SELECT clicks FROM Link WHERE `id` = ?",
[linkId],
);
+
if (response.rows.length === 0) {
response = await conn.execute(
"SELECT clicks FROM Domain WHERE `id` = ?",
@@ -163,6 +165,23 @@ export const getAnalytics = async ({
url.searchParams.append("granularity", intervalData[interval].granularity);
}
+ if(startDate && endDate) {
+ url.searchParams.append(
+ "start",
+ startDate
+ .toString()
+ .replace("T", " ")
+ .replace("Z", ""),
+ );
+ url.searchParams.append(
+ "end",
+ endDate
+ .toString()
+ .replace("T", " ")
+ .replace("Z", ""),
+ );
+ }
+
VALID_ANALYTICS_FILTERS.forEach((filter) => {
if (rest[filter] !== undefined) {
url.searchParams.append(filter, rest[filter]);
diff --git a/apps/web/lib/zod/schemas/analytics.ts b/apps/web/lib/zod/schemas/analytics.ts
index abf08927af..cd97ca3ad4 100644
--- a/apps/web/lib/zod/schemas/analytics.ts
+++ b/apps/web/lib/zod/schemas/analytics.ts
@@ -3,6 +3,7 @@ import z from "@/lib/zod";
import { COUNTRY_CODES } from "@dub/utils";
import { booleanQuerySchema } from "./misc";
+
export const getAnalyticsQuerySchema = z.object({
domain: z.string().optional().describe("The domain of the short link."),
key: z.string().optional().describe("The short link slug."),
@@ -10,6 +11,31 @@ export const getAnalyticsQuerySchema = z.object({
.enum(intervals)
.optional()
.describe("The interval to retrieve analytics for."),
+ startDate: z
+ .string()
+ .datetime()
+ .refine(value => {
+ const inputDate = new Date(value);
+ const foundingDate = new Date("2022-09-22T00:00:00.000Z"); // Dub.co founding date
+ return inputDate >= foundingDate;
+ },
+ {
+ message: "The start date cannot be earlier than September 22, 2022."
+ })
+ .optional()
+ .describe("The start date and time when to retrieve analytics from."),
+ endDate: z
+ .string()
+ .datetime()
+ .refine(value => {
+ const todaysDate = new Date();
+ const inputDate = new Date(value);
+ return inputDate <= todaysDate;
+ }, {
+ message: "The end date cannot be in future."
+ })
+ .optional()
+ .describe("The end date and time when to retrieve analytics from."),
country: z
.enum(COUNTRY_CODES)
.optional()