Sending Data to Segment

Krishna Vepakomma
October 25, 2024

Step-by-Step Guide to Configure a Source and Start Sending Data Using Segment SDK

Step 1: Add a New Source in Segment

  1. Log in to your Segment account.
  2. Navigate to the "Connections" page and click on the "Catalog" tab.
  3. Select the "Sources" tab.
  4. Search for the desired source (e.g., JavaScript, Node.js, etc.) and click on it.
  5. Click the "Add Source" button and follow the prompts to complete the setup.

Step 2: Configure the Source

  1. After adding the source, you will be provided with a write key. This key is essential for integrating Segment with your application.
  2. Follow the specific instructions for the SDK you are using to install and set up the Segment library.
  3. For example, to install the Node.js SDK, use:
  4. npm install @segment/analytics-node
  5. Then, initialize the library with your write key:
  6. const Analytics = require('@segment/analytics-node');
    const analytics = new Analytics('YOUR_WRITE_KEY');

Step 3: Sending Data Using Segment SDK

Example Use Case: Identify and Track Calls
  1. Identify Call: Use the identify call to associate a user with their traits (e.g., email, name, subscription plan).
  2. analytics.identify({
     userId: 'user123',
     traits: {
       name: 'John Doe',
       email: 'john.doe@example.com',
       plan: 'premium',
       signupDate: '2024-06-07'
     }
    });
  3. Track Call: Use the track call to record any actions the user performs (e.g., login, signup, feature usage).
  4. analytics.track({
     userId: 'user123',
     event: 'Signed Up',
     properties: {
       plan: 'premium',
       source: 'marketing_campaign'
     }
    });
    analytics.track({
     userId: 'user123',
     event: 'Logged In',
     properties: {
       time: '2024-06-07T12:34:56Z'
     }
    });
    analytics.track({
     userId: 'user123',
     event: 'Subscription Changed',
     properties: {
       oldPlan: 'basic',
       newPlan: 'premium'
     }
    });
    analytics.track({
     userId: 'user123',
     event: 'Account Deleted',
     properties: {
       reason: 'user request'
     }
    });

Example Use Case: Integrating with Signup, Login, Feature Actions, Subscriptions, and Delete Account

  1. Signup
  2. app.post('/signup', (req, res) => {
     const { userId, name, email, plan } = req.body;

     // Identify the user
     analytics.identify({
       userId: userId,
       traits: {
         name: name,
         email: email,
         plan: plan,
         signupDate: new Date().toISOString()
       }
     });

     // Track the signup event
     analytics.track({
       userId: userId,
       event: 'Signed Up',
       properties: {
         plan: plan
       }
     });

     res.status(200).send('User signed up successfully');
    });
  3. Login
  4. app.post('/login', (req, res) => {
     const { userId } = req.body;

     // Track the login event
     analytics.track({
       userId: userId,
       event: 'Logged In',
       properties: {
         time: new Date().toISOString()
       }
     });

     res.status(200).send('User logged in successfully');
    });
  5. Feature Actions
  6. app.post('/feature-use', (req, res) => {
     const { userId, featureName } = req.body;

     // Track feature usage
     analytics.track({
       userId: userId,
       event: `Used Feature: ${featureName}`,
       properties: {
         feature: featureName,
         time: new Date().toISOString()
       }
     });

     res.status(200).send('Feature usage recorded');
    });
  7. Subscription Changes
  8. app.post('/subscription-change', (req, res) => {
     const { userId, oldPlan, newPlan } = req.body;

     // Track the subscription change event
     analytics.track({
       userId: userId,
       event: 'Subscription Changed',
       properties: {
         oldPlan: oldPlan,
         newPlan: newPlan
       }
     });

     res.status(200).send('Subscription change recorded');
    });
  9. Delete Account
  10. app.post('/delete-account', (req, res) => {
     const { userId, reason } = req.body;

     // Track the account deletion event
     analytics.track({
       userId: userId,
       event: 'Account Deleted',
       properties: {
         reason: reason
       }
     });

     res.status(200).send('Account deletion recorded');
    });

Conclusion

By following this guide, startups can efficiently set up Segment to send data from their applications using the Segment SDK. The examples provided demonstrate how to use identify and track calls to capture user traits and events, helping businesses gain valuable insights and make data-driven decisions.