2
0
Files
bot/packages/forge/blocks/segment/actions/identify.ts
John Walsh 29ab812512 Add Segment block (#1672)
Added support for Twilio Segment

New Segment Block added to Forge. Includes the following:
- Credentials
- Identify User, including Traits
- Alias User
- Track Event, including Properties
- Track Bot View ( Page ), including Properties
- Generate UUID for User IDs

Tested integration with Segment, verified working. Uses Segment's NodeJS
analytics library to support non-browser based platforms, such as
WhatsApp

---------

Co-authored-by: John Walsh <john@famkit.com>
Co-authored-by: Baptiste Arnaud <baptiste.arnaud95@gmail.com>
2024-08-16 17:23:23 +02:00

72 lines
1.7 KiB
TypeScript

import { createAction, option } from '@typebot.io/forge'
import { Analytics } from '@segment/analytics-node'
import { auth } from '../auth'
export const identify = createAction({
auth,
name: 'Identify User',
options: option.object({
userId: option.string.layout({
label: 'User ID',
isRequired: true,
}),
email: option.string.layout({
label: 'Email',
isRequired: false,
}),
traits: option.array(option.object({
key: option.string.layout({
label: 'Key',
isRequired: true,
}),
value: option.string.layout({
label: 'Value',
isRequired: true,
}),
})).layout({
itemLabel: 'trait',
}),
}),
run: {
server: async ({
credentials: { apiKey },
options: { userId, email, traits },
}) => {
if (!email || email.length === 0
|| !userId || userId.length === 0
|| apiKey === undefined) return
const analytics = new Analytics({ writeKey: apiKey })
if (traits === undefined || traits.length === 0) {
analytics.identify({
userId: userId,
traits: {
email: email
}
})
} else {
analytics.identify({
userId: userId,
traits: createTraits(traits, email)
})
}
await analytics.closeAndFlush()
}
},
})
const createTraits = (traits: { key?: string; value?: string }[], email: string) => {
const _traits: Record<string, any> = {}
// add email as a default trait
traits.push({ key: 'email', value: email })
traits.forEach(({ key, value }) => {
if (!key || !value) return
_traits[key] = value
})
return _traits
}