Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat: Add Dub server side

Richie McIlroyc5943ebf7999
diff --git a/packages/database/auth/auth-options.tsx b/packages/database/auth/auth-options.tsx
index 4bc6a208d..404572020 100644
--- a/packages/database/auth/auth-options.tsx
+++ b/packages/database/auth/auth-options.tsx
@@ -4,9 +4,11 @@ import type { NextAuthOptions } from "next-auth";
import EmailProvider from "next-auth/providers/email";
import GoogleProvider from "next-auth/providers/google";
import WorkOSProvider from "next-auth/providers/workos";
-import { NODE_ENV, serverEnv } from "@cap/env";
+import { serverEnv } from "@cap/env";
import type { Adapter } from "next-auth/adapters";
import type { Provider } from "next-auth/providers/index";
+import { cookies } from "next/headers";
+import { dub } from "../dub";
import { db } from "../";
import { users, organizations, organizationMembers } from "../schema";
@@ -103,6 +105,30 @@ export const authOptions = (): NextAuthOptions => {
events: {
async signIn({ user, account, isNewUser }) {
if (isNewUser) {
+ // Check if dub_id cookie is present
+ const dubId = cookies().get("dub_id")?.value;
+
+ if (dubId) {
+ try {
+ // Send lead event to Dub
+ await dub().track.lead({
+ clickId: dubId,
+ eventName: "Sign Up",
+ externalId: user.id,
+ customerName: user.name || undefined,
+ customerEmail: user.email || undefined,
+ customerAvatar: user.image || undefined,
+ });
+
+ // Delete the dub_id cookie
+ cookies().set("dub_id", "", {
+ expires: new Date(0),
+ });
+ } catch (error) {
+ console.error("Failed to track lead with Dub:", error);
+ }
+ }
+
const organizationId = nanoId();
await db().insert(organizations).values({
diff --git a/packages/database/dub.ts b/packages/database/dub.ts
new file mode 100644
index 000000000..a18682e77
--- /dev/null
+++ b/packages/database/dub.ts
@@ -0,0 +1,7 @@
+import { Dub } from "dub";
+import { serverEnv } from "@cap/env";
+
+export const dub = () =>
+ new Dub({
+ token: serverEnv().DUB_API_KEY,
+ });
\ No newline at end of file
diff --git a/packages/database/package.json b/packages/database/package.json
index a0191ab31..9cef7b718 100644
--- a/packages/database/package.json
+++ b/packages/database/package.json
@@ -22,6 +22,7 @@
"@react-email/render": "1.1.2",
"@react-email/tailwind": "^1.0.5",
"drizzle-orm": "0.43.1",
+ "dub": "^0.33.1",
"nanoid": "^5.0.4",
"next": "14.2.9",
"next-auth": "^4.24.5",
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index eccb528d4..cec0ff421 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -807,6 +807,9 @@ importers:
drizzle-orm:
specifier: 0.43.1
version: 0.43.1(@cloudflare/[email protected])(@opentelemetry/[email protected])(@planetscale/[email protected])([email protected])
+ dub:
+ specifier: ^0.33.1
+ version: 0.33.1([email protected])
nanoid:
specifier: ^5.0.4
version: 5.1.5
@@ -20870,6 +20873,10 @@ snapshots:
dependencies:
zod: 3.25.20
+ dependencies:
+ zod: 3.25.67
+
dependencies:
call-bind-apply-helpers: 1.0.2
@@ -21300,7 +21307,7 @@ snapshots:
eslint: 8.57.1
eslint-import-resolver-node: 0.3.9
eslint-import-resolver-typescript: 3.10.1([email protected])([email protected])
- eslint-plugin-import: 2.31.0(@typescript-eslint/[email protected]([email protected])([email protected]))([email protected])
eslint-plugin-jsx-a11y: 6.10.2([email protected])
eslint-plugin-react: 7.37.5([email protected])
eslint-plugin-react-hooks: 4.6.2([email protected])
@@ -21377,17 +21384,6 @@ snapshots:
transitivePeerDependencies:
- supports-color
- dependencies:
- debug: 3.2.7
- optionalDependencies:
- '@typescript-eslint/parser': 5.62.0([email protected])([email protected])
- eslint: 8.57.1
- eslint-import-resolver-node: 0.3.9
- eslint-import-resolver-typescript: 3.10.1([email protected]([email protected]))([email protected])
- transitivePeerDependencies:
- - supports-color
-
dependencies:
debug: 3.2.7
@@ -21411,35 +21407,6 @@ snapshots:
- supports-color
- dependencies:
- '@rtsao/scc': 1.1.0
- array-includes: 3.1.8
- array.prototype.findlastindex: 1.2.6
- array.prototype.flat: 1.3.3
- array.prototype.flatmap: 1.3.3
- debug: 3.2.7
- doctrine: 2.1.0
- eslint: 8.57.1
- eslint-import-resolver-node: 0.3.9
- hasown: 2.0.2
- is-core-module: 2.16.1
- is-glob: 4.0.3
- minimatch: 3.1.2
- object.fromentries: 2.0.8
- object.groupby: 1.0.3
- object.values: 1.2.1
- semver: 6.3.1
- string.prototype.trimend: 1.0.9
- tsconfig-paths: 3.15.0
- optionalDependencies:
- '@typescript-eslint/parser': 5.62.0([email protected])([email protected])
- transitivePeerDependencies:
- - eslint-import-resolver-typescript
- - eslint-import-resolver-webpack
- - supports-color
-
dependencies:
'@rtsao/scc': 1.1.0
array-includes: 3.1.8