Example - Segment integration
In this example we'll demonstrate a webhook integration with Segment
Segment expects events to be reported for a specific user. Consequently, this integration only supports events from Stigg that are reported on customers.
Adding a source function in Segment
Segment expects events to be reported on a specific user. In order to support this flow, we'll need to map Stigg's webhook payload to a Segment.track()
event using a Segment's source function.
In Segment:
- From your workspace, go to the Catalog and click the Functions tab.
- Click "New Function".
- Select "Source Function" and click "Build" - when you click "Build", a code editor appears.
- Paste the below snippet.
- Click "Configure" to name the source function.
- Click "Create Function" to save it - once you do that, the source function appears on the Functions page in your workspaceβs catalog.
// Learn more about source functions API at
// https://segment.com/docs/connections/sources/source-functions
/**
* Handle incoming HTTP request
*
* @param {FunctionRequest} request
* @param {FunctionSettings} settings
*/
async function onRequest(request, settings) {
const body = request.json();
// filter out events that are not reported on a customer
if (!body.customer || !body.customer.id) {
return;
}
// See https://segment.com/docs/connections/spec/track/
Segment.track({
event: body.type.split('.').join('_'),
userId: body.customer.id,
properties: body
});
}
Connecting the source function to a source in Segment
In Segment:
You must be a Workspace Owner or Source Admin to connect an instance of your function in your workspace.
- From the Functions tab, click "Connect Source" and follow the prompts to set it up in your workspace.
- Once configured, find the webhook URL - either on the "Overview" or "Settings β Endpoint" page.
- Copy the URL.
Creating the webhook in Stigg
In Stigg:
- Navigate to the Settings > Integrations > Webhooks screen.
- Click on the "+ New webhook" button.
- In the opened modal enter the following details:
- Service name - the name of the service that's integrated with Stigg, for example: "Segment".
- Endpoint URL - the URL that was generated by Segment.
- Events to send - select the list of events that you'd like to send to this endpoint.
It's recommended to only send events that relate to customers. The full list of events can be found here.
Click "Add" to confirm the action.
Sending data to the webhook
In Stigg:
- Click on the row of the newly created webhook from the webhook list screen in order to view the webhook details.
- Under the "Webhook events" section, click on the dotted menu icon of the relevant event that you'd like to test, and select the "Send test event" action.
- Click on "Send test event" to confirm the action.
Validating the integration
In Segment:
- From the Sources tab select the source that was added.
- Select the "Debugger" tab.
- Ensure that you see the event that you sent from Stigg with the same payload.
Once the integration has been validated, you can connect the source to destinations in Segment.
References
Updated 10 months ago