Skip to main content


In this document, you’ll learn about the Segment plugin, what it does, and how to install and use it.


Segment is a powerful Customer Data Platform that allows users to collect, transform, send and archive their customer data.

Segment allows users to manage different tracking and marketing tools using one API and interface, making it very simple to try out and integrate with different services in your ecommerce stack.

Common integration use cases that can be implemented with Segment include:

  • Google Analytics
  • Mailchimp
  • Zendesk
  • Data warehousing for advanced data analytics and segmentation through services like Snowflake

The Segment plugin in Medusa allows you to track ecommerce events and record them in Segment. Then, you can push these events to other destinations using Segment.

Events That the Segment Plugin Tracks

By default, the Segment plugin tracks the following events:

  1. Copy to Clipboard: Triggered when an order is placed.
  2. Copy to Clipboard: Triggered when a shipment is created for an order.
  3. Copy to Clipboard: Triggered when a new claim is created.
  4. Copy to Clipboard: Triggered when an item in an order is returned.
  5. Copy to Clipboard: Triggered when an order is canceled.
  6. Copy to Clipboard: Triggered when a swap is created.
  7. Copy to Clipboard: Triggered when a shipment is created for a swap.
  8. Copy to Clipboard: Triggered when payment for a swap is completed.

Check out the Event Reference to learn about all available events in Medusa.


Medusa Server

It is assumed you already have a Medusa server installed. If not, please follow the Quickstart guide to get started in minutes.

In addition, make sure to have Redis installed and configured with your Medusa server. If not, follow this documentation to install Redis and then configure it.

Segment Account

You need to create a Segment account to follow along with the tutorial. Segment offers a free plan to get started quickly.

Create a Segment Source

On your Segment dashboard, choose Catalog from the sidebar under Connections.

Under Connections in the sidebar choose Catalog

Then, in the catalog list find the Server category and choose Node.js from the list.

Choose Node.js under the Server category

This opens a new side menu. In the side menu, click on Add Source.

Click on Add Source in the side menu showing information about the Node.js source

This opens a new page to create a Node.js source. Enter the name of the source then click Add Source.

Enter a name under the Name field then click on the Add Source button

On the new source dashboard, you should find a Write Key. You’ll use this key in the next section after you install the Segment plugin on your Medusa server.

The Write Key is available on the new source's page

Optional: Add Destination

After you create the Segment source, you can add destinations. This is where the data is sent when you send them to Segment. You can add more than one destination.

To add a destination, on the same Segment source page, click on Add Destination in the Destinations section.

Click on the Add Destination button on the source page

You can then choose from a list of destinations such as Google Universal Analytics or Facebook Pixel.

List of some of the available destinations in Segment's catalog

The process of integrating each destination is different, so you must follow the steps detailed in Segment for each destination you choose.

Install the Segment Plugin

In the directory of your Medusa server, run the following command to install the Segment plugin:

Report Incorrect CodeReport Incorrect CodeReport Incorrect CodeCopy to Clipboard
npm install medusa-plugin-segment
Copy to Clipboard

Then, add the following environment variable:

Report Incorrect CodeReport Incorrect CodeReport Incorrect CodeCopy to Clipboard
Copy to Clipboard

Where Copy to Clipboard is the Write Key shown on the page of the Segment source you created in the previous section.

Finally, in Copy to Clipboard, add the following new item to the Copy to Clipboard array:

Report Incorrect CodeReport Incorrect CodeReport Incorrect CodeCopy to Clipboard
const plugins = [
resolve: `medusa-plugin-segment`,
options: {
write_key: process.env.SEGMENT_WRITE_KEY,
Copy to Clipboard

Test the Plugin

Run your server with the following command:

Report Incorrect CodeReport Incorrect CodeReport Incorrect CodeCopy to Clipboard
npm run start
Copy to Clipboard

Then, try triggering one of the mentioned events earlier in this document. For example, you can place an order either using the REST APIs or using the Next.js or Gatsby storefronts.

After you place an order, on the Segment source that you created, click on the Debugger tab. You should see at least one event triggered for each order you place. If you click on the event, you can see the order details are passed to the event.

The order completed event is recorded on the Segment source

If you added a destination, you can also check your destination to make sure the data is reflected there.

If the data is not appearing on the destination, the issue is related to your configuration between the Segment source and destination and not related to the Segment plugin. Go over the steps that Segment showed you when you added the destination to figure out the issue.

Add Custom Tracking

In some cases, you might want to track more events or custom events. You can do that using the Copy to Clipboard provided by the Segment Plugin.

For example, you can add the following subscriber to listen to the Copy to Clipboard event and add tracking for every customer created:

Report Incorrect CodeReport Incorrect CodeReport Incorrect CodeCopy to Clipboard
class CustomerSubscriber {
constructor({ segmentService, eventBusService }) {
this.segmentService = segmentService;

eventBusService.subscribe("customer.created", this.handleCustomer);

handleCustomer = async (data) => {
const customerData = data;
delete customerData['password_hash'];

event: 'Customer Created',
properties: customerData

export default CustomerSubscriber;
Copy to Clipboard

You resolve the Copy to Clipboard using dependency injection. Then, when the Copy to Clipboard event is triggered, you use the Copy to Clipboard method available in the Copy to Clipboard to send tracking data to Segment.

Services can be resolved and used in Subscribers, endpoints, and other Services. Learn how to resolve services in the Services documentation.

Copy to Clipboard accepts an object of data, where the keys Copy to Clipboard and Copy to Clipboard are required. Instead of Copy to Clipboard, you can use Copy to Clipboard to pass an anonymous user ID.

If you want to pass additional data to Segment, pass them under the Copy to Clipboard object key.

The Copy to Clipboard also provides the method Copy to Clipboard to tie a user to their actions or specific traits.

Test Custom Tracking

After adding the above subscriber, run your server again if it isn’t running and create a customer using the REST APIs or one of the Medusa storefronts. If you check the Debugger in your Segment source, you should see a new event “Customer Created” tracked. If you click on it, you’ll see the data you passed to the Copy to Clipboard method.

The customer created event is recoreded on the Segment source

What’s Next