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.

final changes

Steven Tey2fae5202c571
diff --git a/apps/web/app/api/cron/usage/utils.ts b/apps/web/app/api/cron/usage/utils.ts
index 08f74941f6..65f8882e2b 100644
--- a/apps/web/app/api/cron/usage/utils.ts
+++ b/apps/web/app/api/cron/usage/utils.ts
@@ -125,24 +125,39 @@ export const updateUsage = async (skip?: number) => {
endpoint: "top_links",
interval: "30d",
excludeRoot: "true",
- }).then((data) =>
- data
- .slice(0, 5)
- .map(
- ({
- domain,
- key,
+ }).then(async (data) => {
+ const topFive = data.slice(0, 5);
+ return await Promise.all(
+ topFive.map(
+ async ({
+ link: linkId,
clicks,
}: {
- domain: string;
- key: string;
+ link: string;
clicks: number;
- }) => ({
- link: linkConstructor({ domain, key, pretty: true }),
- clicks,
- }),
+ }) => {
+ const link = await prisma.link.findUnique({
+ where: {
+ id: linkId,
+ },
+ select: {
+ domain: true,
+ key: true,
+ },
+ });
+ if (!link) return;
+ return {
+ link: linkConstructor({
+ domain: link.domain,
+ key: link.key,
+ pretty: true,
+ }),
+ clicks,
+ };
+ },
),
- )
+ );
+ })
: [];
const emails = project.users.map((user) => user.user.email) as string[];
diff --git a/apps/web/app/api/edge/stats/[endpoint]/route.ts b/apps/web/app/api/edge/stats/[endpoint]/route.ts
index 069c053606..7386cb8568 100644
--- a/apps/web/app/api/edge/stats/[endpoint]/route.ts
+++ b/apps/web/app/api/edge/stats/[endpoint]/route.ts
@@ -56,7 +56,8 @@ export const GET = async (
}
const response = await getStats({
- projectId: link.projectId,
+ // projectId can be undefined (for public links that haven't been claimed/synced to a project)
+ ...(link.projectId && { projectId: link.projectId }),
linkId: link.id,
endpoint,
interval,
diff --git a/apps/web/app/api/og/stats/route.tsx b/apps/web/app/api/og/stats/route.tsx
index 82efb32795..40dc3d597c 100644
--- a/apps/web/app/api/og/stats/route.tsx
+++ b/apps/web/app/api/og/stats/route.tsx
@@ -23,16 +23,17 @@ export async function GET(req: NextRequest) {
const domain = req.nextUrl.searchParams.get("domain") || "dub.sh";
const key = req.nextUrl.searchParams.get("key") || "github";
- const data = await getLinkViaEdge(domain, key);
- if (!data?.publicStats) {
+ const link = await getLinkViaEdge(domain, key);
+ if (!link?.publicStats) {
return new Response(`Stats for this link are not public`, {
status: 403,
});
}
const timeseries = await getStats({
- projectId: data.projectId as string,
- linkId: data.id,
+ // projectId can be undefined (for public links that haven't been claimed/synced to a project)
+ ...(link.projectId && { projectId: link.projectId }),
+ linkId: link.id,
endpoint: "timeseries",
interval: "30d",
});
diff --git a/apps/web/lib/stats.ts b/apps/web/lib/stats.ts
index 747234116e..466d0dde90 100644
--- a/apps/web/lib/stats.ts
+++ b/apps/web/lib/stats.ts
@@ -1,4 +1,3 @@
-import { DUB_PROJECT_ID, isDubDomain } from "@dub/utils";
import { conn } from "./planetscale";
export type IntervalProps = "1h" | "24h" | "7d" | "30d" | "90d" | "all";
@@ -96,7 +95,7 @@ export const getStats = async ({
interval,
...rest
}: {
- projectId: string;
+ projectId?: string;
linkId?: string;
domain?: string;
endpoint: string;
@@ -137,7 +136,9 @@ export const getStats = async ({
let url = new URL(
`https://api.us-east.tinybird.co/v0/pipes/${endpoint}_new.json`,
);
- url.searchParams.append("projectId", projectId);
+ if (projectId) {
+ url.searchParams.append("projectId", projectId);
+ }
if (linkId) {
url.searchParams.append("linkId", linkId);
} else if (domain) {
diff --git a/apps/web/scripts/download-top-links.ts b/apps/web/scripts/download-top-links.ts
index 339f7b8041..4d099248ff 100644
--- a/apps/web/scripts/download-top-links.ts
+++ b/apps/web/scripts/download-top-links.ts
@@ -1,6 +1,7 @@
import "dotenv-flow/config";
import * as Papa from "papaparse";
import * as fs from "fs";
+import prisma from "@/lib/prisma";
import { linkConstructor } from "./utils";
import { getStats } from "@/lib/stats";
@@ -8,17 +9,36 @@ async function main() {
const topLinks = await getStats({
projectId: "xxx",
endpoint: "top_links",
+ interval: "30d",
+ excludeRoot: "true",
+ }).then(async (data) => {
+ return await Promise.all(
+ data.map(
+ async ({ link: linkId, clicks }: { link: string; clicks: number }) => {
+ const link = await prisma.link.findUnique({
+ where: {
+ id: linkId,
+ },
+ select: {
+ domain: true,
+ key: true,
+ },
+ });
+ if (!link) return;
+ return {
+ link: linkConstructor({
+ domain: link.domain,
+ key: link.key,
+ pretty: true,
+ }),
+ clicks,
+ };
+ },
+ ),
+ );
});
- const processedLinks = topLinks.map(({ domain, key, clicks }) => ({
- link: linkConstructor({
- domain,
- key,
- }),
- clicks,
- }));
-
- fs.writeFileSync("xxx.csv", Papa.unparse(processedLinks));
+ fs.writeFileSync("xxx.csv", Papa.unparse(topLinks));
}
main();
diff --git a/apps/web/scripts/get-top-links-for-project.ts b/apps/web/scripts/get-top-links-for-project.ts
index b077cef0fa..20bc75ffd3 100644
--- a/apps/web/scripts/get-top-links-for-project.ts
+++ b/apps/web/scripts/get-top-links-for-project.ts
@@ -38,24 +38,34 @@ async function main() {
projectId: project.id,
endpoint: "top_links",
interval: "30d",
- }).then((data) =>
- data
- .slice(0, 5)
- .map(
- ({
- domain,
- key,
- clicks,
- }: {
- domain: string;
- key: string;
- clicks: number;
- }) => ({
- link: linkConstructor({ domain, key, pretty: true }),
- clicks,
- }),
+ excludeRoot: "true",
+ }).then(async (data) => {
+ const topFive = data.slice(0, 5);
+ return await Promise.all(
+ topFive.map(
+ async ({ link: linkId, clicks }: { link: string; clicks: number }) => {
+ const link = await prisma.link.findUnique({
+ where: {
+ id: linkId,
+ },
+ select: {
+ domain: true,
+ key: true,
+ },
+ });
+ if (!link) return;
+ return {
+ link: linkConstructor({
+ domain: link.domain,
+ key: link.key,
+ pretty: true,
+ }),
+ clicks,
+ };
+ },
),
- );
+ );
+ });
console.table(topLinks);
}
diff --git a/apps/web/ui/modals/add-edit-link-modal/index.tsx b/apps/web/ui/modals/add-edit-link-modal/index.tsx
index 2fddd13cf7..b4c23ae928 100644
--- a/apps/web/ui/modals/add-edit-link-modal/index.tsx
+++ b/apps/web/ui/modals/add-edit-link-modal/index.tsx
@@ -460,7 +460,7 @@ function AddEditLinkModal({
type="button"
onClick={() => {
window.confirm(
- "Editing an existing short link will result in broken links and reset its analytics. Are you sure you want to continue?",
+ "Editing an existing short link could potentially break existing links. Are you sure you want to continue?",
) && setLockKey(false);
}}
>