The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Add all time link stats
Steven Tey85970e5da6d0
diff --git a/app/ui/home/demo.tsx b/app/ui/home/demo.tsxindex 5683cf5f78..d93c058a54 100644--- a/app/ui/home/demo.tsx+++ b/app/ui/home/demo.tsx@@ -52,7 +52,7 @@ const Demo = () => {title="Maximum number of links reached. Swipe to delete existing links orcreate a free account."cta="Start For Free"- ctaLink="https://app.dub.sh/register"+ href="https://app.dub.sh/register"/>}>diff --git a/app/ui/modals/add-edit-link-modal/index.tsx b/app/ui/modals/add-edit-link-modal/index.tsxindex 2b1e416d16..c862ca9c1e 100644--- a/app/ui/modals/add-edit-link-modal/index.tsx+++ b/app/ui/modals/add-edit-link-modal/index.tsx@@ -593,8 +593,8 @@ function AddEditLinkButton({content={<TooltipContenttitle="Your project has exceeded its usage limit. We're still collecting data on your existing links, but you need to upgrade to add more links."- cta="Upgrade"- ctaLink={`/${slug}/settings/billing`}+ cta="Upgrade to Pro"+ href={`/${slug}/settings/billing`}/>}>diff --git a/app/ui/modals/link-qr-modal.tsx b/app/ui/modals/link-qr-modal.tsxindex f8d631ed9e..00f4c272d9 100644--- a/app/ui/modals/link-qr-modal.tsx+++ b/app/ui/modals/link-qr-modal.tsx@@ -235,7 +235,7 @@ function AdvancedSettings({ qrData, setFgColor, showLogo, setShowLogo }) {<TooltipContenttitle="As a freemium product, we rely on word of mouth to spread the word about Dub. If you'd like to remove the Dub logo/upload your own, please consider upgrading to a Pro plan."cta="Upgrade to Pro"- ctaLink={isApp ? "/settings" : "/#pricing"}+ href={isApp ? "/settings" : "/#pricing"}/>}>diff --git a/app/ui/tooltip.tsx b/app/ui/tooltip.tsxindex 0247968a5d..21c9aaaa09 100644--- a/app/ui/tooltip.tsx+++ b/app/ui/tooltip.tsx@@ -125,23 +125,33 @@ export default function Tooltip({export function TooltipContent({title,cta,- ctaLink,+ href,+ onClick,}: {title: string;cta?: string;- ctaLink?: string;+ href?: string;+ onClick?: () => void;}) {return (<div className="flex max-w-xs flex-col items-center space-y-3 p-5 text-center"><p className="text-sm text-gray-700">{title}</p>- {cta && ctaLink && (- <Link- href={ctaLink}- className="mt-4 rounded-full border border-black bg-black px-3 py-1.5 text-sm text-white transition-all hover:bg-white hover:text-black"- >- {cta}- </Link>- )}+ {cta &&+ (href ? (+ <Link+ href={href}+ className="mt-4 rounded-md border border-black bg-black px-5 py-1.5 text-sm text-white transition-all hover:bg-white hover:text-black"+ >+ {cta}+ </Link>+ ) : onClick ? (+ <button+ className="mt-4 rounded-md border border-black bg-black px-5 py-1.5 text-sm text-white transition-all hover:bg-white hover:text-black"+ onClick={onClick}+ >+ {cta}+ </button>+ ) : null)}</div>);}diff --git a/components/app/links/link-card.tsx b/components/app/links/link-card.tsxindex f71fa3f8df..49fa3c2b46 100644--- a/components/app/links/link-card.tsx+++ b/components/app/links/link-card.tsx@@ -206,7 +206,7 @@ export default function LinkCard({ props }: { props: LinkProps }) {<TooltipContenttitle="Your branded links won't work until you verify your domain."cta="Verify your domain"- ctaLink={`/${slug}/domains`}+ href={`/${slug}/domains`}/>}>@@ -293,8 +293,8 @@ export default function LinkCard({ props }: { props: LinkProps }) {content={<TooltipContenttitle="Your project has exceeded its usage limit. We're still collecting data on your existing links, but you need to upgrade to edit them."- cta="Upgrade"- ctaLink={`/${slug}/settings/billing`}+ cta="Upgrade to Pro"+ href={`/${slug}/settings/billing`}/>}>@@ -330,8 +330,8 @@ export default function LinkCard({ props }: { props: LinkProps }) {content={<TooltipContenttitle="Your project has exceeded its usage limit. We're still collecting data on your existing links, but you need to upgrade to create a new link."- cta="Upgrade"- ctaLink={`/${slug}/settings/billing`}+ cta="Upgrade to Pro"+ href={`/${slug}/settings/billing`}/>}>diff --git a/components/app/modals/add-edit-domain-modal.tsx b/components/app/modals/add-edit-domain-modal.tsxindex a76b667eb6..2a80c93a7d 100644--- a/components/app/modals/add-edit-domain-modal.tsx+++ b/components/app/modals/add-edit-domain-modal.tsx@@ -273,7 +273,7 @@ function AddEditDomainModal({<TooltipContenttitle="You can't configure a custom landing page on a free plan. Upgrade to a Pro plan to proceed."cta="Upgrade to Pro"- ctaLink={`/${slug}/settings/billing`}+ href={`/${slug}/settings/billing`}/>}fullWidthdiff --git a/components/app/modals/add-edit-link-modal/index.tsx b/components/app/modals/add-edit-link-modal/index.tsxindex db7c03ff9f..346d9c1697 100644--- a/components/app/modals/add-edit-link-modal/index.tsx+++ b/components/app/modals/add-edit-link-modal/index.tsx@@ -590,8 +590,8 @@ function AddEditLinkButton({content={<TooltipContenttitle="Your project has exceeded its usage limit. We're still collecting data on your existing links, but you need to upgrade to add more links."- cta="Upgrade"- ctaLink={`/${slug}/settings/billing`}+ cta="Upgrade to Pro"+ href={`/${slug}/settings/billing`}/>}>diff --git a/components/app/modals/link-qr-modal.tsx b/components/app/modals/link-qr-modal.tsxindex 4316776242..8cb2a64ecd 100644--- a/components/app/modals/link-qr-modal.tsx+++ b/components/app/modals/link-qr-modal.tsx@@ -233,7 +233,7 @@ function AdvancedSettings({ qrData, setFgColor, showLogo, setShowLogo }) {<TooltipContenttitle="As a freemium product, we rely on word of mouth to spread the word about Dub. If you'd like to remove the Dub logo/upload your own, please consider upgrading to a Pro plan."cta="Upgrade to Pro"- ctaLink={isApp ? "/settings" : "/#pricing"}+ href={isApp ? "/settings" : "/#pricing"}/>}>diff --git a/components/app/projects/project-card.tsx b/components/app/projects/project-card.tsxindex a6a8c9d74c..ffbf212543 100644--- a/components/app/projects/project-card.tsx+++ b/components/app/projects/project-card.tsx@@ -54,7 +54,7 @@ export default function ProjectCard({title="This domain is not correctly configured. Please configure your domain tostart adding links."cta="Configure Domain"- ctaLink={`/${slug}/domains`}+ href={`/${slug}/domains`}/>}>diff --git a/components/stats/toggle.tsx b/components/stats/toggle.tsxindex 51d3d03470..40d02b78ef 100644--- a/components/stats/toggle.tsx+++ b/components/stats/toggle.tsx@@ -18,11 +18,16 @@ import { fetcher } from "@/lib/utils";import { toast } from "sonner";import Switch from "#/ui/switch";import { StatsContext } from ".";+import useProject from "@/lib/swr/use-project";+import Tooltip, { TooltipContent } from "#/ui/tooltip";+import { ModalContext } from "#/ui/modal-provider";export default function Toggle({ atModalTop }: { atModalTop?: boolean }) {const router = useRouter();+ const { slug: projectSlug } = router.query as { slug?: string };const { basePath, domain, interval, key } = useContext(StatsContext);+ const { setShowAddProjectModal } = useContext(ModalContext);const atTop = useScroll(80) || atModalTop;const [openDatePopover, setOpenDatePopover] = useState(false);@@ -31,6 +36,8 @@ export default function Toggle({ atModalTop }: { atModalTop?: boolean }) {return INTERVALS.find((s) => s.slug === interval) || INTERVALS[1];}, [interval]);+ const { plan } = useProject();+return (<divclassName={`z-10 mb-5 ${@@ -54,29 +61,58 @@ export default function Toggle({ atModalTop }: { atModalTop?: boolean }) {<Popovercontent={<div className="w-full p-2 md:w-48">- {INTERVALS.map(({ display, slug }) => (- <button- key={slug}- onClick={() => {- router.push(- {- query: {- ...router.query,- interval: slug,+ {INTERVALS.map(({ display, slug }) =>+ slug === "eternity" && (!plan || plan === "free") ? (+ <Tooltip+ content={+ <TooltipContent+ title={+ projectSlug+ ? "All-time stats can only be viewed on a Pro plan or higher. Upgrade now to view all-time stats."+ : "All-time stats can only be viewed on a project with a Pro plan. Create a project or navigate to an existing project to upgrade."+ }+ cta={+ projectSlug ? "Upgrade to Pro" : "Create Project"+ }+ {...(projectSlug+ ? { href: `/${projectSlug}/settings/billing` }+ : {+ onClick: () => {+ setShowAddProjectModal(true);+ setOpenDatePopover(false);+ },+ })}+ />+ }+ >+ <div className="flex w-full cursor-not-allowed items-center justify-between space-x-2 rounded-md p-2 text-sm text-gray-400">+ {display}+ </div>+ </Tooltip>+ ) : (+ <button+ key={slug}+ onClick={() => {+ router.push(+ {+ query: {+ ...router.query,+ interval: slug,+ },},- },- `${basePath}?interval=${slug}`,- { shallow: true },- );- }}- className="flex w-full items-center justify-between space-x-2 rounded-md p-2 hover:bg-gray-100 active:bg-gray-200"- >- <p className="text-sm">{display}</p>- {selectedInterval.slug === slug && (- <Tick className="h-4 w-4" aria-hidden="true" />- )}- </button>- ))}+ `${basePath}?interval=${slug}`,+ { shallow: true },+ );+ }}+ className="flex w-full items-center justify-between space-x-2 rounded-md p-2 hover:bg-gray-100 active:bg-gray-200"+ >+ <p className="text-sm">{display}</p>+ {selectedInterval.slug === slug && (+ <Tick className="h-4 w-4" aria-hidden="true" />+ )}+ </button>+ ),+ )}</div>}openPopover={openDatePopover}diff --git a/lib/constants/index.ts b/lib/constants/index.tsindex 3edc450e89..8bcf065b8a 100644--- a/lib/constants/index.ts+++ b/lib/constants/index.ts@@ -28,6 +28,10 @@ export const INTERVALS = [display: "Last 3 months",slug: "90d",},+ {+ display: "All Time",+ slug: "eternity",+ },];export const FRAMER_MOTION_LIST_ITEM_VARIANTS = {diff --git a/lib/cron/usage.tsx b/lib/cron/usage.tsxindex 60b4164330..e64a6308fe 100644--- a/lib/cron/usage.tsx+++ b/lib/cron/usage.tsx@@ -51,7 +51,7 @@ export const updateUsage = async () => {const email = users[0].user.email;await log(- `${name} is over usage limit. Usage: ${usage}, Limit: ${usageLimit}`,+ `${name} is over usage limit. Usage: ${usage}, Limit: ${usageLimit}, Email: ${email}`,"cron",true,);diff --git a/lib/stats.ts b/lib/stats.tsindex d9b18acd15..47ffe6d1a0 100644--- a/lib/stats.ts+++ b/lib/stats.ts@@ -1,6 +1,6 @@import { conn } from "./planetscale";-export type IntervalProps = "1h" | "24h" | "7d" | "30d" | "90d";+export type IntervalProps = "1h" | "24h" | "7d" | "30d" | "90d" | "eternity";export const intervalData = {"1h": {@@ -56,6 +56,17 @@ export const intervalData = {day: "numeric",}),},+ eternity: {+ // difference between now and Sep 22, 2022+ milliseconds: Date.now() - new Date("2022-09-22").getTime(),+ interval: 2629746000,+ granularity: "month",+ format: (e: number) =>+ new Date(e).toLocaleDateString("en-us", {+ month: "short",+ year: "numeric",+ }),+ },};export type LocationTabs = "country" | "city" | "region";@@ -149,6 +160,7 @@ export const getStats = async ({);url.searchParams.append("domain", domain);url.searchParams.append("key", key);+if (interval) {url.searchParams.append("start",@@ -164,7 +176,7 @@ export const getStats = async ({url.searchParams.append("granularity", intervalData[interval].granularity);}- return await fetch(url, {+ return await fetch(url.toString(), {headers: {Authorization: `Bearer ${process.env.TINYBIRD_API_KEY}`,},diff --git a/pages/api/links/[key]/stats/[endpoint].ts b/pages/api/links/[key]/stats/[endpoint].tsindex 28612d60c8..af63fa665f 100644--- a/pages/api/links/[key]/stats/[endpoint].ts+++ b/pages/api/links/[key]/stats/[endpoint].ts@@ -2,7 +2,7 @@ import { withLinksAuth } from "@/lib/auth";import { getStats, IntervalProps } from "@/lib/stats";export default withLinksAuth(- async (req, res, _session, _project, domain) => {+ async (req, res, _session, project, domain) => {// GET /api/links/[key]/stats/[endpoint] - get link stats from Tinybirdif (req.method === "GET") {const { key, endpoint, interval } = req.query as {@@ -10,12 +10,18 @@ export default withLinksAuth(endpoint: string;interval: IntervalProps;};++ if (interval === "eternity" && (!project || project.plan === "free")) {+ return res.status(403).end("Forbidden: Require higher plan");+ }+const response = await getStats({domain: domain || "dub.sh",key,endpoint,interval,});+ if (endpoint === "timeseries") console.log(response);if (!response) {return res.status(405).end(`Method ${req.method} Not Allowed`);diff --git a/tsconfig.json b/tsconfig.jsonindex 1f28e1285a..df65843c25 100644--- a/tsconfig.json+++ b/tsconfig.json@@ -33,5 +33,5 @@]},"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],- "exclude": ["node_modules", "pages/api/scripts"]+ "exclude": ["node_modules"]}