How to Set Up Segment for Customer Data Collection
·APIScout Team
segmentcustomer dataanalyticstutorialapi integration
How to Set Up Segment for Customer Data Collection
Segment is the customer data pipeline. Track events once, send them everywhere — analytics, marketing, data warehouse, CRM. Instead of integrating 10 tools separately, integrate Segment once and route data to all of them.
What You'll Build
- Event tracking (page views, clicks, conversions)
- User identification across sessions
- Data routing to multiple destinations
- Server-side tracking for sensitive events
- Custom traits and user profiles
Prerequisites: React/Next.js, Segment account (free: 1,000 visitors/month).
1. Setup
Create Source
- Sign up at segment.com
- Add a Source → JavaScript (Website)
- Copy your Write Key
Install
npm install @segment/analytics-next
Initialize
// lib/segment.ts
import { AnalyticsBrowser } from '@segment/analytics-next';
export const analytics = AnalyticsBrowser.load({
writeKey: process.env.NEXT_PUBLIC_SEGMENT_WRITE_KEY!,
});
Provider Component
// app/providers.tsx
'use client';
import { useEffect } from 'react';
import { analytics } from '@/lib/segment';
import { usePathname } from 'next/navigation';
export function SegmentProvider({ children }: { children: React.ReactNode }) {
const pathname = usePathname();
useEffect(() => {
analytics.page(); // Track page view
}, [pathname]);
return <>{children}</>;
}
2. Core Methods
Page Tracking
// Automatic page tracking (in provider above)
analytics.page();
// With properties
analytics.page('Pricing', {
plan_shown: 'pro',
ab_test: 'pricing-v2',
});
Event Tracking
// Track a custom event
analytics.track('Button Clicked', {
button_name: 'Start Free Trial',
page: 'pricing',
plan: 'pro',
});
// Track a purchase
analytics.track('Purchase Completed', {
revenue: 29.00,
currency: 'USD',
plan: 'pro',
billing_period: 'monthly',
});
// Track feature usage
analytics.track('Feature Used', {
feature: 'api_dashboard',
action: 'export_data',
format: 'csv',
});
User Identification
// After login — link anonymous activity to known user
analytics.identify(user.id, {
email: user.email,
name: user.name,
plan: user.plan,
company: user.company,
createdAt: user.createdAt,
});
// After logout — reset identity
analytics.reset();
Group (Company/Team)
// Associate user with a company (B2B)
analytics.group(company.id, {
name: company.name,
plan: company.plan,
employees: company.employeeCount,
industry: company.industry,
});
3. Tracking Plan
Define what events to track before instrumenting:
| Event | Properties | Trigger |
|---|---|---|
Page Viewed | path, referrer, title | Every page load |
Sign Up Started | method (email, google) | Sign-up form shown |
Sign Up Completed | method, plan | Account created |
Login | method | User logs in |
Feature Used | feature_name, action | Key feature interactions |
Plan Selected | plan_name, price, period | Pricing page selection |
Checkout Started | plan, amount | Payment form shown |
Purchase Completed | revenue, plan, currency | Payment successful |
Subscription Cancelled | plan, reason, days_active | User cancels |
Event Naming Conventions
✅ Object + Action (past tense): "Purchase Completed", "Feature Used"
❌ Verb + Object: "Completed Purchase", "Used Feature"
❌ camelCase: "purchaseCompleted"
❌ snake_case: "purchase_completed"
4. Server-Side Tracking
For events that shouldn't be tracked client-side (revenue, sensitive data):
npm install analytics-node # Or @segment/analytics-node
// lib/segment-server.ts
import { Analytics } from '@segment/analytics-node';
export const analyticsServer = new Analytics({
writeKey: process.env.SEGMENT_WRITE_KEY!,
});
// Track server-side event
analyticsServer.track({
userId: user.id,
event: 'Subscription Created',
properties: {
plan: 'pro',
amount: 2900,
currency: 'usd',
mrr: 29.00,
},
});
// Identify from server
analyticsServer.identify({
userId: user.id,
traits: {
email: user.email,
plan: 'pro',
lifetime_value: 348.00,
},
});
5. Connect Destinations
In Segment Dashboard → Destinations, add tools:
| Destination | Purpose | Data Sent |
|---|---|---|
| Google Analytics 4 | Web analytics | Page views, events |
| Mixpanel | Product analytics | Events, user profiles |
| Amplitude | Product analytics | Events, user properties |
| HubSpot | CRM | Identify calls, events |
| Intercom | Customer messaging | Identify, track |
| BigQuery | Data warehouse | All events (raw) |
| Slack | Notifications | Filtered events |
| Braze | Marketing automation | Events, user traits |
Destination Filters
Only send relevant events to each destination:
Google Analytics ← All events
Mixpanel ← Product events only (exclude marketing events)
HubSpot ← Identify calls + Purchase events only
BigQuery ← Everything (raw data lake)
Slack ← Purchase events only (revenue notifications)
6. Debugging
Segment Debugger
In Segment Dashboard → Sources → Your Source → Debugger:
- See events in real time
- Verify properties are correct
- Check which destinations received each event
Local Development
// Enable debug mode
analytics.debug(true);
// Events log to console instead of sending to Segment
Pricing
| Plan | Visitors/Month | Price |
|---|---|---|
| Free | 1,000 | $0 |
| Team | 10,000 | $120/month |
| Business | Custom | Custom |
"Visitors" = unique users tracked per month (not events).
Common Mistakes
| Mistake | Impact | Fix |
|---|---|---|
| Tracking everything | Noise, high costs | Define a tracking plan first (15-20 events max) |
| Inconsistent event names | Can't query data | Use "Object Action" format consistently |
Not calling identify() | Can't connect pre/post-login | Identify after every login |
| Client-side only tracking | Missed server events, unreliable revenue | Use server-side for important events |
| No tracking plan documentation | New devs add random events | Document events in a spreadsheet/wiki |
| Sending PII to all destinations | Privacy violations | Use destination filters to control data flow |
Building your data stack? Compare Segment vs PostHog vs Rudderstack on APIScout — customer data platforms compared.