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>
71 lines
1.6 KiB
TypeScript
71 lines
1.6 KiB
TypeScript
import { createAction, option } from '@typebot.io/forge'
|
|
import { Analytics } from '@segment/analytics-node'
|
|
import { auth } from '../auth'
|
|
|
|
export const trackEvent = createAction({
|
|
auth,
|
|
name: 'Track',
|
|
options: option.object({
|
|
eventName: option.string.layout({
|
|
label: 'Name',
|
|
isRequired: true,
|
|
}),
|
|
userId: option.string.layout({
|
|
label: 'User ID',
|
|
isRequired: true,
|
|
}),
|
|
properties: option.array(option.object({
|
|
key: option.string.layout({
|
|
label: 'Key',
|
|
isRequired: true,
|
|
}),
|
|
value: option.string.layout({
|
|
label: 'Value',
|
|
isRequired: true,
|
|
}),
|
|
})).layout({
|
|
itemLabel: 'property',
|
|
}),
|
|
}),
|
|
run: {
|
|
server: async ({
|
|
credentials: { apiKey },
|
|
options: { eventName, userId, properties },
|
|
}) => {
|
|
if (!eventName || eventName.length === 0
|
|
|| !userId || userId.length === 0
|
|
|| apiKey === undefined) return
|
|
|
|
const analytics = new Analytics({ writeKey: apiKey })
|
|
|
|
if (properties === undefined || properties.length === 0) {
|
|
analytics.track({
|
|
userId: userId,
|
|
event: eventName
|
|
})
|
|
} else {
|
|
analytics.track({
|
|
userId: userId,
|
|
event: eventName,
|
|
properties: createProperties(properties)
|
|
})
|
|
}
|
|
await analytics.closeAndFlush()
|
|
}
|
|
},
|
|
})
|
|
|
|
const createProperties = (properties: { key?: string; value?: string }[]) => {
|
|
const props: Record<string, any> = {}
|
|
|
|
properties.forEach(({ key, value }) => {
|
|
if (!key || !value) return
|
|
props[key] = value
|
|
})
|
|
|
|
return props
|
|
}
|
|
|
|
|
|
|