11import { useEffect } from 'react' ;
22import { useOnboarding } from '@/hooks/use-onboarding' ;
3+ import { usePostHog } from 'posthog-js/react' ;
34import { OnboardingContainer } from './onboarding-container' ;
45import { OnboardingNavigation } from './onboarding-navigation' ;
56import { useMutation } from '@connectrpc/connect-query' ;
@@ -8,6 +9,7 @@ import { useRouter } from 'next/router';
89import { EnumStatusCode } from '@wundergraph/cosmo-connect/dist/common/common_pb' ;
910import { useToast } from '../ui/use-toast' ;
1011import { SubmitHandler , useZodForm } from '@/hooks/use-form' ;
12+ import { captureOnboardingEvent } from '@/lib/track' ;
1113import { Controller } from 'react-hook-form' ;
1214import { z } from 'zod' ;
1315import { Form } from '../ui/form' ;
@@ -33,10 +35,21 @@ const WhyListItem = ({ title, text }: { title: string; text: string }) => (
3335 </ li >
3436) ;
3537
38+ const normalizeReferrer = ( referrer : string | string [ ] ) : string => {
39+ if ( Array . isArray ( referrer ) ) {
40+ return referrer . join ( ' ' ) ;
41+ }
42+
43+ return referrer ;
44+ } ;
45+
3646export const Step1 = ( ) => {
3747 const router = useRouter ( ) ;
48+ const posthog = usePostHog ( ) ;
3849 const { toast } = useToast ( ) ;
3950 const { setStep, setSkipped, setOnboarding, onboarding } = useOnboarding ( ) ;
51+ // Referrer can be `wgc` when onboarding is opened via `wgc demo` command
52+ const referrer = normalizeReferrer ( router . query . referrer || document . referrer ) ;
4053
4154 const form = useZodForm < OnboardingFormValues > ( {
4255 mode : 'onChange' ,
@@ -63,6 +76,12 @@ export const Step1 = () => {
6376 slack : formValues . channels . slack ,
6477 email : formValues . channels . email ,
6578 } ) ;
79+ captureOnboardingEvent ( posthog , {
80+ name : 'onboarding_step_completed' ,
81+ options : {
82+ step_name : 'welcome' ,
83+ } ,
84+ } ) ;
6685 router . push ( '/onboarding/2' ) ;
6786 } ,
6887 onError : ( error ) => {
@@ -82,7 +101,21 @@ export const Step1 = () => {
82101
83102 useEffect ( ( ) => {
84103 setStep ( 1 ) ;
85- } , [ setStep ] ) ;
104+ } , [ setStep , posthog , referrer ] ) ;
105+
106+ useEffect ( ( ) => {
107+ // We want to trigger the onboarding only for the first time when the onboarding record
108+ // does not exist in the database yet. Users can navigate to this first step as well
109+ // and in that case we want to ignore it.
110+ if ( onboarding ) return ;
111+
112+ captureOnboardingEvent ( posthog , {
113+ name : 'onboarding_started' ,
114+ options : {
115+ entry_source : referrer ,
116+ } ,
117+ } ) ;
118+ } , [ onboarding , referrer , posthog ] ) ;
86119
87120 return (
88121 < OnboardingContainer >
@@ -151,7 +184,15 @@ export const Step1 = () => {
151184 </ div >
152185
153186 < OnboardingNavigation
154- onSkip = { setSkipped }
187+ onSkip = { ( ) => {
188+ captureOnboardingEvent ( posthog , {
189+ name : 'onboarding_skipped' ,
190+ options : {
191+ step_name : 'welcome' ,
192+ } ,
193+ } ) ;
194+ setSkipped ( ) ;
195+ } }
155196 forwardLabel = "Start the tour"
156197 forward = { {
157198 onClick : form . handleSubmit ( onSubmit ) ,
0 commit comments