feat: add utm tracking on booking (#19965)
* add tracking to booking + pass to handleNewBooking * save tracking to booking * return undefined if no utm params * add test for handleNewBooking * fix type error in test * refactor code * code clean up * code clean up * add missing import * fix type import * add utm params to insights (#20063) * add utm params to insights * remove duplicates * remove duplicate columns --------- Co-authored-by: CarinaWolli <[email protected]> --------- Co-authored-by: CarinaWolli <[email protected]> Co-authored-by: Omar López <[email protected]>
This commit is contained in:
16 files changed
+354
-4
No files matched your search
@@ -7,6 +7,7 @@ import { parseRecurringDates } from "@calcom/lib/parse-dates";
|
||||
import type { RoutingFormSearchParams } from "@calcom/platform-types";
|
||||
|
||||
import type { BookerEvent, BookingCreateBody, RecurringBookingCreateBody } from "../../types";
|
||||
import type { Tracking } from "../handleNewBooking/types";
|
||||
|
||||
export type BookingOptions = {
|
||||
values: Record<string, unknown>;
|
||||
@@ -99,7 +100,8 @@ export const mapBookingToMutationInput = ({
|
||||
// Other than that it forwards the mapping to mapBookingToMutationInput.
|
||||
export const mapRecurringBookingToMutationInput = (
|
||||
booking: BookingOptions,
|
||||
recurringCount: number
|
||||
recurringCount: number,
|
||||
tracking?: Tracking
|
||||
): RecurringBookingCreateBody[] => {
|
||||
const recurringEventId = uuidv4();
|
||||
const [, recurringDates] = parseRecurringDates(
|
||||
@@ -124,5 +126,6 @@ export const mapRecurringBookingToMutationInput = (
|
||||
recurringEventId,
|
||||
schedulingType: booking.event.schedulingType || undefined,
|
||||
recurringCount: recurringDates.length,
|
||||
tracking,
|
||||
}));
|
||||
};
|
||||
@@ -1204,6 +1204,7 @@ async function handler(
|
||||
evt,
|
||||
originalRescheduledBooking,
|
||||
creationSource: req.body.creationSource,
|
||||
tracking: reqBody.tracking,
|
||||
});
|
||||
|
||||
if (booking?.userId) {
|
||||
|
||||
@@ -15,7 +15,7 @@ import type { AwaitedBookingData, EventTypeId } from "./getBookingData";
|
||||
import type { NewBookingEventType } from "./getEventTypesFromDB";
|
||||
import type { LoadedUsers } from "./loadUsers";
|
||||
import type { OriginalRescheduledBooking } from "./originalRescheduledBookingUtils";
|
||||
import type { PaymentAppData } from "./types";
|
||||
import type { PaymentAppData, Tracking } from "./types";
|
||||
|
||||
type ReqBodyWithEnd = TgetBookingDataSchema & { end: string };
|
||||
|
||||
@@ -50,6 +50,7 @@ type CreateBookingParams = {
|
||||
evt: CalendarEvent;
|
||||
originalRescheduledBooking: OriginalRescheduledBooking;
|
||||
creationSource?: CreationSource;
|
||||
tracking?: Tracking;
|
||||
};
|
||||
|
||||
function updateEventDetails(
|
||||
@@ -85,6 +86,7 @@ export async function createBooking({
|
||||
reroutingFormResponses,
|
||||
rescheduledBy,
|
||||
creationSource,
|
||||
tracking,
|
||||
}: CreateBookingParams & { rescheduledBy: string | undefined }) {
|
||||
updateEventDetails(evt, originalRescheduledBooking, input.changedOrganizer);
|
||||
const associatedBookingForFormResponse = routingFormResponseId
|
||||
@@ -102,6 +104,7 @@ export async function createBooking({
|
||||
evt,
|
||||
originalRescheduledBooking,
|
||||
creationSource,
|
||||
tracking,
|
||||
});
|
||||
|
||||
return await saveBooking(
|
||||
@@ -213,6 +216,7 @@ function buildNewBookingData(params: CreateBookingParams) {
|
||||
reroutingFormResponses,
|
||||
rescheduledBy,
|
||||
creationSource,
|
||||
tracking,
|
||||
} = params;
|
||||
|
||||
const attendeesData = getAttendeesData(evt);
|
||||
@@ -261,6 +265,7 @@ function buildNewBookingData(params: CreateBookingParams) {
|
||||
? { connect: { id: routingFormResponseId } }
|
||||
: undefined,
|
||||
creationSource,
|
||||
tracking: tracking ? { create: tracking } : undefined,
|
||||
};
|
||||
|
||||
if (reqBody.recurringEventId) {
|
||||
|
||||
@@ -43,6 +43,7 @@ import {
|
||||
expectBrokenIntegrationEmails,
|
||||
expectSuccessfulCalendarEventCreationInCalendar,
|
||||
expectICalUIDAsString,
|
||||
expectBookingTrackingToBeInDatabase,
|
||||
} from "@calcom/web/test/utils/bookingScenario/expects";
|
||||
import { getMockRequestDataForBooking } from "@calcom/web/test/utils/bookingScenario/getMockRequestDataForBooking";
|
||||
import { setupAndTeardown } from "@calcom/web/test/utils/bookingScenario/setupAndTeardown";
|
||||
@@ -1550,6 +1551,92 @@ describe("handleNewBooking", () => {
|
||||
);
|
||||
});
|
||||
|
||||
describe("UTM tracking tests", () => {
|
||||
test(
|
||||
"should create a booking with UTM tracking",
|
||||
async ({ emails }) => {
|
||||
const handleNewBooking = (await import("@calcom/features/bookings/lib/handleNewBooking")).default;
|
||||
const booker = getBooker({
|
||||
email: "[email protected]",
|
||||
name: "Booker",
|
||||
});
|
||||
|
||||
const organizer = getOrganizer({
|
||||
name: "Organizer",
|
||||
email: "[email protected]",
|
||||
id: 101,
|
||||
schedules: [TestData.schedules.IstWorkHours],
|
||||
credentials: [getZoomAppCredential()],
|
||||
selectedCalendars: [TestData.selectedCalendars.google],
|
||||
});
|
||||
|
||||
const tracking = {
|
||||
utm_source: "source test",
|
||||
utm_medium: "medium test",
|
||||
utm_campaign: "campaign test",
|
||||
utm_term: "term test",
|
||||
utm_content: "content test",
|
||||
};
|
||||
|
||||
const { req } = createMockNextJsRequest({
|
||||
method: "POST",
|
||||
body: getMockRequestDataForBooking({
|
||||
data: {
|
||||
eventTypeId: 1,
|
||||
responses: {
|
||||
email: booker.email,
|
||||
name: booker.name,
|
||||
},
|
||||
tracking,
|
||||
},
|
||||
}),
|
||||
});
|
||||
|
||||
const scenarioData = getScenarioData({
|
||||
webhooks: [
|
||||
{
|
||||
userId: organizer.id,
|
||||
eventTriggers: ["BOOKING_CREATED"],
|
||||
subscriberUrl: "http://my-webhook.example.com",
|
||||
active: true,
|
||||
eventTypeId: 1,
|
||||
appId: null,
|
||||
},
|
||||
],
|
||||
eventTypes: [
|
||||
{
|
||||
id: 1,
|
||||
slotInterval: 30,
|
||||
length: 30,
|
||||
users: [
|
||||
{
|
||||
id: 101,
|
||||
},
|
||||
],
|
||||
locations: [
|
||||
{
|
||||
type: BookingLocations.ZoomVideo,
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
organizer,
|
||||
apps: [TestData.apps["zoomvideo"]],
|
||||
});
|
||||
|
||||
mockSuccessfulVideoMeetingCreation({
|
||||
metadataLookupKey: "zoomvideo",
|
||||
});
|
||||
await createBookingScenario(scenarioData);
|
||||
const createdBooking = await handleNewBooking(req);
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
||||
expectBookingTrackingToBeInDatabase(tracking, createdBooking.uid!);
|
||||
},
|
||||
timeout
|
||||
);
|
||||
});
|
||||
|
||||
describe("Creation source tests", () => {
|
||||
test(
|
||||
`should create a booking with creation source as WEBAPP`,
|
||||
|
||||
@@ -43,3 +43,11 @@ export type IsFixedAwareUser = User & {
|
||||
};
|
||||
|
||||
export type { PaymentAppData };
|
||||
|
||||
export type Tracking = {
|
||||
utm_source?: string;
|
||||
utm_medium?: string;
|
||||
utm_campaign?: string;
|
||||
utm_term?: string;
|
||||
utm_content?: string;
|
||||
};
|
||||
@@ -84,6 +84,11 @@ export function RoutingFormResponsesTable() {
|
||||
columnVisibility: {
|
||||
formId: false,
|
||||
bookingUserId: false,
|
||||
utm_source: false,
|
||||
utm_medium: false,
|
||||
utm_campaign: false,
|
||||
utm_term: false,
|
||||
utm_content: false,
|
||||
},
|
||||
},
|
||||
getFacetedUniqueValues: getInsightsFacetedUniqueValues,
|
||||
|
||||
@@ -199,6 +199,76 @@ export const useInsightsColumns = ({
|
||||
return <div className="max-w-[250px]">{assignmentReason}</div>;
|
||||
},
|
||||
}),
|
||||
columnHelper.accessor("utm_source", {
|
||||
id: "utm_source",
|
||||
header: t("utm_source"),
|
||||
size: 100,
|
||||
enableColumnFilter: false,
|
||||
enableSorting: false,
|
||||
cell: (info) => {
|
||||
return (
|
||||
<div className="truncate">
|
||||
<span>{info.getValue()}</span>
|
||||
</div>
|
||||
);
|
||||
},
|
||||
}),
|
||||
columnHelper.accessor("utm_medium", {
|
||||
id: "utm_medium",
|
||||
header: "utm_medium",
|
||||
size: 100,
|
||||
enableColumnFilter: false,
|
||||
enableSorting: false,
|
||||
cell: (info) => {
|
||||
return (
|
||||
<div className="truncate">
|
||||
<span>{info.getValue()}</span>
|
||||
</div>
|
||||
);
|
||||
},
|
||||
}),
|
||||
columnHelper.accessor("utm_term", {
|
||||
id: "utm_term",
|
||||
header: "utm_term",
|
||||
size: 100,
|
||||
enableColumnFilter: false,
|
||||
enableSorting: false,
|
||||
cell: (info) => {
|
||||
return (
|
||||
<div className="truncate">
|
||||
<span>{info.getValue()}</span>
|
||||
</div>
|
||||
);
|
||||
},
|
||||
}),
|
||||
columnHelper.accessor("utm_content", {
|
||||
id: "utm_content",
|
||||
header: "utm_content",
|
||||
size: 100,
|
||||
enableColumnFilter: false,
|
||||
enableSorting: false,
|
||||
cell: (info) => {
|
||||
return (
|
||||
<div className="truncate">
|
||||
<span>{info.getValue()}</span>
|
||||
</div>
|
||||
);
|
||||
},
|
||||
}),
|
||||
columnHelper.accessor("utm_campaign", {
|
||||
id: "utm_campaign",
|
||||
header: "utm_campaign",
|
||||
size: 100,
|
||||
enableColumnFilter: false,
|
||||
enableSorting: false,
|
||||
cell: (info) => {
|
||||
return (
|
||||
<div className="truncate">
|
||||
<span>{info.getValue()}</span>
|
||||
</div>
|
||||
);
|
||||
},
|
||||
}),
|
||||
columnHelper.accessor("createdAt", {
|
||||
id: "createdAt",
|
||||
header: t("routing_form_insights_submitted_at"),
|
||||
|
||||
@@ -321,6 +321,11 @@ class RoutingEventsInsights {
|
||||
bookingStartTime: true,
|
||||
bookingEndTime: true,
|
||||
createdAt: true,
|
||||
utm_source: true,
|
||||
utm_medium: true,
|
||||
utm_campaign: true,
|
||||
utm_term: true,
|
||||
utm_content: true,
|
||||
},
|
||||
where: whereClause,
|
||||
orderBy: sorting && sorting.length > 0 ? makeOrderBy(sorting) : { createdAt: "desc" },
|
||||
@@ -426,6 +431,11 @@ class RoutingEventsInsights {
|
||||
"Routed To Name": item.bookingUserName || "",
|
||||
"Routed To Email": item.bookingUserEmail || "",
|
||||
...mapKeys(fields, (_, key) => startCase(key)),
|
||||
utm_source: item.utm_source || "",
|
||||
utm_medium: item.utm_medium || "",
|
||||
utm_campaign: item.utm_campaign || "",
|
||||
utm_term: item.utm_term || "",
|
||||
utm_content: item.utm_content || "",
|
||||
};
|
||||
});
|
||||
|
||||
|
||||
Reference in new issue
Block a user