feat: Implement Dub.co for conversion tracking (#16165)

* FEAT: Implement Dub.co for conversion tracking

* add link creation, @vercel/functions

* use refer.cal.com/:username instead of go.cal.com/r/:username

* sale conversion tracking

* add copy referral link

* limit to IS_CALCOM only

* Update yarn.lock

* fix DubAnalytics

* use workaround for isNewUser

* pass req to getOptions

* Update next-auth-options.ts

* fix ts errors

* only show DubAnalytics outside EU

* add Dub Analytics to /signup

* use WEBSITE_URL instead

* on-demand generate links

* add migration

* add check for existing link + change fetch method to poast

* remove refer.cal.com from PoweredByCal

* limit DubAnalytics to /signup only

* simplify generate-referral-link

* restore yarn.lock

* add yarn with dub sdk in

* add yarn with dub sdk in

* yarn

* Update yarn.lock

---------

Co-authored-by: sean <[email protected]>
Co-authored-by: Peer Richelsen <[email protected]>
This commit is contained in:
authored and GitHub committed 2024-09-12 12:33:10 -07:00
1 parent f4bc7d2ea4
commit cf16cd3ed2
49 files changed
+1548 -120

No files matched your search

+7 -2
View File
@@ -1,5 +1,10 @@
import type { NextApiRequest, NextApiResponse } from "next";
import NextAuth from "next-auth";
import { AUTH_OPTIONS } from "@calcom/features/auth/lib/next-auth-options";
import { getOptions } from "@calcom/features/auth/lib/next-auth-options";
export default NextAuth(AUTH_OPTIONS);
// pass req to NextAuth: https://github.com/nextauthjs/next-auth/discussions/469
const handler = (req: NextApiRequest, res: NextApiResponse) =>
NextAuth(req, res, getOptions({ getDclid: () => req.cookies.dclid }));
export default handler;
@@ -0,0 +1,49 @@
import type { NextApiRequest, NextApiResponse } from "next";
import { dub } from "@calcom/features/auth/lib/dub";
import { getServerSession } from "@calcom/features/auth/lib/getServerSession";
import prisma from "@calcom/prisma";
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
if (req.method !== "POST") {
return res.status(405).json({ message: "Method not allowed" });
}
const session = await getServerSession({ req, res });
if (!session?.user?.username) {
return res.status(401).json({ message: "Unauthorized" });
}
try {
return await dub.links.get({
externalId: `ext_${session.user.id.toString()}`,
});
} catch (error) {
console.log("Referral link not found, creating...");
}
const { id: referralLinkId, shortLink } = await dub.links.create({
domain: "refer.cal.com",
key: session.user.username,
url: "https://cal.com",
externalId: session.user.id.toString(), // @see https://d.to/externalId
trackConversion: true, // enable conversion tracking @see https://d.to/conversions
});
/*
Even though we are relying in externalId for retrieving the referral link,
we still save the referralLinkId in the database for future reference.
E.g. we can use this to tell how many users created a referral link.
*/
await prisma.user.update({
where: {
id: session.user.id,
},
data: {
referralLinkId,
},
});
return res.status(200).json({ shortLink });
}
+8
View File
@@ -1,5 +1,6 @@
"use client";
import { Analytics as DubAnalytics } from "@dub/analytics/react";
import { zodResolver } from "@hookform/resolvers/zod";
import { signIn } from "next-auth/react";
import { Trans } from "next-i18next";
@@ -26,7 +27,9 @@ import {
CLOUDFLARE_SITE_ID,
WEBSITE_PRIVACY_POLICY_URL,
WEBSITE_TERMS_URL,
WEBSITE_URL,
} from "@calcom/lib/constants";
import { isENVDev } from "@calcom/lib/env";
import { fetchUsername } from "@calcom/lib/fetchUsername";
import { pushGTMEvent } from "@calcom/lib/gtm";
import { useCompatSearchParams } from "@calcom/lib/hooks/useCompatSearchParams";
@@ -309,6 +312,11 @@ export default function Signup({
__html: `<iframe src="https://www.googletagmanager.com/ns.html?id=${process.env.NEXT_PUBLIC_GTM_ID}" height="0" width="0" style="display:none;visibility:hidden"></iframe>`,
}}
/>
<DubAnalytics
cookieOptions={{
domain: isENVDev ? undefined : `.${new URL(WEBSITE_URL).hostname}`,
}}
/>
</>
) : null}
<div