Segment is a powerful CDP with a variety of features. If you use a lot of tools, you’ll be delighted to find it has hundreds of platform integrations available right out of the box. It can integrate with popular solutions in your Martech stack like Google Analytics, Facebook Ads, Hubspot, etc.
Despite its advanced capabilities, sometimes Segment doesn’t have an integration for your system. Or worse, it only allows for a shallow integration, so it doesn’t do exactly what you want.
Segment Functions can resolve this issue by integrating your tool with unsupported systems. Our team has rich experience integrating Segment, setting up Segment’s custom features such as Personas, or optimizing how Segment is paired with GTM. So in this article, we’ll introduce you to Segment Functions. We will explain how you can generate sources and destinations and deliver data to new tools in an instant.
Best of all, we’ll provide real-life use cases so you understand how this can benefit your organization’s processes.
What Are Segment Functions, and What Can They Do for Your Tech Stack?
Launched in 2019, Segment Functions is currently out of Beta and available for use.
Segment Functions’ main purpose is to bridge the gap between different tools in your tech stack. You can use it to bring external data into Segment (Source Functions) and deliver the data in Segment to external destinations (Destination Functions). Segment Functions let you connect unsupported systems by using your own custom Javascript.
Let’s say you’re selling tickets for an event. With Segment Functions, you can send data about orders completed into your Segment CDP. If you want to get the information about the attendees to a tool where you can then target the confirmed attendees, you can send the data to another destination, such as your email flow tool.
Segment Functions can be utilized as Sources or Destinations in Connections, and Destinations in Personas. The feature uses server-side processing so it can access APIs via server-side code.
Keep in mind that this feature can bridge the gap from an unsupported system provided the system can do a few things:
- Can the system create automation when an action occurs? (triggered events)
- Can these automations POST data to another system? (webhooks)
As long as these two conditions are met, you can get the data into Segment.
Source Functions vs. Destination Functions
To help you understand how Segment Functions work, let’s take a look at Source Functions and Destination Functions.
Source Functions let you aggregate data and deliver it to Segment even when the source application is not supported. Source Functions help you pull data from unsupported systems. You just need the system to be able to send a webhook and use an endpoint URL.
Source Functions let you deliver data to Segment from an unsupported system.
Meanwhile, Destination Functions send data from Segment to an unsupported system. The feauture works with track events and delivers them to external tool or API. You can also transform or annotate the information before it is sent.
Destination Functions send data from Segment to an unsupported system.
For more insight on source vs destinations, we recommend checking out Segment’s resources on sources vs destinations in Segment’s docs. It’s an essential concept in customer data platforms. Not only is it relevant to Segment Functions, but it also relates to everything in Segment.
The Use Cases: How to Utilize Segment Functions for the Biggest Impact
How can Segment Functions help your business? Let’s take a look at some use cases from real-life clients.
Use Case #1: Zendesk Ticket Monitoring
Segment has Zendesk integrations, but this doesn’t support Zendesk ticket actions.
A client wanted to track and identify when a ticket changes its status, and capture the properties and traits that were isolated in their ticket system. In this example, Zendesk uses a Source Function. Since developers can use it to send custom actions to webhooks, we were able to create our own automated triggers.
Next, we wrote custom onTrack methods in the function to generate Zendesk Ticket Status events to Segment. This was filled with ticket information and user properties! We also sent Identify calls to determine unknown Zendesk users. Thanks to our reliable CDP, all data could be sent to other destinations, and get acted on.
Use Case #2: BigCommerce Order Updates API
BigCommerce has a Segment integration which is limited in scope. Implementation isn’t as straightforward when compared to Segment’s other sources. So we needed to leverage BigCommerce Webhooks and API endpoints to instrument events on the server-side for order transactions.
We created a custom Segment Function as a source and provided that endpoint to BigCommerce for the store/order/created action.
When an order was successfully placed in BigCommerce, the Order ID would be sent to our Segment Function.
Next, our Segment Function took that Order ID and requested additional order details. This includes billing, shipping, coupon, and product data from the distinct APIs that served that data.
All this information was linked into one big onTrack and onIdentify method that would generate Order Received and Product Purchase Received events in Segment, and call Identify on all the traits we wanted from the order data.
Example Snippets of Segment Functions
To get you another step closer to the data super powers you’ll gain by using Functions, here are two sample pieces of code for Segment Functions that you can use as templates. Segment does provide boilerplate JS when you create a Function, but these complete snippets will still give you a better idea of what’s ahead.
async function onRequest(request, settings) {
const body = request.json(); //Our function expects a JSON payload
//Here, you will want to do any formatting of your JSON payload before passing to Segment
let props {};
props.uid = body.uid; //you will want to capture a unique ID, email, etc about this event from the request body
props.firstName = body.firstName;
props.lastName = body.lastName;
props.email = body.email;
//Now we can call Segment functions using our new props
Segment.identify({
userId: props.uid,
traits: props
});
Segment.track({
event: 'Webhook Event Fired', //be sure to use a unique event name in Object Action format!
userId: props.uid,
properties: props
});
}
async function onTrack(event,settings) {
//logic for formatting your data if required
let payload = {
event_name: event.event,
event_properties: event.properties,
timestamp: event.timestamp
}
//now POST your data to your external service
await fetch('https://example-service.com/api', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(payload);
})
}
Common Challenges and Mistakes People Make with Functions
Let’s take a look at some common mistakes people make with Segment Functions:
Data Source Compatibility
Segment functions require a specific type of webhook request for the data to be processed. Webhooks — which send data to the Segment function — must indicate the Content-Type header to application/json or application/x-www-form-urlencode. Otherwise, it will not be interpreted by the function.
In case we need to integrate a webhook that can’t send data via this method, we can bridge the gap by writing custom middleware. For example, we recently wrote an AWS Lambda API integration that worked as a ‘passthrough’ so that it could take in webhook data and format it correctly before sending the data to our Segment functions.
Server-side Integration
Segment Functions are not like Segment client-side integrations. This means the automatic, client-side data which Segment normally captures is not available.
Let’s say, you want to create an anonymous ID in a Segment function. On the client-side, Segment can do this for you automatically. In Segment Functions, you will need to generate your own anonymous ID (or supply as user ID) to the function.
The same process applies for cookies, gclids, msclids, etc. These data types are not automatically collected. Segment functions should be considered as ‘server-side’ sources of data.
Keep an Eye on Your Usage
Once you’ve connected an outside data source to Segment, take note of the amount of data and possible volume of unique identifiers that might come in. Otherwise, you may be surprised by usage spikes caused by a huge influx of new users. Keep an eye on the amount of data flowing in and make adjustments to avoid blowing up your MTUs!
Build a Perfect MarTech Stack
Managing data across multiple tools in your MarTech stack gets easier with Segment. You can use Segment Functions to bring a variety of data into your CDP and send it to a variety of destinations. Then you can use it to run your automations, personalized messaging, and scale your ROI the way that is only possible with an integrated tech stack.
Leave a Reply