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.

Added usage summary emails to cron

Steven Tey59854d0824c7
diff --git a/emails/clicks-summary.tsx b/emails/clicks-summary.tsx
index c23f34fca8..a71a102479 100644
--- a/emails/clicks-summary.tsx
+++ b/emails/clicks-summary.tsx
@@ -24,7 +24,7 @@ export default function ClicksSummary({
projectSlug = "acme",
totalClicks = 63689,
createdLinks = 25,
- bestPerformingLinks = [
+ topLinks = [
{
link: "acme.com/sales",
clicks: 2187,
@@ -52,7 +52,7 @@ export default function ClicksSummary({
projectSlug: string;
totalClicks: number;
createdLinks: number;
- bestPerformingLinks: {
+ topLinks: {
link: string;
clicks: number;
}[];
@@ -103,34 +103,38 @@ export default function ClicksSummary({
</Column>
</Row>
</Section>
- <Text className="text-sm leading-6 text-black">
- Here are your top 5 best performing links:
- </Text>
- <Section>
- <Row className="pb-2">
- <Column align="left" className="text-sm text-gray-500">
- Link
- </Column>
- <Column align="right" className="text-sm text-gray-500">
- Clicks
- </Column>
- </Row>
- {bestPerformingLinks.map(({ link, clicks }, index) => (
- <div key={index}>
- <Row>
- <Column align="left" className="text-sm font-medium">
- {truncate(link, 30)}
+ {topLinks.length > 0 && (
+ <>
+ <Text className="text-sm leading-6 text-black">
+ Here are your top {topLinks.length} best performing links:
+ </Text>
+ <Section>
+ <Row className="pb-2">
+ <Column align="left" className="text-sm text-gray-500">
+ Link
</Column>
- <Column align="right" className="text-sm text-gray-600">
- {nFormatter(clicks)}
+ <Column align="right" className="text-sm text-gray-500">
+ Clicks
</Column>
</Row>
- {index !== bestPerformingLinks.length - 1 && (
- <Hr className="my-2 w-full border border-gray-200" />
- )}
- </div>
- ))}
- </Section>
+ {topLinks.map(({ link, clicks }, index) => (
+ <div key={index}>
+ <Row>
+ <Column align="left" className="text-sm font-medium">
+ {truncate(link, 30)}
+ </Column>
+ <Column align="right" className="text-sm text-gray-600">
+ {nFormatter(clicks)}
+ </Column>
+ </Row>
+ {index !== topLinks.length - 1 && (
+ <Hr className="my-2 w-full border border-gray-200" />
+ )}
+ </div>
+ ))}
+ </Section>
+ </>
+ )}
<Text className="mt-10 text-sm leading-6 text-black">
You can view your full stats by clicking the button below.
</Text>
diff --git a/lib/cron/usage.tsx b/lib/cron/usage.tsx
index ca628811e2..fb450832e7 100644
--- a/lib/cron/usage.tsx
+++ b/lib/cron/usage.tsx
@@ -2,7 +2,9 @@ import { sendEmail } from "emails";
import UsageExceeded from "emails/usage-exceeded";
import prisma from "#/lib/prisma";
import { log } from "#/lib/utils";
-import { ProjectProps } from "../types";
+import { ProjectProps } from "#/lib/types";
+import { getTopLinks } from "#/lib/tinybird";
+import ClicksSummary from "emails/clicks-summary";
export const updateUsage = async () => {
const projects = await prisma.project.findMany({
@@ -29,6 +31,11 @@ export const updateUsage = async () => {
user: true,
},
},
+ domains: {
+ where: {
+ verified: true,
+ },
+ },
sentEmails: true,
},
});
@@ -85,6 +92,41 @@ export const updateUsage = async () => {
// TODO: Monthly summary emails (total clicks, best performing links, etc.)
const resetBillingResponse = await Promise.allSettled(
billingReset.map(async (project) => {
+ const [createdLinks, topLinks] = await Promise.allSettled([
+ prisma.link.count({
+ where: {
+ project: {
+ id: project.id,
+ },
+ createdAt: {
+ // larger than billingCycleStart (but a month ago)
+ gte: new Date(
+ new Date().setDate(
+ project.billingCycleStart || new Date().getDate() - 30,
+ ),
+ ),
+ },
+ },
+ }),
+ getTopLinks(project.domains.map((domain) => domain.slug)),
+ ]);
+
+ const email = project.users[0].user.email as string;
+
+ await sendEmail({
+ subject: `Your 30-day Dub summary`,
+ email,
+ react: ClicksSummary({
+ email,
+ projectName: project.name,
+ projectSlug: project.slug,
+ totalClicks: project.usage,
+ createdLinks:
+ createdLinks.status === "fulfilled" ? createdLinks.value : 0,
+ topLinks: topLinks.status === "fulfilled" ? topLinks.value : [],
+ }),
+ });
+
return await prisma.project.update({
where: {
id: project.id,
diff --git a/lib/tinybird.ts b/lib/tinybird.ts
index 665544be73..d869b39a99 100644
--- a/lib/tinybird.ts
+++ b/lib/tinybird.ts
@@ -67,3 +67,23 @@ export async function recordClick(
: []),
]);
}
+
+export async function getTopLinks(domains: string[]) {
+ return await fetch(
+ `https://api.us-east.tinybird.co/v0/pipes/top_links.json?domains=${domains.join(
+ ",",
+ )}`,
+ {
+ headers: {
+ Authorization: `Bearer ${process.env.TINYBIRD_API_KEY}`,
+ },
+ },
+ )
+ .then((res) => res.json())
+ .then(({ data }) =>
+ data.map((link: { domain: string; key: string; clicks: number }) => ({
+ link: `${link.domain}/${link.key}`,
+ clicks: link.clicks,
+ })),
+ );
+}