2023-02-16 22:39:57 +00:00
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
|
|
|
import type { CollectOpts, EventHandler } from "next-collect";
|
2022-06-02 16:19:01 +00:00
|
|
|
import { useCollector } from "next-collect/client";
|
2022-08-24 20:18:42 +00:00
|
|
|
// Importing types so we're not directly importing next/server
|
2022-06-02 16:19:01 +00:00
|
|
|
import type { NextRequest, NextResponse } from "next/server";
|
2021-04-27 14:19:12 +00:00
|
|
|
|
2022-07-28 19:58:26 +00:00
|
|
|
import { CONSOLE_URL } from "./constants";
|
2022-06-03 18:33:53 +00:00
|
|
|
|
2021-05-07 17:05:33 +00:00
|
|
|
export const telemetryEventTypes = {
|
2021-08-19 12:27:01 +00:00
|
|
|
pageView: "page_view",
|
2022-06-02 16:19:01 +00:00
|
|
|
apiCall: "api_call",
|
2021-08-19 12:27:01 +00:00
|
|
|
bookingConfirmed: "booking_confirmed",
|
|
|
|
bookingCancelled: "booking_cancelled",
|
2022-01-19 17:26:44 +00:00
|
|
|
importSubmitted: "import_submitted",
|
2022-05-17 16:22:57 +00:00
|
|
|
login: "login",
|
2022-05-11 05:14:08 +00:00
|
|
|
embedView: "embed_view",
|
|
|
|
embedBookingConfirmed: "embed_booking_confirmed",
|
2023-02-10 09:53:03 +00:00
|
|
|
onboardingFinished: "onboarding_finished",
|
|
|
|
onboardingStarted: "onboarding_started",
|
|
|
|
signup: "signup",
|
|
|
|
team_created: "team_created",
|
|
|
|
website: {
|
|
|
|
pageView: "website_page_view",
|
|
|
|
},
|
2021-08-19 12:27:01 +00:00
|
|
|
};
|
2021-04-27 14:19:12 +00:00
|
|
|
|
2022-05-17 20:43:27 +00:00
|
|
|
export function collectPageParameters(
|
|
|
|
route?: string,
|
|
|
|
extraData: Record<string, unknown> = {}
|
|
|
|
): Record<string, unknown> {
|
2022-06-02 16:19:01 +00:00
|
|
|
const host = document.location.host;
|
2021-08-19 12:27:01 +00:00
|
|
|
const docPath = route ?? "";
|
|
|
|
return {
|
|
|
|
page_url: route,
|
2022-06-02 16:19:01 +00:00
|
|
|
doc_encoding: document.characterSet,
|
2021-08-19 12:27:01 +00:00
|
|
|
url: document.location.protocol + "//" + host + (docPath ?? ""),
|
2022-04-14 02:47:34 +00:00
|
|
|
...extraData,
|
2021-08-19 12:27:01 +00:00
|
|
|
};
|
2021-05-07 17:05:33 +00:00
|
|
|
}
|
|
|
|
|
2022-06-03 18:33:53 +00:00
|
|
|
const reportUsage: EventHandler = async (event, { fetch }) => {
|
2022-06-06 21:33:13 +00:00
|
|
|
const ets = telemetryEventTypes;
|
|
|
|
if ([ets.bookingConfirmed, ets.embedBookingConfirmed].includes(event.eventType)) {
|
2022-06-03 18:33:53 +00:00
|
|
|
const key = process.env.CALCOM_LICENSE_KEY;
|
|
|
|
const url = `${CONSOLE_URL}/api/deployments/usage?key=${key}&quantity=1`;
|
|
|
|
try {
|
|
|
|
return fetch(url, { method: "POST", mode: "cors" });
|
|
|
|
} catch (e) {
|
|
|
|
console.error(`Error reporting booking for key: '${key}'`, e);
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-07-28 19:58:26 +00:00
|
|
|
export const nextCollectBasicSettings: CollectOpts = {
|
2022-06-02 16:19:01 +00:00
|
|
|
drivers: [
|
2022-06-22 20:41:19 +00:00
|
|
|
process.env.CALCOM_LICENSE_KEY && process.env.NEXT_PUBLIC_IS_E2E !== "1" ? reportUsage : undefined,
|
|
|
|
process.env.CALCOM_TELEMETRY_DISABLED === "1" || process.env.NEXT_PUBLIC_IS_E2E === "1"
|
|
|
|
? undefined
|
|
|
|
: {
|
2022-06-02 20:06:13 +00:00
|
|
|
type: "jitsu",
|
|
|
|
opts: {
|
|
|
|
key: "s2s.2pvs2bbpqq1zxna97wcml.esb6cikfrf7yn0qoh1nj1",
|
|
|
|
server: "https://t.calendso.com",
|
|
|
|
},
|
2022-06-22 20:41:19 +00:00
|
|
|
},
|
2022-06-02 16:19:01 +00:00
|
|
|
process.env.TELEMETRY_DEBUG && { type: "echo", opts: { disableColor: true } },
|
|
|
|
],
|
|
|
|
eventTypes: [
|
|
|
|
{ "*.ttf": null },
|
|
|
|
{ "*.webmanifest": null },
|
|
|
|
{ "*.json": null },
|
|
|
|
{ "*.svg": null },
|
|
|
|
{ "*.map": null },
|
|
|
|
{ "*.png": null },
|
|
|
|
{ "*.gif": null },
|
|
|
|
{ "/api/collect-events": null },
|
|
|
|
{ "/api*": null },
|
|
|
|
{ "/img*": null },
|
|
|
|
{ "/favicon*": null },
|
|
|
|
{ "/*": telemetryEventTypes.pageView },
|
|
|
|
],
|
|
|
|
};
|
2021-04-27 14:19:12 +00:00
|
|
|
|
2022-06-02 16:19:01 +00:00
|
|
|
export const extendEventData = (
|
|
|
|
req: NextRequest | NextApiRequest,
|
|
|
|
res: NextResponse | NextApiResponse,
|
|
|
|
original: any
|
|
|
|
) => {
|
|
|
|
const onVercel =
|
|
|
|
typeof req.headers?.get === "function"
|
|
|
|
? !!req.headers.get("x-vercel-id")
|
|
|
|
: !!(req.headers as any)?.["x-vercel-id"];
|
2022-08-17 17:38:21 +00:00
|
|
|
const pageUrl = original?.page_url || req.url || undefined;
|
2022-07-12 17:43:53 +00:00
|
|
|
const cookies = req.cookies as { [key: string]: any };
|
2022-06-02 16:19:01 +00:00
|
|
|
return {
|
|
|
|
title: "",
|
|
|
|
ipAddress: "",
|
|
|
|
queryString: "",
|
|
|
|
page_url: pageUrl,
|
2022-06-02 17:53:02 +00:00
|
|
|
licensekey: process.env.CALCOM_LICENSE_KEY,
|
2022-06-02 16:19:01 +00:00
|
|
|
isTeamBooking:
|
|
|
|
original?.isTeamBooking === undefined
|
|
|
|
? pageUrl?.includes("team/") || undefined
|
|
|
|
: original?.isTeamBooking,
|
|
|
|
referrer: "",
|
|
|
|
onVercel,
|
2022-07-12 17:43:53 +00:00
|
|
|
isAuthorized: !!cookies["next-auth.session-token"] || !!cookies["__Secure-next-auth.session-token"],
|
2022-06-02 16:19:01 +00:00
|
|
|
utc_time: new Date().toISOString(),
|
|
|
|
};
|
|
|
|
};
|
2021-04-27 14:19:12 +00:00
|
|
|
|
2022-06-02 16:19:01 +00:00
|
|
|
export const useTelemetry = useCollector;
|