← Back

Integrate with Segment

💡
We currently support segment by a custom function
 
  • To get started, go to Catalog in the sidebar
    • notion image
       
  • Click Functions in the top bar
    • notion image
       
  • Create new function and select Destination
  • A code editor comes up. Paste following content into it.
const splitbeeRequest = async (event, path, body, projectId) => {
	const res = await fetch(`https://hive.splitbee.io` + path, {
		method: 'POST',
		headers: {
			...(event.userId && { userId: event.userId }),
			...(event.anonymousId && { uid: event.anonymousId }),
			...(event.context.page && { origin: event.context.page.url }),
			...(projectId && { sbp: projectId }),
			...(event.context.userAgent && { 'user-agent': event.context.userAgent })
		},
		body: JSON.stringify({
			...body
		})
	});
};

async function onTrack(event, settings) {
	await splitbeeRequest(
		event,
		'/t',
		{
			event: event.event,
			data: event.properties
		},
		settings.projectId
	);
}

async function onIdentify(event, settings) {
	await splitbeeRequest(
		event,
		'/user',
		{
			...event.traits
		},
		settings.projectId
	);
}

/**
 * Handle group event
 * @param  {SegmentGroupEvent} event
 * @param  {FunctionSettings} settings
 */
async function onGroup(event, settings) {
	// Learn more at https://segment.com/docs/connections/spec/group/
	console.log(event);
}

async function onPage(event, settings) {
	console.log(settings);
	const location = new URL(event.context.page.url);
	const referrer =
		event.properties.referrer.indexOf(
			location.protocol + '//' + location.host
		) === 0
			? undefined
			: event.context.referrer;

	await splitbeeRequest(
		event,
		'/i',
		{
			origin: event.properties.url,
			...(referrer && { referrer })
		},
		settings.projectId
	);
}

/**
 * Handle screen event
 * @param  {SegmentScreenEvent} event
 * @param  {FunctionSettings} settings
 */
async function onScreen(event, settings) {
	// Learn more at https://segment.com/docs/connections/spec/screen/
	console.log(event);
}

/**
 * Handle alias event
 * @param  {SegmentAliasEvent} event
 * @param  {FunctionSettings} settings
 */
async function onAlias(event, settings) {
	// Learn more at https://segment.com/docs/connections/spec/alias/
	console.log(event);
}

/**
 * Handle delete event
 * @param  {SegmentDeleteEvent} event
 * @param  {FunctionSettings} settings
 */
async function onDelete(event, settings) {
	// Learn more at https://segment.com/docs/partners/spec/#delete
	console.log(event);
}
 
  • Next, press on Settings in the right bar
    • notion image
 
  • Add a new setting with the label projectId . The type should be String
  • Press Configure, enter name Splitbee and press Create Function
 

Enable the destination

 
  • Go to Catalog → Functions → Splitbee
  • Now press Connect Destination → Select your Source
  • Now enter your projectId in the settings.
  • Activate the destination by pressing the toggle
notion image

You successfully added the Splitbee integration with Segment! 🎉

 
 

Did we miss something? Not to worry! Just email our support team at hello@splitbee.io✌️