22
33import { useEffect } from 'react'
44import Cal , { getCalApi } from '@calcom/embed-react'
5+ import { isHosted } from '@/lib/core/config/env-flags'
56import type { DemoLead } from '@/app/(landing)/demo/components/demo-form'
67
78/** The Cal.com event the demo books - set `NEXT_PUBLIC_CAL_LINK` to override. */
@@ -14,13 +15,35 @@ const CAL_LINK = process.env.NEXT_PUBLIC_CAL_LINK ?? 'team/sim/demo'
1415 */
1516const CAL_BRAND_COLOR = '#6f3dfa'
1617
18+ /**
19+ * X (Twitter) conversion event fired when a demo is actually booked, so ad
20+ * delivery optimizes toward bookings rather than form submits.
21+ */
22+ const X_DEMO_BOOKED_EVENT_ID = 'tw-q5xbl-q5xbn'
23+
1724interface DemoSchedulerProps {
1825 /** The captured lead used to prefill the Cal.com booking. */
1926 lead : DemoLead
2027}
2128
2229let calEmbedPreloaded = false
2330
31+ /**
32+ * Fires the X conversion once the Cal.com booking is confirmed. There is no
33+ * standalone confirmation page to drop the pixel snippet into — Cal renders the
34+ * "you're booked" state inside its cross-origin iframe — so the embed's
35+ * `bookingSuccessfulV2` event is the confirmation.
36+ *
37+ * Module-scope so the same function identity can be handed to both `on` and
38+ * `off`. `window.twq` is only defined where {@link LandingLayout} renders the
39+ * pixel base code, so the optional call is a second guard for the window
40+ * between mount and `uwt.js` finishing — the stub `twq` queues calls made
41+ * before the script loads and replays them.
42+ */
43+ function trackDemoBooked ( ) : void {
44+ window . twq ?.( 'event' , X_DEMO_BOOKED_EVENT_ID , { } )
45+ }
46+
2447/**
2548 * Warm the Cal.com embed before the scheduler mounts. Loads `embed.js` and
2649 * issues the embed's `preload` instruction, which fetches the booker in a
@@ -55,12 +78,27 @@ export function preloadCalEmbed(): void {
5578 */
5679export function DemoScheduler ( { lead } : DemoSchedulerProps ) {
5780 useEffect ( ( ) => {
58- getCalApi ( { namespace : CAL_NAMESPACE } ) . then ( ( cal ) => {
59- cal ( 'ui' , {
60- hideEventTypeDetails : true ,
61- styles : { branding : { brandColor : CAL_BRAND_COLOR } } ,
81+ let cancelled = false
82+ const api = getCalApi ( { namespace : CAL_NAMESPACE } )
83+ api
84+ . then ( ( cal ) => {
85+ if ( cancelled ) return
86+ cal ( 'ui' , {
87+ hideEventTypeDetails : true ,
88+ styles : { branding : { brandColor : CAL_BRAND_COLOR } } ,
89+ } )
90+ // Matches the layout's pixel gating - a self-hosted deployment loads no
91+ // base pixel, so it must not subscribe an ad-tracking callback either.
92+ if ( isHosted ) cal ( 'on' , { action : 'bookingSuccessfulV2' , callback : trackDemoBooked } )
6293 } )
63- } )
94+ . catch ( ( ) => { } )
95+ return ( ) => {
96+ cancelled = true
97+ if ( ! isHosted ) return
98+ api
99+ . then ( ( cal ) => cal ( 'off' , { action : 'bookingSuccessfulV2' , callback : trackDemoBooked } ) )
100+ . catch ( ( ) => { } )
101+ }
64102 } , [ ] )
65103
66104 return (
0 commit comments