2022-10-14 13:24:43 -03:00
|
|
|
import Script from "next/script";
|
|
|
|
|
|
|
|
import { getEventTypeAppData } from "@calcom/app-store/utils";
|
2022-11-07 18:12:12 -03:00
|
|
|
|
2022-10-22 06:39:36 -03:00
|
|
|
import { trackingApps } from "./eventTypeAnalytics";
|
2022-10-14 13:24:43 -03:00
|
|
|
|
2022-11-07 18:12:12 -03:00
|
|
|
export type AppScript = { attrs?: Record<string, string> } & (
|
|
|
|
| { src: undefined; content?: string }
|
|
|
|
| { src?: string; content: undefined }
|
|
|
|
);
|
2022-10-14 13:24:43 -03:00
|
|
|
|
|
|
|
export default function BookingPageTagManager({
|
|
|
|
eventType,
|
|
|
|
}: {
|
|
|
|
eventType: Parameters<typeof getEventTypeAppData>[0];
|
|
|
|
}) {
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{Object.entries(trackingApps).map(([appId, scriptConfig]) => {
|
2022-10-22 06:39:36 -03:00
|
|
|
const trackingId = getEventTypeAppData(eventType, appId as keyof typeof trackingApps)?.trackingId;
|
2022-10-14 13:24:43 -03:00
|
|
|
if (!trackingId) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-11-07 18:12:12 -03:00
|
|
|
const parseValue = <T extends string | undefined>(val: T): T =>
|
|
|
|
val ? (val.replace(/\{TRACKING_ID\}/g, trackingId) as T) : val;
|
|
|
|
|
|
|
|
return scriptConfig.scripts.map((script, index) => {
|
2022-10-22 06:39:36 -03:00
|
|
|
const parsedAttributes: NonNullable<AppScript["attrs"]> = {};
|
|
|
|
const attrs = script.attrs || {};
|
|
|
|
Object.entries(attrs).forEach(([name, value]) => {
|
|
|
|
if (typeof value === "string") {
|
2022-11-07 18:12:12 -03:00
|
|
|
value = parseValue(value);
|
2022-10-22 06:39:36 -03:00
|
|
|
}
|
|
|
|
parsedAttributes[name] = value;
|
|
|
|
});
|
2022-11-07 18:12:12 -03:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Script
|
|
|
|
src={parseValue(script.src)}
|
|
|
|
id={`${appId}-${index}`}
|
|
|
|
key={`${appId}-${index}`}
|
|
|
|
dangerouslySetInnerHTML={{
|
|
|
|
__html: parseValue(script.content) || "",
|
|
|
|
}}
|
|
|
|
{...parsedAttributes}
|
|
|
|
defer
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
});
|
2022-10-14 13:24:43 -03:00
|
|
|
})}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|