Skip to content

Commit 3b4d587

Browse files
authored
feat(demo): fire the X conversion event when a demo is booked (#6401)
1 parent 1c5393a commit 3b4d587

1 file changed

Lines changed: 43 additions & 5 deletions

File tree

apps/sim/app/(landing)/demo/components/demo-scheduler/demo-scheduler.tsx

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import { useEffect } from 'react'
44
import Cal, { getCalApi } from '@calcom/embed-react'
5+
import { isHosted } from '@/lib/core/config/env-flags'
56
import 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
*/
1516
const 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+
1724
interface DemoSchedulerProps {
1825
/** The captured lead used to prefill the Cal.com booking. */
1926
lead: DemoLead
2027
}
2128

2229
let 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
*/
5679
export 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

Comments
 (0)