> ## Documentation Index
> Fetch the complete documentation index at: https://dub-client-tracking-lead-sale.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Segment

> Learn how to track lead conversion events with Segment and Dub

<Note>
  Conversion tracking require a [Business plan](https://dub.co/pricing)
  subscription or higher.
</Note>

When it comes to [conversion tracking](/conversions/quickstart), a `lead` event happens when a user performs an action that indicates interest in your product or service. This could be anything from:

* Signing up for an account
* Adding a product to cart
* Joining a mailing list

<Frame>
  <img className="rounded-lg border border-gray-100" src="https://assets.dub.co/help/conversion-lead-event.png" alt="A diagram showing how lead events are tracked in the conversion funnel" />
</Frame>

In this guide, we will be focusing on tracking new user sign-ups for a SaaS application that uses Segment to track conversions.

## Prerequisites

Before you get started, make sure you follow the [Dub Conversions quickstart guide](/conversions/quickstart) to get Dub Conversions set up for your links:

1. [Enable conversion tracking for your links](/conversions/quickstart#step-1%3A-enable-conversion-tracking-for-your-links)
2. [Install the @dub/analytics client-side SDK](/sdks/client-side/introduction)

## Configure Segment Action

Next, configure [Segment Dub (Actions)](https://app.segment.com/goto-my-workspace/destinations/catalog/actions-dub) to track lead conversion events.

<Steps>
  <Step title="Add Dub (Actions) destination">
    Head to [Segment Dub (Actions)](https://app.segment.com/goto-my-workspace/destinations/catalog/actions-dub) and add the destination to your Segment workspace.

    <Frame>
      <img src="https://mintcdn.com/dub-client-tracking-lead-sale/Fo3EyiwSOIeMUHrn/images/conversions/segment/segment-actions.png?fit=max&auto=format&n=Fo3EyiwSOIeMUHrn&q=85&s=fd04c3d20eff31ed791f851f6a82940f" alt="Segment Dub (Actions) destination" width="1440" height="1024" width="2980" height="1604" data-path="images/conversions/segment/segment-actions.png" />
    </Frame>
  </Step>

  <Step title="Configure Dub API Key">
    In the Dub (Actions) destination settings, fill out the following fields:

    * **Name:** Enter a name to help you identify this destination in Segment.
    * **API Key:** Enter your Dub API key. You can find this in the [Dub Dashboard](https://app.dub.co/settings/tokens).
    * **Enable Destination:** Toggle this on to allow Segment to send data to Dub.

    Once completed, click **Save Changes**.

    <Frame>
      <img src="https://mintcdn.com/dub-client-tracking-lead-sale/Fo3EyiwSOIeMUHrn/images/conversions/segment/segment-basic-settings.png?fit=max&auto=format&n=Fo3EyiwSOIeMUHrn&q=85&s=fc702fc73b817f907b78ce87100cb3ac" alt="Segment Dub (Actions) Basic Settings" width="1440" height="1024" width="2984" height="930" data-path="images/conversions/segment/segment-basic-settings.png" />
    </Frame>
  </Step>

  <Step title="Add Mapping">
    Next, you’ll choose the **Track a lead** action from the list of available actions.

    By default, this action is configured to send lead data to Dub when the **Event Name** is **Sign Up**.

    <Frame>
      <img src="https://mintcdn.com/dub-client-tracking-lead-sale/Fo3EyiwSOIeMUHrn/images/conversions/segment/segment-track-lead-action.png?fit=max&auto=format&n=Fo3EyiwSOIeMUHrn&q=85&s=d885191dacee71d74a42d3449cfc21a7" alt="Segment Dub (Actions) Mapping" width="1440" height="1024" width="2698" height="1722" data-path="images/conversions/segment/segment-track-lead-action.png" />
    </Frame>

    Below the selected action, you’ll see the mapping for that action.

    <Frame>
      <img src="https://mintcdn.com/dub-client-tracking-lead-sale/Fo3EyiwSOIeMUHrn/images/conversions/segment/segment-track-lead-mapping.png?fit=max&auto=format&n=Fo3EyiwSOIeMUHrn&q=85&s=8e3a5bf9542d9bb797a2b42ed6284fee" alt="Segment Dub (Actions) Mapping" width="1440" height="1024" width="1016" height="723" data-path="images/conversions/segment/segment-track-lead-mapping.png" />
    </Frame>

    You can customize the trigger and mapping to fit the specific needs of your application.

    Finally, click **Next** and then **Save and enable** to add the mapping to the destination.
  </Step>

  <Step title="Send lead events to Dub">
    On the server side, you’ll use the `@segment/analytics-node` SDK to send lead events to Segment.

    Make sure to include relevant user traits such as `name`, `email`, and `clickId` in the payload.

    You’ll also need to ensure that the `clickId` field is properly mapped in your Segment Actions destination so that it’s forwarded correctly to Dub.

    ```tsx
    import { Analytics } from "@segment/analytics-node";

    const segment = new Analytics({
      writeKey: "<YOUR_SEGMENT_WRITE_KEY>",
    });

    const cookieStore = await cookies();
    const clickId = cookieStore.get("dub_id")?.value;

    segment.track({
      userId: id,
      event: "Sign Up",
      context: {
        traits: {
          name,
          email,
          avatar,
          clickId,
        },
      },
      integrations: {
        All: true,
      },
    });
    ```

    Once the event is tracked, Segment will forward the lead data to Dub based on the mappings you’ve configured.
  </Step>
</Steps>

Here's the full list of attributes you can pass when sending a lead event:

| Property             | Required | Description                                                                                                              |
| :------------------- | :------- | :----------------------------------------------------------------------------------------------------------------------- |
| `clickId`            | **Yes**  | The unique `dub_id` parameter that the lead conversion event is attributed to.                                           |
| `eventName`          | **Yes**  | The name of the event. Example: "Sign up".                                                                               |
| `customerExternalId` | **Yes**  | The unique ID of the customer in your system. Will be used to identify and attribute all future events to this customer. |
| `customerEmail`      | No       | The email address of the customer. If not passed, a random email address will be generated.                              |
| `customerName`       | No       | The name of the customer. If not passed, a random name will be generated (e.g. "Big Red Caribou").                       |
| `customerAvatar`     | No       | The avatar URL of the customer. If not passed, a random avatar URL will be generated.                                    |

## Example App

To learn more about how to track leads with Segment, check out the following example app:

<Card title="Segment + Next.js App Router Example" icon="github" href="https://github.com/dubinc/examples/blob/main/conversions/segment/actions/track-lead.ts">
  Next.js app using Segment to track new user sign-ups.
</Card>

## View your conversions

Once you've completed the setup, all your tracked conversions will show up in [Dub Analytics](https://dub.co/analytics). We provide 3 different views to help you understand your conversions:

* **Time-series**: A [time-series view](https://app.dub.co/dub/analytics?view=timeseries) of the number clicks, leads and sales.

<Frame>
  <img src="https://mintcdn.com/dub-client-tracking-lead-sale/Fo3EyiwSOIeMUHrn/images/conversions/timeseries-chart.png?fit=max&auto=format&n=Fo3EyiwSOIeMUHrn&q=85&s=71d3c78498e8befddb5d2fda748d7d54" alt="Time-series line chart" width="2400" height="1260" data-path="images/conversions/timeseries-chart.png" />
</Frame>

* **Funnel chart**: A [funnel chart view](http://app.dub.co/analytics?view=funnel) visualizing the conversion & dropoff rates across the different steps in the conversion funnel (clicks → leads → sales).

<Frame>
  <img src="https://mintcdn.com/dub-client-tracking-lead-sale/Fo3EyiwSOIeMUHrn/images/conversions/funnel-chart.png?fit=max&auto=format&n=Fo3EyiwSOIeMUHrn&q=85&s=1a70ca66d94eee8d705323f7fd33944e" alt="Funnel chart view showing the conversion & dropoff rates from clicks → leads → sales" width="2400" height="1260" data-path="images/conversions/funnel-chart.png" />
</Frame>

* **Real-time events stream**: A [real-time events stream](https://app.dub.co/events) of every single conversion event that occurs across all your links in your workspace.

<Frame>
  <img src="https://mintcdn.com/dub-client-tracking-lead-sale/Fo3EyiwSOIeMUHrn/images/conversions/events-table.png?fit=max&auto=format&n=Fo3EyiwSOIeMUHrn&q=85&s=70741a124cdbaa226d35cec457e65f54" alt="The Events Stream dashboard on Dub" width="2400" height="1260" data-path="images/conversions/events-table.png" />
</Frame>
