This directory holds the code for the Segment Integration Tutorial.
You can either:
- install and use it as a Medusa application;
- or copy its source files into an existing Medusa application.
- Node.js v20+
- Git CLI
- PostgreSQL
- Segment account with a Node.js source.
- Clone the repository and change to the
segment-integrationdirectory:
git clone https://github.com/medusajs/examples.git
cd examples/segment-integration2. Rename the .env.template file to .env.
3. If necessary, change the PostgreSQL username, password, and host in the DATABASE_URL environment variable.
4. Set the Segment environment variable:
SEGMENT_WRITE_KEY=Where its value is the Write API key of the Node.js source in segment.
Learn more about retrieving these variables in the tutorial
5. Install dependencies:
yarn # or npm install6. Setup and seed the database:
npx medusa db:setup
yarn seed # or npm run seed7. Start the Medusa application:
yarn dev # or npm run devYou can test the integration out by placing an order.
If you have an existing Medusa application, copy the following directories and files into your project:
src/modules/segmentsrc/subscriberssrc/workflows
Then, add the Segment Module Provider to medusa-config.ts:
module.exports = defineConfig({
// ...
modules: [
{
resolve: "@medusajs/medusa/analytics",
options: {
providers: [
{
resolve: "./src/modules/segment",
id: "segment",
options: {
writeKey: process.env.SEGMENT_WRITE_KEY || "",
},
},
],
},
},
]
})Next, add the following environment variable:
SEGMENT_WRITE_KEY=Where its value is the Write API key of the Node.js source in segment.
Learn more about retrieving these variables in the tutorial
After that, install the @segment/analytics-node package:
yarn add @segment/analytics-node # or npm install @segment/analytics-nodeThis guide was implemented with
@segment/analytics-node@^2.2.1.
You can test the integration out by placing an order.