# How to connect Batch to Segment?

[Segment.com](https://segment.com/) is a customer data platform that simplifies data collection by providing a central point for storing and transferring data. We provide an example of integration with Segment so you can send your data directly from Segment to Batch.

This integration uses a Segment feature called [Destination Function](https://segment.com/docs/connections/functions/destination-functions/#:~:text=Destination%20functions%20allow%20you%20to,t%20view%20or%20use%20them).

{% hint style="info" %}
If you are interested in this example of integration, please contact us at: <partners@batch.com>.
{% endhint %}

## How can I use my Segment data on Batch?  <a href="#how-can-i-use-my-segment-data-on-batch" id="how-can-i-use-my-segment-data-on-batch"></a>

You can create a custom Destination Function to send **attributes and events** to **Batch**.

{% hint style="info" %}
If you are a Batch Omnichannel or Email client, the sample code will allow you to set up flows to send project-level data using the [Profile API](https://doc.batch.com/api/profile/update/).
{% endhint %}

### Create a Destination Function <a href="#h_4317a0bf72" id="h_4317a0bf72"></a>

Destination functions allow you to handle your Segment events and send them to any external tool or API, through JavaScript functions, without having to set up an infrastructure.

To create a **Destination Function in Segment**, navigate through *Connections > Catalog > Functions > Destination Functions*.

Then, we suggest you use our pre-built script to create your function and adapt it to your use cases.

Segment has a built-in testing module that will help you understand if your Function calls Batch APIs as expected. Try it using your events payloads.

<div align="center" data-full-width="true"><figure><img src="/files/cDbSuynml2SHNdDPLd0k" alt="Segment&#x27;s testing module" width="327"><figcaption><p><em>Segment’s testing module</em></p></figcaption></figure></div>

### Deploy as a Destination <a href="#h_5517e0755c" id="h_5517e0755c"></a>

Once your script is ready, you can **Save & Deploy** your function to a destination.

* **Pick one source**. It could be any of your sources: iOS, Android, Web, or other.
* **Declare settings**. They are a good option to store your API Keys. To ensure that the script recognizes the API keys, please name them as follows: `batchRestApiKey`, `batchLiveApiKey` or `batchProjectApiKey`. The REST API Key is sensitive; you must declare it as such.

{% hint style="info" %}
The Project API Key is optional. It is only necessary if you need to call project-level APIs (e.g., the [Profile API](https://doc.batch.com/api/profile/update/)).
{% endhint %}

<figure><img src="/files/fswy4n3oo78jlgpVYvsz" alt="Settings tab in Segment Functions interface" width="563"><figcaption><p><em>Settings tab in Segment Functions interface</em></p></figcaption></figure>

* **Activate** the Destination so that data starts flowing between Segment and Batch.

You should note that a Destination is an instance of a Destination Function.

If you have several sources plugged into Segment, you must create one Destination per source. However, you can use the same Function for all Destinations, as long as your events are handled correctly within the Function.

<figure><img src="/files/CbQUUAxqI3Ah4GEVymj4" alt="Batch is now registered as a Destination."><figcaption><p><em>Batch is registered as a Destination</em></p></figcaption></figure>

## How to send audiences from Segment to Batch? <a href="#h_9104700ab4" id="h_9104700ab4"></a>

Here is how you can create a custom Destination Function to send **custom audiences** to **Batch**.

You must follow the following steps in that order :

1. Create your Destination Function
2. Create your Destination
3. Create a filter in your Destination. Filter out all events that are not of event type “Track”. You can use “Only Send Event” > “Event Type is track”
4. Create your Segment Audience and send its events to the Batch Destination

{% hint style="info" %}
Steps 1, 2 & 3 must be done only once for the technical setup. Then, your marketing teams only need to follow step 4 to create new audiences.
{% endhint %}

### Create a Destination Function <a href="#h_141df317c4" id="h_141df317c4"></a>

Destination functions allow you to handle your Segment events and send them to any external tool or API, through JavaScript functions and without having to set up an infrastructure.

To create a **Destination Function in Segment**, navigate through *Connections > Catalog > Functions > Destination Functions*.

Then we suggest you use our pre-built script to create your function and adapt it to your use cases.

Segment has a built-in testing module that will help you understand if your Function calls Batch APIs as expected. Try it using your audience events payloads (Track and Identify).

<figure><img src="/files/cDbSuynml2SHNdDPLd0k" alt=""><figcaption><p><em>Segment’s testing module</em></p></figcaption></figure>

### Deploy as a Destination <a href="#h_5517e0755c" id="h_5517e0755c"></a>

Once your script is ready, you can **Save & Deploy** your function to a destination.

* **Pick one source**. It could be any of your sources: iOS, Android, Web, or other.
* **Declare settings**. They are a good option to store your API Keys. To ensure that the script recognizes the API keys, please name them as follows: `batchRestApiKey`, `batchLiveApiKey` or `batchProjectApiKey`. The REST API Key is sensitive, you must declare it as such.

{% hint style="info" %}
The Project API Key is optional. It is only necessary if you need to call project-level APIs (e.g., the [Profile API](https://doc.batch.com/api/profile/update/)).
{% endhint %}

<figure><img src="/files/fswy4n3oo78jlgpVYvsz" alt="" width="563"><figcaption><p><em>Settings tab in Segment Functions interface</em></p></figcaption></figure>

* **Activate** the Destination so that data starts flowing between Segment and Batch.

You need to deploy one destination for each Batch app (e.g., one destination for the iOS app and one destination for the Android app).

### Create your Segment Audience <a href="#h_55b4a053b6" id="h_55b4a053b6"></a>

**Pre-requisite**: To create Audiences, you need to have created a Unify / Personas space and declared data sources.

* In *Engage > Audiences,* create an audience with the audience builder.\
  You can target events from your data sources and create your audiences accordingly. There are several computing methods available. Please refer to [Segment’s Audience Documentation](https://segment.com/docs/engage/audiences/) to understand it better.
* Once your audience is designed, choose to send events to one or multiple Batch Destinations created beforehand.
* In the menu that opens, select "Send Track".\
  ​Note: The values of the "Enter Event" and "Exit Event" fields don't matter.

\
Give your audience a name, and the audience creation will be complete:

<figure><img src="/files/ytcPg2dQHVbeiCu8dG8X" alt="creating an audience in progress"><figcaption><p>Create an audience</p></figcaption></figure>

\
🎉 Your audience will be initialized in Segment and created in Batch immediately. The audience will be updated in Batch each time a user enters or leaves it.

<figure><img src="/files/GocrsK8DhqTLXvLUQD4H" alt="Batch is a Destination of the Audience."><figcaption><p><em>Batch is a Destination of the Audience.</em></p></figcaption></figure>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://doc.batch.com/integrations/cdp/how-to-connect-batch-to-segment.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
