# Segment

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

import ConversionTrackingPrerequisites from "/snippets/conversion-tracking-prerequisites.mdx";
import LeadAttributes from "/snippets/lead-attributes.mdx";
import SaleAttributes from "/snippets/sale-attributes.mdx";
import ViewConversions from "/snippets/view-conversions.mdx";

<Tip>
  This feature is only available on [Business plans and
  above](https://dub.co/pricing/partners).
</Tip>

Dub's [Segment integration](https://dub.co/integrations/segment) provides a bi-directional sync between your Segment workspace and Dub. There are 2 parts to the integration:

- [Dub (Actions) Destination](https://www.twilio.com/docs/segment/connections/destinations/catalog/actions-dub) – Tracking conversion events on Dub directly from Segment
- [Dub Source](https://www.twilio.com/docs/segment/connections/sources/catalog/cloud-apps/dub) – Sending customer events from Dub to Segment

In this guide, we will be focusing on using the [Dub (Actions) Destination](https://www.twilio.com/docs/segment/connections/destinations/catalog/actions-dub) to track conversion events directly from Segment.

<ConversionTrackingPrerequisites />

## Configure Segment Action

Next, configure [Segment Dub (Actions)](https://app.segment.com/goto-my-workspace/destinations/catalog/actions-dub) to track sales 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="/images/conversions/segment/segment-actions.png"
        alt="Segment Dub (Actions) destination"
        width="1440"
        height="1024"
      />

    </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](/docs/api-reference/authentication#api-keys). 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="/images/conversions/segment/segment-basic-settings.png"
        alt="Segment Dub (Actions) Basic Settings"
        width="1440"
        height="1024"
      />
    </Frame>

  </Step>
  
  <Step title="Add Mapping">

    Next, depending on the event you want to track, choose the corresponding action from the list of available actions:

    | Event to track | Default event name | Example |
    | --- | --- | --- |
    | Lead | Sign Up | A user signs up for an account |
    | Sale | Order Completed | A user purchases a product |

    <Frame>
      <img
        src="/images/conversions/segment/segment-track-sale-action.png"
        alt="Segment Dub (Actions) Mapping"
        width="1440"
        height="1024"
      />
    </Frame>

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

    <Frame>
      <img
        src="/images/conversions/segment/segment-track-sale-mapping.png"
        alt="Segment Dub (Actions) Mapping"
        width="1440"
        height="1024"
      />
    </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 conversion events to Dub">

    On the server side, you’ll use the `@segment/analytics-node` SDK to send conversion events to Segment.

    Make sure to include relevant properties in the payload:

  <CodeGroup>

    ```tsx Track a lead
    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,
      },
    });
    ```

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

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

    segment.track({
      userId: id,
      event: "Order Completed",
      properties: {
        payment_processor: "stripe",
        order_id: "ORD_123",
        currency: "USD",
        revenue: 1000,
      },
      integrations: {
        All: true,
      },
    });
    ```

  </CodeGroup>

    Once the event is tracked, Segment will forward the conversion data to Dub based on the mappings you’ve configured.

  </Step>

</Steps>

## Supported attributes

<Tabs>
  <Tab title="Lead event">

    <LeadAttributes />

  </Tab>
  <Tab title="Sale event">

    <SaleAttributes />

  </Tab>
</Tabs>

## View conversion results

And that's it – you're all set! You can now sit back, relax, and watch your conversion revenue grow. We provide 3 different views to help you understand your conversions:

<ViewConversions />

## Example apps

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