Advanced Segment Cdp
Production-ready skill that handles expert, patterns, segment, customer. Includes structured workflows, validation checks, and reusable patterns for web development.
Segment CDP
A customer data platform skill for implementing Segment analytics, managing event tracking, building customer data pipelines, and configuring integrations with downstream tools.
When to Use
Choose Segment CDP when:
- Implementing unified customer event tracking across web, mobile, and server
- Building a customer data pipeline that feeds multiple analytics and marketing tools
- Managing user identity resolution and cross-device tracking
- Setting up event schemas and data governance for analytics
Consider alternatives when:
- Simple page view tracking only — use Google Analytics directly
- Server-side analytics without client tracking — use database event logging
- Real-time event streaming — use Apache Kafka or AWS Kinesis
Quick Start
# Install Segment analytics.js npm install @segment/analytics-next
import { AnalyticsBrowser } from '@segment/analytics-next'; const analytics = AnalyticsBrowser.load({ writeKey: 'YOUR_WRITE_KEY' }); // Identify a user analytics.identify('user-123', { name: 'Jane Smith', email: '[email protected]', plan: 'premium', created_at: '2024-01-15' }); // Track an event analytics.track('Product Viewed', { product_id: 'SKU-001', product_name: 'Premium Widget', price: 49.99, category: 'Widgets', currency: 'USD' }); // Track a page view analytics.page('Home', { title: 'Welcome to Our App', url: window.location.href, referrer: document.referrer }); // Group users into organizations analytics.group('org-456', { name: 'Acme Corp', industry: 'Technology', employees: 150, plan: 'enterprise' });
# Server-side tracking with Python import analytics analytics.write_key = 'YOUR_WRITE_KEY' class SegmentTracker: def track_event(self, user_id, event_name, properties=None): analytics.track(user_id, event_name, properties or {}) def identify_user(self, user_id, traits): analytics.identify(user_id, traits) def track_purchase(self, user_id, order): # Ecommerce tracking following Segment spec analytics.track(user_id, 'Order Completed', { 'order_id': order['id'], 'total': order['total'], 'revenue': order['revenue'], 'currency': order['currency'], 'products': [ { 'product_id': item['id'], 'name': item['name'], 'price': item['price'], 'quantity': item['quantity'] } for item in order['items'] ] }) def flush(self): analytics.flush()
Core Concepts
Segment API Methods
| Method | Purpose | Example |
|---|---|---|
identify | Associate traits with a user | Name, email, plan |
track | Record a user action | Button click, purchase |
page | Record a page view | Page title, URL |
group | Associate user with organization | Company, team |
alias | Merge two user identities | Anonymous → authenticated |
screen | Record mobile screen view | Screen name, properties |
Event Tracking Schema
// Define a typed tracking plan interface TrackingEvents { 'Product Viewed': { product_id: string; product_name: string; price: number; category: string; }; 'Product Added': { product_id: string; quantity: number; cart_id: string; }; 'Checkout Started': { order_id: string; total: number; products: Array<{ product_id: string; quantity: number }>; }; 'Order Completed': { order_id: string; total: number; revenue: number; currency: string; }; 'Signup Completed': { method: 'email' | 'google' | 'github'; plan: string; }; } class TypedAnalytics { private analytics: AnalyticsBrowser; constructor(writeKey: string) { this.analytics = AnalyticsBrowser.load({ writeKey }); } track<K extends keyof TrackingEvents>( event: K, properties: TrackingEvents[K] ) { this.analytics.track(event, properties); } }
Configuration
| Option | Description | Default |
|---|---|---|
writeKey | Segment source write key | Required |
apiHost | Custom API endpoint | "api.segment.io/v1" |
retryCount | Retry attempts for failed events | 3 |
flushAt | Events to batch before sending | 20 |
flushInterval | Max ms between flushes | 10000 |
enable | Enable/disable tracking | true |
integrations | Per-integration settings | {} |
cdnURL | Custom CDN for analytics.js | Default CDN |
Best Practices
- Define a tracking plan before implementation with event names, properties, and types documented in a shared spreadsheet or Segment Protocols — ad-hoc event tracking leads to inconsistent data that is unusable for analysis
- Use consistent naming conventions for events (Title Case like "Product Viewed") and properties (snake_case like
product_id) across all platforms to ensure data joins correctly in downstream tools - Call
identifybeforetrackso events are associated with the correct user from the start; anonymous events without identity resolution create gaps in user journey analysis - Implement event validation using Segment Protocols or client-side type checking to catch schema violations before they pollute your data warehouse with malformed events
- Batch server-side events using
flushAtandflushIntervalsettings to minimize API calls while ensuring events are sent within a reasonable time window
Common Issues
Anonymous to authenticated identity stitching: Users browse anonymously before logging in, creating two separate profiles. Call analytics.alias(newUserId, anonymousId) immediately after authentication to merge the anonymous browsing history with the authenticated user profile.
Event volume exceeding plan limits: Tracking every UI interaction generates enormous event volumes. Focus on business-meaningful events (conversions, key feature usage, errors) rather than tracking every click, and use sampling or aggregation for high-frequency events like scroll depth.
Client-side ad blockers blocking Segment: Browser ad blockers and privacy extensions block requests to api.segment.io. Set up a Segment proxy through your own domain to route analytics requests through a first-party endpoint that ad blockers do not recognize.
Reviews
No reviews yet. Be the first to review this template!
Similar Templates
Full-Stack Code Reviewer
Comprehensive code review skill that checks for security vulnerabilities, performance issues, accessibility, and best practices across frontend and backend code.
Test Suite Generator
Generates comprehensive test suites with unit tests, integration tests, and edge cases. Supports Jest, Vitest, Pytest, and Go testing.
Pro Architecture Workspace
Battle-tested skill for architectural, decision, making, framework. Includes structured workflows, validation checks, and reusable patterns for development.