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.

fix: polyfill top links

flooorianflod7b2b37b7afb
diff --git a/apps/web/app/api/analytics/[endpoint]/export/route.ts b/apps/web/app/api/analytics/[endpoint]/export/route.ts
index 2b87c17cf5..e493e8a77f 100644
--- a/apps/web/app/api/analytics/[endpoint]/export/route.ts
+++ b/apps/web/app/api/analytics/[endpoint]/export/route.ts
@@ -2,18 +2,38 @@ import { getAnalytics } from "@/lib/analytics";
import { DubApiError } from "@/lib/api/errors";
import { withAuth } from "@/lib/auth";
import { getDomainViaEdge } from "@/lib/planetscale";
+import prisma from "@/lib/prisma";
import {
analyticsEndpointSchema,
getAnalyticsQuerySchema,
} from "@/lib/zod/schemas/analytics";
+import { linkConstructor } from "@dub/utils";
import { json2csv } from "json-2-csv";
+// converts data to CSV and returns a Response object
+const respondCSV = (data: Record<string, string>[], filename: string) => {
+ const csv = json2csv(data, {
+ parseValue(fieldValue, defaultParser) {
+ if (fieldValue instanceof Date) {
+ return fieldValue.toISOString();
+ }
+ return defaultParser(fieldValue);
+ },
+ });
+ return new Response(csv, {
+ headers: {
+ "Content-Type": "text/csv",
+ "Content-Disposition": `attachment; filename="${filename}"`,
+ },
+ });
+};
+
// GET /api/analytics/[endpoint]/export – get export data for analytics
export const GET = withAuth(
async ({ params, searchParams, project, link }) => {
const { endpoint } = analyticsEndpointSchema.parse(params);
const parsedParams = getAnalyticsQuerySchema.parse(searchParams);
- const { domain, key, interval, url } = parsedParams;
+ const { domain, key, interval } = parsedParams;
// return 403 if project is on the free plan and interval is 90d or all
if (
@@ -32,54 +52,74 @@ export const GET = withAuth(
? await getDomainViaEdge(domain).then((d) => d?.id)
: null;
- const response = await getAnalytics({
- projectId: project.id,
- ...(linkId && { linkId }),
- endpoint,
- ...parsedParams,
- });
+ if (endpoint === "top_links") {
+ const topLinks = await getAnalytics({
+ projectId: project.id,
+ endpoint: "top_links",
+ ...parsedParams,
+ }).then(async (data) => {
+ // if project is on the free plan, only return the first 100 links
+ const links = project?.plan === "free" ? data.slice(0, 100) : data;
- // if there is no data to export so return 204
- if (response.length === 0) {
- return new Response(undefined, {
- status: 204,
+ return await Promise.all(
+ links.map(
+ async ({
+ link: linkId,
+ clicks,
+ }: {
+ link: string;
+ clicks: number;
+ }) => {
+ const link = await prisma.link.findUnique({
+ where: {
+ id: linkId,
+ },
+ select: {
+ domain: true,
+ key: true,
+ url: true,
+ },
+ });
+ if (!link) return;
+ return {
+ linkId,
+ shortLink: linkConstructor({
+ domain: link.domain,
+ key: link.key,
+ }),
+ domain: link.domain,
+ key: link.key,
+ url: link.url,
+ clicks,
+ };
+ },
+ ),
+ );
});
- }
- // convert the response to CSV
- const csv = json2csv(
- endpoint === "timeseries"
- ? response.map((entry: { start: Date; clicks: number }) => ({
- date: entry.start,
- clicks: entry.clicks,
- }))
- : endpoint === "top_links"
- ? response.map((entry: { link: string; clicks: number }) => ({
- linkId: entry.link,
- shortLink: `https://${domain}.dub.co/${key}`,
- domain: domain,
- key: key,
- url: url,
- clicks: entry.clicks,
- }))
- : response,
- {
- parseValue(fieldValue, defaultParser) {
- if (fieldValue instanceof Date) {
- return fieldValue.toISOString();
- }
+ if (!topLinks) {
+ return new Response(undefined, {
+ status: 204,
+ });
+ }
- return defaultParser(fieldValue);
- },
- },
- );
+ return respondCSV(topLinks, "top_links.csv");
+ } else {
+ const response = await getAnalytics({
+ projectId: project.id,
+ ...(linkId && { linkId }),
+ endpoint,
+ ...parsedParams,
+ });
- return new Response(csv, {
- headers: {
- "Content-Type": "text/csv",
- "Content-Disposition": `attachment; filename="clicks.csv"`,
- },
- });
+ if (response.length === 0) {
+ return new Response(undefined, {
+ status: 204,
+ });
+ }
+
+ return respondCSV(response, `${endpoint}.csv`);
+ }
},
{
needNotExceededClicks: true,