The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Created getLinkClicksCount utility function
Steven Tey8f369def1783
diff --git a/lib/upstash.ts b/lib/upstash.tsindex 242a128d24..7d7d3bb9f4 100644--- a/lib/upstash.ts+++ b/lib/upstash.ts@@ -135,6 +135,13 @@ export async function getLinkCountForProject(slug: string) {return await redis.zcard(`${slug}:links:timestamps`);}+export async function getLinkClicksCount(hostname: string, key: string) {+ const start = Date.now() - 2629746000; // 30 days ago+ return (+ (await redis.zcount(`${hostname}:clicks:${key}`, start, Date.now())) || "0"+ );+}+export async function addLink(hostname: string,url: string,diff --git a/pages/api/edge/links/[key]/clicks.ts b/pages/api/edge/links/[key]/clicks.tsindex 76f7103434..336c533041 100644--- a/pages/api/edge/links/[key]/clicks.ts+++ b/pages/api/edge/links/[key]/clicks.ts@@ -1,5 +1,5 @@import type { NextRequest } from "next/server";-import { redis } from "@/lib/upstash";+import { getLinkClicksCount } from "@/lib/upstash";export const config = {runtime: "experimental-edge",@@ -9,7 +9,7 @@ export default async function handler(req: NextRequest) {if (req.method === "GET") {const url = req.nextUrl.pathname;const key = url.split("/")[4];- const response = (await redis.zcard(`dub.sh:clicks:${key}`)) || "0";+ const response = await getLinkClicksCount("dub.sh", key);return new Response(JSON.stringify(response), { status: 200 });} else {return new Response(`Method ${req.method} Not Allowed`, { status: 405 });diff --git a/pages/api/projects/[slug]/domains/[domain]/links/[key]/clicks.ts b/pages/api/projects/[slug]/domains/[domain]/links/[key]/clicks.tsindex df90e7c63d..0a209f6cf0 100644--- a/pages/api/projects/[slug]/domains/[domain]/links/[key]/clicks.ts+++ b/pages/api/projects/[slug]/domains/[domain]/links/[key]/clicks.ts@@ -1,12 +1,12 @@import { NextApiRequest, NextApiResponse } from "next";import { withProjectAuth } from "@/lib/auth";-import { redis } from "@/lib/upstash";+import { getLinkClicksCount, redis } from "@/lib/upstash";export default withProjectAuth(async (req: NextApiRequest, res: NextApiResponse) => {if (req.method === "GET") {- const { domain, key } = req.query;- const response = (await redis.zcard(`${domain}:clicks:${key}`)) || "0";+ const { domain, key } = req.query as { domain: string; key: string };+ const response = await getLinkClicksCount(domain, key);return res.status(200).json(response);} else {res.setHeader("Allow", ["GET"]);