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.

Dogfood `dub` SDK

Steven Tey29d531efe580
diff --git a/apps/web/app/api/workspaces/[idOrSlug]/billing/upgrade/route.ts b/apps/web/app/api/workspaces/[idOrSlug]/billing/upgrade/route.ts
index 7e207fa290..efe3894219 100644
--- a/apps/web/app/api/workspaces/[idOrSlug]/billing/upgrade/route.ts
+++ b/apps/web/app/api/workspaces/[idOrSlug]/billing/upgrade/route.ts
@@ -69,6 +69,9 @@ export const POST = withWorkspace(async ({ req, workspace, session }) => {
mode: "subscription",
allow_promotion_codes: true,
client_reference_id: workspace.id,
+ metadata: {
+ dubCustomerId: session.user.id,
+ },
});
return NextResponse.json(stripeSession);
diff --git a/apps/web/lib/auth/options.ts b/apps/web/lib/auth/options.ts
index eff6e490b4..f1c92e9fac 100644
--- a/apps/web/lib/auth/options.ts
+++ b/apps/web/lib/auth/options.ts
@@ -12,6 +12,8 @@ import CredentialsProvider from "next-auth/providers/credentials";
import EmailProvider from "next-auth/providers/email";
import GithubProvider from "next-auth/providers/github";
import GoogleProvider from "next-auth/providers/google";
+import { cookies } from "next/headers";
+import { dub } from "../dub";
import { subscribe } from "../flodesk";
import { isStored, storage } from "../storage";
import { UserProps } from "../types";
@@ -321,14 +323,20 @@ export const authOptions: NextAuthOptions = {
const user = await prisma.user.findUnique({
where: { email },
select: {
+ id: true,
name: true,
+ email: true,
+ image: true,
createdAt: true,
},
});
+ if (!user) {
+ return;
+ }
// only send the welcome email if the user was created in the last 10s
// (this is a workaround because the `isNewUser` flag is triggered when a user does `dangerousEmailAccountLinking`)
if (
- user?.createdAt &&
+ user.createdAt &&
new Date(user.createdAt).getTime() > Date.now() - 10000 &&
process.env.NEXT_PUBLIC_IS_DUB
) {
@@ -345,6 +353,24 @@ export const authOptions: NextAuthOptions = {
}),
]);
}
+
+ const clickId = cookies().get("dclid")?.value;
+ if (clickId) {
+ // send lead event to Dub
+ await dub.track.lead({
+ clickId: clickId,
+ eventName: "Sign Up",
+ customerId: user.id,
+ customerName: user.name,
+ customerEmail: user.email,
+ customerAvatar: user.image,
+ });
+ // delete the clickId cookie
+ cookies().set("dclid", "", {
+ expires: new Date(0),
+ path: "/",
+ });
+ }
}
// lazily backup user avatar to R2
const currentImage = message.user.image;
diff --git a/apps/web/lib/dub.ts b/apps/web/lib/dub.ts
new file mode 100644
index 0000000000..643d6b0ea6
--- /dev/null
+++ b/apps/web/lib/dub.ts
@@ -0,0 +1,5 @@
+import { Dub } from "dub";
+
+export const dub = new Dub({
+ workspaceId: "ws_cl7pj5kq4006835rbjlt2ofka",
+});
diff --git a/apps/web/package.json b/apps/web/package.json
index 17aa83022a..bc470f9f45 100644
--- a/apps/web/package.json
+++ b/apps/web/package.json
@@ -57,6 +57,7 @@
"concurrently": "^8.0.1",
"d3-array": "^3.2.4",
"date-fns": "^3.6.0",
+ "dub": "^0.29.8",
"framer-motion": "^10.16.16",
"fuse.js": "^6.6.2",
"github-slugger": "^2.0.0",
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 062a6814fa..6db54a582d 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -169,6 +169,9 @@ importers:
date-fns:
specifier: ^3.6.0
version: 3.6.0
+ dub:
+ specifier: ^0.29.8
+ version: 0.29.8([email protected])
framer-motion:
specifier: ^10.16.16
@@ -10755,6 +10758,14 @@ packages:
resolution: {integrity: sha512-IPzF4w4/Rd94bA9imS68tZBaYyBWSCE47V1RGuMrB94iyTOIEwRmVL2x/4An+6mETpLrKJ5hQkB8W4kFAadeIQ==}
engines: {node: '>=12'}
+ resolution: {integrity: sha512-dS9PIWesNYIfPasTR5g75FOz7ItXnKPjLp75Phd3IZYSm7apXwCQURdT1L+fkKQVLoIgR6lYus67a8pxHpRHgg==}
+ peerDependencies:
+ zod: '>= 3'
+ dependencies:
+ zod: 3.22.4
+ dev: false
+
resolution: {integrity: sha512-I88TYZWc9XiYHRQ4/3c5rjjfgkjhLyW2luGIheGERbNQ6OY7yTybanSpDXZa8y7VUP9YmDcYa+eyq4ca7iLqWA==}
dev: false
@@ -17098,6 +17109,7 @@ packages:
resolution: {integrity: sha512-mwqeW5XsA2qAejG46gYdENaxXjx9onRNCfn7L0duuP4hCuTIi/QO7PDK07KJfp1d+izWPrzEJDcSqBa0OZQriA==}
+ deprecated: Rimraf versions prior to v4 are no longer supported
hasBin: true
dependencies:
glob: 7.2.3