> ## Documentation Index
> Fetch the complete documentation index at: https://docs.stigg.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Segment

<Note>
  Segment expects events to be reported for a specific user. Consequently, this integration only supports events from Stigg that are reported on **customers**.
</Note>

# 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](https://segment.com/docs/connections/functions/source-functions/).

In Segment:

1. From your workspace, go to the Catalog and click the [Functions tab](https://app.segment.com/goto-my-workspace/functions/catalog).
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.

<CodeGroup>
  ```javascript JavaScript theme={null}
  // 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
  	});
  }
  ```
</CodeGroup>

# Connecting the source function to a source in Segment

In Segment:

<Note>
  You must be a **Workspace Owner** or **Source Admin** to connect an instance of your function in your workspace.
</Note>

1. From the [Functions tab](https://app.segment.com/goto-my-workspace/functions/catalog), 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.

<Note>
  It's recommended to only send events that relate to customers. The full list of events can be found [here](/documentation/native-integrations/webhooks/events).
</Note>

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](https://app.segment.com/goto-my-workspace/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](https://segment.com/docs/connections/destinations/add-destination/).

# References

<Card title="Source Functions (Segment)" icon="code" href="https://segment.com/docs/connections/functions/source-functions/" />

<Card title="Using the Source Debugger (Segment)" icon="bug" href="https://segment.com/docs/connections/sources/debugger/" />

<Card title="Sending Data to Destinations (Segment)" icon="share-nodes" href="https://segment.com/docs/connections/destinations/add-destination/" />
