Skip to content

Commit

Permalink
fix: use trpc customer-io router to identify
Browse files Browse the repository at this point in the history
  • Loading branch information
zacjones93 committed Jan 21, 2025
1 parent 255c7ca commit dbde5f5
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 22 deletions.
33 changes: 25 additions & 8 deletions src/pages/bootcamp/components/SignUpForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,46 @@ import {useState} from 'react'
import {Button} from './ui/button'
import {Input} from './ui/input'
import {motion} from 'framer-motion'
import {nanoid} from 'nanoid'
import useCio from '@/hooks/use-cio'
import {trpc} from '@/app/_trpc/client'

export default function SignUpForm() {
const [email, setEmail] = useState('')
const [isSubmitting, setIsSubmitting] = useState(false)
const {cioIdentify} = useCio()
const {subscriber} = useCio()
const identify = trpc.customerIO.identify.useMutation({
onSuccess: (data) => {
console.log('IDENTIFY', data)
},
onError: (error) => {
console.log('ERROR', error)
},
})

const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault()

if (isSubmitting) return

setIsSubmitting(true)
const subscriberId = nanoid()
let subscriberId = subscriber?.id

try {
console.log('Submitting email:', email)
await cioIdentify(subscriberId, {
email,
created_at: Math.floor(Date.now() / 1000),
source: 'landing_page_waitlist',
})

const currentDateTime = Math.floor(Date.now() * 0.001) // Customer.io uses seconds with their UNIX epoch timestamps

if (!subscriberId) {
identify.mutateAsync({
email,
selectedInterests: {ai_bootcamp_waitlist: currentDateTime},
})
} else {
identify.mutateAsync({
id: subscriberId,
selectedInterests: {ai_bootcamp_waitlist: currentDateTime},
})
}

setEmail('')
console.log('Successfully submitted email')
Expand Down
38 changes: 24 additions & 14 deletions src/server/routers/customer-io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {getAttributes} from '@/lib/customer-io'
import emailIsValid from '@/utils/email-is-valid'
import {getContactId} from '@/lib/users'
import {ACCESS_TOKEN_KEY} from '@/utils/auth'
import {requestContactGuid} from '@/utils/request-contact-guid'

const {TrackClient, RegionUS} = require('customerio-node')
const siteId = process.env.NEXT_PUBLIC_CUSTOMER_IO_SITE_ID
Expand All @@ -17,9 +18,7 @@ export const customerIORouter = router({
z.object({
email: z.string().optional(),
id: z.string().optional(),
selectedInterests: z.object({
article_cta_portfolio: z.number().optional(),
}),
selectedInterests: z.record(z.number().optional()),
}),
)
.mutation(async ({input, ctx}) => {
Expand All @@ -29,12 +28,12 @@ export const customerIORouter = router({
const token = ctx?.userToken || process.env.EGGHEAD_SUPPORT_BOT_TOKEN
if (!token) return null

const user_contact = await getContactId({token, email})
const {contact_id} = await requestContactGuid(email)

const customer = await getAttributes(user_contact)
const customer = await getAttributes(contact_id)

if (customer) {
await cio.identify(user_contact, {
await cio.identify(contact_id, {
...(!customer?.attributes.signed_up_for_newsletter && {
signed_up_for_newsletter: date,
}),
Expand All @@ -44,14 +43,25 @@ export const customerIORouter = router({
})
return customer
} else {
await cio.identify(user_contact, {
email,
id: user_contact,
...selectedInterests,
pro: false,
created_at: date, // Customer.io uses seconds with their UNIX epoch timestamps
signed_up_for_newsletter: date,
})
try {
await cio.identify(email, {
email,
id: contact_id,
...selectedInterests,
pro: false,
created_at: date, // Customer.io uses seconds with their UNIX epoch timestamps
signed_up_for_newsletter: date,
})

return {
contact_id,
email,
selectedInterests,
}
} catch (error) {
console.error('Error identifying customer:', error)
return null
}
}
}),
})

0 comments on commit dbde5f5

Please sign in to comment.