feat: Dub App integration (#21321)
Co-authored-by: Peer Richelsen <[email protected]> Co-authored-by: sean-brydon <[email protected]>
This commit is contained in:
35 files changed
+518
-2
No files matched your search
@@ -1,4 +1,4 @@
|
||||
import { Dub } from "dub";
|
||||
import { Dub } from "dub-package";
|
||||
|
||||
export const dub = new Dub({
|
||||
token: process.env.DUB_API_KEY,
|
||||
|
||||
@@ -1053,7 +1053,7 @@ export const getOptions = ({
|
||||
dub.track.lead({
|
||||
clickId,
|
||||
eventName: "Sign Up",
|
||||
customerId: user.id.toString(),
|
||||
externalId: user.id.toString(),
|
||||
customerName: user.name,
|
||||
customerEmail: user.email,
|
||||
customerAvatar: user.image,
|
||||
|
||||
@@ -60,6 +60,7 @@ export const mapBookingToMutationInput = ({
|
||||
const _isDryRun = isBookingDryRun(searchParams);
|
||||
const _cacheParam = searchParams?.get("cal.cache");
|
||||
const _shouldServeCache = _cacheParam ? _cacheParam === "true" : undefined;
|
||||
const dub_id = searchParams?.get("dub_id");
|
||||
|
||||
return {
|
||||
...values,
|
||||
@@ -91,6 +92,7 @@ export const mapBookingToMutationInput = ({
|
||||
reroutingFormResponses: reroutingFormResponses ? JSON.parse(reroutingFormResponses) : undefined,
|
||||
_isDryRun,
|
||||
_shouldServeCache,
|
||||
dub_id,
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
@@ -44,6 +44,7 @@ import {
|
||||
} from "@calcom/features/webhooks/lib/scheduleTrigger";
|
||||
import { getVideoCallUrlFromCalEvent } from "@calcom/lib/CalEventParser";
|
||||
import EventManager from "@calcom/lib/EventManager";
|
||||
import { handleAnalyticsEvents } from "@calcom/lib/analyticsManager/handleAnalyticsEvents";
|
||||
import { shouldIgnoreContactOwner } from "@calcom/lib/bookings/routing/utils";
|
||||
import { getUsernameList } from "@calcom/lib/defaultEvents";
|
||||
import {
|
||||
@@ -2160,6 +2161,18 @@ async function handler(
|
||||
loggerWithEventDetails.error("Error while scheduling no show triggers", JSON.stringify({ error }));
|
||||
}
|
||||
|
||||
if (!isDryRun) {
|
||||
await handleAnalyticsEvents({
|
||||
credentials: allCredentials,
|
||||
rawBookingData,
|
||||
bookingInfo: {
|
||||
name: fullName,
|
||||
email: bookerEmail,
|
||||
eventName: "Cal.com lead",
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// TODO: Refactor better so this booking object is not passed
|
||||
// all around and instead the individual fields are sent as args.
|
||||
const bookingResponse = {
|
||||
|
||||
@@ -20,6 +20,7 @@ type TaskPayloads = {
|
||||
createCRMEvent: z.infer<typeof import("./tasks/crm/schema").createCRMEventSchema>;
|
||||
sendWorkflowEmails: z.infer<typeof import("./tasks/sendWorkflowEmails").ZSendWorkflowEmailsSchema>;
|
||||
scanWorkflowBody: z.infer<typeof import("./tasks/scanWorkflowBody").scanWorkflowBodySchema>;
|
||||
sendAnalyticsEvent: z.infer<typeof import("./tasks/analytics/schema").sendAnalyticsEventSchema>;
|
||||
};
|
||||
export type TaskTypes = keyof TaskPayloads;
|
||||
export type TaskHandler = (payload: string) => Promise<void>;
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
import z from "zod";
|
||||
|
||||
export const sendAnalyticsEventSchema = z.object({
|
||||
credentialId: z.number(),
|
||||
info: z.object({
|
||||
name: z.string(),
|
||||
email: z.string(),
|
||||
id: z.string(),
|
||||
eventName: z.string(),
|
||||
externalId: z.string().optional(),
|
||||
}),
|
||||
});
|
||||
@@ -0,0 +1,37 @@
|
||||
import AnalyticsManager from "@calcom/lib/analyticsManager/analyticsManager";
|
||||
import logger from "@calcom/lib/logger";
|
||||
import { safeStringify } from "@calcom/lib/safeStringify";
|
||||
import { CredentialRepository } from "@calcom/lib/server/repository/credential";
|
||||
|
||||
import { sendAnalyticsEventSchema } from "./schema";
|
||||
|
||||
const log = logger.getSubLogger({ prefix: [`[[tasker] sendAnalyticsEvent]`] });
|
||||
|
||||
export async function sendAnalyticsEvent(payload: string): Promise<void> {
|
||||
try {
|
||||
const parsedPayload = sendAnalyticsEventSchema.safeParse(JSON.parse(payload));
|
||||
if (!parsedPayload.success) {
|
||||
throw new Error(`malformed payload in sendAnalyticsEvent: ${parsedPayload.error}`);
|
||||
}
|
||||
|
||||
const { credentialId, info } = parsedPayload.data;
|
||||
const credential = await CredentialRepository.findFirstByIdWithKeyAndUser({ id: credentialId });
|
||||
|
||||
if (!credential) {
|
||||
throw new Error("Invalid credential");
|
||||
}
|
||||
const manager = new AnalyticsManager(credential);
|
||||
|
||||
if (!manager) return;
|
||||
|
||||
await manager.sendEvent(info);
|
||||
} catch (err) {
|
||||
log.error(
|
||||
`[Will retry] Error creating analytics event: error: ${safeStringify(err)} payload: ${safeStringify({
|
||||
payload,
|
||||
})}`
|
||||
);
|
||||
// Intentional rethrow to trigger retry
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
@@ -24,6 +24,8 @@ const tasks: Record<TaskTypes, () => Promise<TaskHandler>> = {
|
||||
createCRMEvent: () => import("./crm/createCRMEvent").then((module) => module.createCRMEvent),
|
||||
sendWorkflowEmails: () => import("./sendWorkflowEmails").then((module) => module.sendWorkflowEmails),
|
||||
scanWorkflowBody: () => import("./scanWorkflowBody").then((module) => module.scanWorkflowBody),
|
||||
sendAnalyticsEvent: () =>
|
||||
import("./analytics/sendAnalyticsEvent").then((module) => module.sendAnalyticsEvent),
|
||||
};
|
||||
|
||||
export const tasksConfig = {
|
||||
|
||||
Reference in new issue
Block a user