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:

  1. From your workspace, go to the Catalog and click the Functions tab.
  2. Click "New Function".
  3. Select "Source Function" and click "Build" - when you click "Build", a code editor appears.
  4. Paste the below snippet.
  5. Click "Configure" to name the source function.
  6. 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.

  1. From the Functions tab, click "Connect Source" and follow the prompts to set it up in your workspace.
  2. Once configured, find the webhook URL - either on the "Overview" or "Settings β†’ Endpoint" page.
  3. Copy the URL.

Creating the webhook in Stigg

In Stigg:

  1. Navigate to the Settings > Integrations > Webhooks screen.
  2. Click on the "+ New webhook" button.
  3. In the opened modal enter the following details:
    1. Service name - the name of the service that's integrated with Stigg, for example: "Segment".
    2. Endpoint URL - the URL that was generated by Segment.
    3. 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:

  1. Click on the row of the newly created webhook from the webhook list screen in order to view the webhook details.
  2. 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.
  3. Click on "Send test event" to confirm the action.

Validating the integration

In Segment:

  1. From the Sources tab select the source that was added.
  2. Select the "Debugger" tab.
  3. 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