opencode-skills-collection 1.0.185 → 1.0.187
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bundled-skills/.antigravity-install-manifest.json +5 -1
- package/bundled-skills/3d-web-experience/SKILL.md +152 -37
- package/bundled-skills/agent-evaluation/SKILL.md +1088 -26
- package/bundled-skills/agent-memory-systems/SKILL.md +1037 -25
- package/bundled-skills/agent-tool-builder/SKILL.md +668 -16
- package/bundled-skills/ai-agents-architect/SKILL.md +271 -31
- package/bundled-skills/ai-product/SKILL.md +716 -26
- package/bundled-skills/ai-wrapper-product/SKILL.md +450 -44
- package/bundled-skills/algolia-search/SKILL.md +867 -15
- package/bundled-skills/autonomous-agents/SKILL.md +1033 -26
- package/bundled-skills/aws-serverless/SKILL.md +1046 -35
- package/bundled-skills/azure-functions/SKILL.md +1318 -19
- package/bundled-skills/browser-automation/SKILL.md +1065 -28
- package/bundled-skills/browser-extension-builder/SKILL.md +159 -32
- package/bundled-skills/bullmq-specialist/SKILL.md +347 -16
- package/bundled-skills/clerk-auth/SKILL.md +796 -15
- package/bundled-skills/computer-use-agents/SKILL.md +1870 -28
- package/bundled-skills/context-window-management/SKILL.md +271 -18
- package/bundled-skills/conversation-memory/SKILL.md +453 -24
- package/bundled-skills/crewai/SKILL.md +252 -46
- package/bundled-skills/discord-bot-architect/SKILL.md +1207 -34
- package/bundled-skills/docs/integrations/jetski-cortex.md +3 -3
- package/bundled-skills/docs/integrations/jetski-gemini-loader/README.md +1 -1
- package/bundled-skills/docs/maintainers/repo-growth-seo.md +3 -3
- package/bundled-skills/docs/maintainers/skills-update-guide.md +1 -1
- package/bundled-skills/docs/users/bundles.md +1 -1
- package/bundled-skills/docs/users/claude-code-skills.md +1 -1
- package/bundled-skills/docs/users/gemini-cli-skills.md +1 -1
- package/bundled-skills/docs/users/getting-started.md +1 -1
- package/bundled-skills/docs/users/kiro-integration.md +1 -1
- package/bundled-skills/docs/users/usage.md +4 -4
- package/bundled-skills/docs/users/visual-guide.md +4 -4
- package/bundled-skills/email-systems/SKILL.md +646 -26
- package/bundled-skills/faf-expert/SKILL.md +221 -0
- package/bundled-skills/faf-wizard/SKILL.md +252 -0
- package/bundled-skills/file-uploads/SKILL.md +212 -11
- package/bundled-skills/firebase/SKILL.md +646 -16
- package/bundled-skills/gcp-cloud-run/SKILL.md +1117 -32
- package/bundled-skills/graphql/SKILL.md +1026 -27
- package/bundled-skills/hubspot-integration/SKILL.md +804 -19
- package/bundled-skills/idea-darwin/SKILL.md +120 -0
- package/bundled-skills/inngest/SKILL.md +431 -16
- package/bundled-skills/interactive-portfolio/SKILL.md +342 -44
- package/bundled-skills/langfuse/SKILL.md +296 -41
- package/bundled-skills/langgraph/SKILL.md +259 -50
- package/bundled-skills/micro-saas-launcher/SKILL.md +343 -44
- package/bundled-skills/neon-postgres/SKILL.md +572 -15
- package/bundled-skills/nextjs-supabase-auth/SKILL.md +269 -21
- package/bundled-skills/notion-template-business/SKILL.md +371 -44
- package/bundled-skills/personal-tool-builder/SKILL.md +537 -44
- package/bundled-skills/plaid-fintech/SKILL.md +825 -19
- package/bundled-skills/prompt-caching/SKILL.md +438 -25
- package/bundled-skills/rag-engineer/SKILL.md +271 -29
- package/bundled-skills/salesforce-development/SKILL.md +912 -19
- package/bundled-skills/satori/SKILL.md +54 -0
- package/bundled-skills/scroll-experience/SKILL.md +381 -44
- package/bundled-skills/segment-cdp/SKILL.md +817 -19
- package/bundled-skills/shopify-apps/SKILL.md +1475 -19
- package/bundled-skills/slack-bot-builder/SKILL.md +1162 -28
- package/bundled-skills/telegram-bot-builder/SKILL.md +152 -37
- package/bundled-skills/telegram-mini-app/SKILL.md +445 -44
- package/bundled-skills/trigger-dev/SKILL.md +916 -27
- package/bundled-skills/twilio-communications/SKILL.md +1310 -28
- package/bundled-skills/upstash-qstash/SKILL.md +898 -27
- package/bundled-skills/vercel-deployment/SKILL.md +637 -39
- package/bundled-skills/viral-generator-builder/SKILL.md +132 -37
- package/bundled-skills/voice-agents/SKILL.md +937 -27
- package/bundled-skills/voice-ai-development/SKILL.md +375 -46
- package/bundled-skills/workflow-automation/SKILL.md +982 -29
- package/bundled-skills/zapier-make-patterns/SKILL.md +772 -27
- package/package.json +1 -1
|
@@ -1,13 +1,19 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: segment-cdp
|
|
3
|
-
description:
|
|
3
|
+
description: Expert patterns for Segment Customer Data Platform including
|
|
4
|
+
Analytics.js, server-side tracking, tracking plans with Protocols, identity
|
|
5
|
+
resolution, destinations configuration, and data governance best practices.
|
|
4
6
|
risk: safe
|
|
5
|
-
source:
|
|
6
|
-
date_added:
|
|
7
|
+
source: vibeship-spawner-skills (Apache 2.0)
|
|
8
|
+
date_added: 2026-02-27
|
|
7
9
|
---
|
|
8
10
|
|
|
9
11
|
# Segment CDP
|
|
10
12
|
|
|
13
|
+
Expert patterns for Segment Customer Data Platform including Analytics.js,
|
|
14
|
+
server-side tracking, tracking plans with Protocols, identity resolution,
|
|
15
|
+
destinations configuration, and data governance best practices.
|
|
16
|
+
|
|
11
17
|
## Patterns
|
|
12
18
|
|
|
13
19
|
### Analytics.js Browser Integration
|
|
@@ -15,38 +21,830 @@ date_added: "2026-02-27"
|
|
|
15
21
|
Client-side tracking with Analytics.js. Include track, identify, page,
|
|
16
22
|
and group calls. Anonymous ID persists until identify merges with user.
|
|
17
23
|
|
|
24
|
+
// Next.js - Analytics provider component
|
|
25
|
+
// lib/segment.ts
|
|
26
|
+
import { AnalyticsBrowser } from '@segment/analytics-next';
|
|
27
|
+
|
|
28
|
+
export const analytics = AnalyticsBrowser.load({
|
|
29
|
+
writeKey: process.env.NEXT_PUBLIC_SEGMENT_WRITE_KEY!,
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
// Typed event helpers
|
|
33
|
+
export interface UserTraits {
|
|
34
|
+
email?: string;
|
|
35
|
+
name?: string;
|
|
36
|
+
plan?: 'free' | 'pro' | 'enterprise';
|
|
37
|
+
createdAt?: string;
|
|
38
|
+
company?: {
|
|
39
|
+
id: string;
|
|
40
|
+
name: string;
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function identify(userId: string, traits?: UserTraits) {
|
|
45
|
+
analytics.identify(userId, traits);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function track<T extends Record<string, any>>(
|
|
49
|
+
event: string,
|
|
50
|
+
properties?: T
|
|
51
|
+
) {
|
|
52
|
+
analytics.track(event, properties);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function page(name?: string, properties?: Record<string, any>) {
|
|
56
|
+
analytics.page(name, properties);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function group(groupId: string, traits?: Record<string, any>) {
|
|
60
|
+
analytics.group(groupId, traits);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// React hook for analytics
|
|
64
|
+
// hooks/useAnalytics.ts
|
|
65
|
+
import { useEffect } from 'react';
|
|
66
|
+
import { usePathname, useSearchParams } from 'next/navigation';
|
|
67
|
+
import { analytics, page } from '@/lib/segment';
|
|
68
|
+
|
|
69
|
+
export function usePageTracking() {
|
|
70
|
+
const pathname = usePathname();
|
|
71
|
+
const searchParams = useSearchParams();
|
|
72
|
+
|
|
73
|
+
useEffect(() => {
|
|
74
|
+
// Track page view on route change
|
|
75
|
+
page(pathname, {
|
|
76
|
+
path: pathname,
|
|
77
|
+
search: searchParams.toString(),
|
|
78
|
+
url: window.location.href,
|
|
79
|
+
title: document.title,
|
|
80
|
+
});
|
|
81
|
+
}, [pathname, searchParams]);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// Usage in _app.tsx or layout.tsx
|
|
85
|
+
function RootLayout({ children }) {
|
|
86
|
+
usePageTracking();
|
|
87
|
+
|
|
88
|
+
return <html>{children}</html>;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// Event tracking in components
|
|
92
|
+
function PricingButton({ plan }: { plan: string }) {
|
|
93
|
+
const handleClick = () => {
|
|
94
|
+
track('Plan Selected', {
|
|
95
|
+
plan_name: plan,
|
|
96
|
+
page: 'pricing',
|
|
97
|
+
source: 'pricing_page',
|
|
98
|
+
});
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
return <button onClick={handleClick}>Select {plan}</button>;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// Identify on auth
|
|
105
|
+
function onUserLogin(user: User) {
|
|
106
|
+
identify(user.id, {
|
|
107
|
+
email: user.email,
|
|
108
|
+
name: user.name,
|
|
109
|
+
plan: user.plan,
|
|
110
|
+
createdAt: user.createdAt,
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
track('User Signed In', {
|
|
114
|
+
method: 'email',
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
### Context
|
|
119
|
+
|
|
120
|
+
- browser tracking
|
|
121
|
+
- website analytics
|
|
122
|
+
- client-side events
|
|
123
|
+
|
|
18
124
|
### Server-Side Tracking with Node.js
|
|
19
125
|
|
|
20
126
|
High-performance server-side tracking using @segment/analytics-node.
|
|
21
127
|
Non-blocking with internal batching. Essential for backend events,
|
|
22
128
|
webhooks, and sensitive data.
|
|
23
129
|
|
|
130
|
+
// lib/segment-server.ts
|
|
131
|
+
import { Analytics } from '@segment/analytics-node';
|
|
132
|
+
|
|
133
|
+
// Initialize once
|
|
134
|
+
const analytics = new Analytics({
|
|
135
|
+
writeKey: process.env.SEGMENT_WRITE_KEY!,
|
|
136
|
+
flushAt: 20, // Batch size before flush
|
|
137
|
+
flushInterval: 10000, // Flush every 10 seconds
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
// Typed server-side tracking
|
|
141
|
+
export interface ServerContext {
|
|
142
|
+
ip?: string;
|
|
143
|
+
userAgent?: string;
|
|
144
|
+
locale?: string;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export function serverIdentify(
|
|
148
|
+
userId: string,
|
|
149
|
+
traits: Record<string, any>,
|
|
150
|
+
context?: ServerContext
|
|
151
|
+
) {
|
|
152
|
+
analytics.identify({
|
|
153
|
+
userId,
|
|
154
|
+
traits,
|
|
155
|
+
context: {
|
|
156
|
+
ip: context?.ip,
|
|
157
|
+
userAgent: context?.userAgent,
|
|
158
|
+
locale: context?.locale,
|
|
159
|
+
},
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export function serverTrack(
|
|
164
|
+
userId: string,
|
|
165
|
+
event: string,
|
|
166
|
+
properties?: Record<string, any>,
|
|
167
|
+
context?: ServerContext
|
|
168
|
+
) {
|
|
169
|
+
analytics.track({
|
|
170
|
+
userId,
|
|
171
|
+
event,
|
|
172
|
+
properties,
|
|
173
|
+
timestamp: new Date(),
|
|
174
|
+
context: {
|
|
175
|
+
ip: context?.ip,
|
|
176
|
+
userAgent: context?.userAgent,
|
|
177
|
+
},
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// Flush on shutdown
|
|
182
|
+
export async function closeAnalytics() {
|
|
183
|
+
await analytics.closeAndFlush();
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// Usage in API routes
|
|
187
|
+
// app/api/webhooks/stripe/route.ts
|
|
188
|
+
export async function POST(req: Request) {
|
|
189
|
+
const event = await req.json();
|
|
190
|
+
|
|
191
|
+
switch (event.type) {
|
|
192
|
+
case 'checkout.session.completed':
|
|
193
|
+
const session = event.data.object;
|
|
194
|
+
|
|
195
|
+
serverTrack(
|
|
196
|
+
session.client_reference_id,
|
|
197
|
+
'Order Completed',
|
|
198
|
+
{
|
|
199
|
+
order_id: session.id,
|
|
200
|
+
total: session.amount_total / 100,
|
|
201
|
+
currency: session.currency,
|
|
202
|
+
payment_method: session.payment_method_types[0],
|
|
203
|
+
},
|
|
204
|
+
{ ip: req.headers.get('x-forwarded-for') || undefined }
|
|
205
|
+
);
|
|
206
|
+
|
|
207
|
+
// Also update user traits
|
|
208
|
+
serverIdentify(session.client_reference_id, {
|
|
209
|
+
total_spent: session.amount_total / 100,
|
|
210
|
+
last_purchase_date: new Date().toISOString(),
|
|
211
|
+
});
|
|
212
|
+
break;
|
|
213
|
+
|
|
214
|
+
case 'customer.subscription.created':
|
|
215
|
+
serverTrack(
|
|
216
|
+
event.data.object.metadata.user_id,
|
|
217
|
+
'Subscription Started',
|
|
218
|
+
{
|
|
219
|
+
plan: event.data.object.items.data[0].price.nickname,
|
|
220
|
+
amount: event.data.object.items.data[0].price.unit_amount / 100,
|
|
221
|
+
interval: event.data.object.items.data[0].price.recurring.interval,
|
|
222
|
+
}
|
|
223
|
+
);
|
|
224
|
+
break;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
return new Response('ok');
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// Graceful shutdown
|
|
231
|
+
process.on('SIGTERM', async () => {
|
|
232
|
+
await closeAnalytics();
|
|
233
|
+
process.exit(0);
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
### Context
|
|
237
|
+
|
|
238
|
+
- server-side tracking
|
|
239
|
+
- backend events
|
|
240
|
+
- webhook processing
|
|
241
|
+
|
|
24
242
|
### Tracking Plan Design
|
|
25
243
|
|
|
26
244
|
Design event schemas using Object + Action naming convention.
|
|
27
245
|
Define required properties, types, and validation rules.
|
|
28
246
|
Connect to Protocols for enforcement.
|
|
29
247
|
|
|
30
|
-
|
|
248
|
+
// Tracking plan definition (conceptual YAML structure)
|
|
249
|
+
// This maps to Segment Protocols configuration
|
|
250
|
+
/*
|
|
251
|
+
tracking_plan:
|
|
252
|
+
display_name: "MyApp Tracking Plan"
|
|
253
|
+
rules:
|
|
254
|
+
events:
|
|
255
|
+
- name: "User Signed Up"
|
|
256
|
+
description: "User completed registration"
|
|
257
|
+
rules:
|
|
258
|
+
required:
|
|
259
|
+
- signup_method
|
|
260
|
+
properties:
|
|
261
|
+
signup_method:
|
|
262
|
+
type: string
|
|
263
|
+
enum: [email, google, github]
|
|
264
|
+
referral_code:
|
|
265
|
+
type: string
|
|
266
|
+
utm_source:
|
|
267
|
+
type: string
|
|
268
|
+
|
|
269
|
+
- name: "Product Viewed"
|
|
270
|
+
description: "User viewed a product page"
|
|
271
|
+
rules:
|
|
272
|
+
required:
|
|
273
|
+
- product_id
|
|
274
|
+
- product_name
|
|
275
|
+
properties:
|
|
276
|
+
product_id:
|
|
277
|
+
type: string
|
|
278
|
+
product_name:
|
|
279
|
+
type: string
|
|
280
|
+
category:
|
|
281
|
+
type: string
|
|
282
|
+
price:
|
|
283
|
+
type: number
|
|
284
|
+
currency:
|
|
285
|
+
type: string
|
|
286
|
+
default: USD
|
|
287
|
+
|
|
288
|
+
- name: "Order Completed"
|
|
289
|
+
description: "User completed a purchase"
|
|
290
|
+
rules:
|
|
291
|
+
required:
|
|
292
|
+
- order_id
|
|
293
|
+
- total
|
|
294
|
+
- products
|
|
295
|
+
properties:
|
|
296
|
+
order_id:
|
|
297
|
+
type: string
|
|
298
|
+
total:
|
|
299
|
+
type: number
|
|
300
|
+
currency:
|
|
301
|
+
type: string
|
|
302
|
+
products:
|
|
303
|
+
type: array
|
|
304
|
+
items:
|
|
305
|
+
type: object
|
|
306
|
+
properties:
|
|
307
|
+
product_id: { type: string }
|
|
308
|
+
name: { type: string }
|
|
309
|
+
price: { type: number }
|
|
310
|
+
quantity: { type: integer }
|
|
311
|
+
|
|
312
|
+
identify:
|
|
313
|
+
traits:
|
|
314
|
+
- name: email
|
|
315
|
+
type: string
|
|
316
|
+
required: true
|
|
317
|
+
- name: name
|
|
318
|
+
type: string
|
|
319
|
+
- name: plan
|
|
320
|
+
type: string
|
|
321
|
+
enum: [free, pro, enterprise]
|
|
322
|
+
- name: company
|
|
323
|
+
type: object
|
|
324
|
+
properties:
|
|
325
|
+
id: { type: string }
|
|
326
|
+
name: { type: string }
|
|
327
|
+
*/
|
|
328
|
+
|
|
329
|
+
// TypeScript implementation with type safety
|
|
330
|
+
// types/segment-events.ts
|
|
331
|
+
export interface TrackingEvents {
|
|
332
|
+
'User Signed Up': {
|
|
333
|
+
signup_method: 'email' | 'google' | 'github';
|
|
334
|
+
referral_code?: string;
|
|
335
|
+
utm_source?: string;
|
|
336
|
+
};
|
|
337
|
+
|
|
338
|
+
'Product Viewed': {
|
|
339
|
+
product_id: string;
|
|
340
|
+
product_name: string;
|
|
341
|
+
category?: string;
|
|
342
|
+
price?: number;
|
|
343
|
+
currency?: string;
|
|
344
|
+
};
|
|
345
|
+
|
|
346
|
+
'Order Completed': {
|
|
347
|
+
order_id: string;
|
|
348
|
+
total: number;
|
|
349
|
+
currency?: string;
|
|
350
|
+
products: Array<{
|
|
351
|
+
product_id: string;
|
|
352
|
+
name: string;
|
|
353
|
+
price: number;
|
|
354
|
+
quantity: number;
|
|
355
|
+
}>;
|
|
356
|
+
};
|
|
357
|
+
|
|
358
|
+
'Feature Used': {
|
|
359
|
+
feature_name: string;
|
|
360
|
+
usage_count?: number;
|
|
361
|
+
};
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
// Type-safe track function
|
|
365
|
+
export function trackEvent<T extends keyof TrackingEvents>(
|
|
366
|
+
event: T,
|
|
367
|
+
properties: TrackingEvents[T]
|
|
368
|
+
) {
|
|
369
|
+
analytics.track(event, properties);
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
// Usage - compile-time type checking
|
|
373
|
+
trackEvent('Order Completed', {
|
|
374
|
+
order_id: 'ord_123',
|
|
375
|
+
total: 99.99,
|
|
376
|
+
products: [
|
|
377
|
+
{ product_id: 'prod_1', name: 'Widget', price: 49.99, quantity: 2 },
|
|
378
|
+
],
|
|
379
|
+
});
|
|
380
|
+
|
|
381
|
+
// This would be a TypeScript error:
|
|
382
|
+
// trackEvent('Order Completed', { total: 99.99 }); // Missing order_id
|
|
383
|
+
|
|
384
|
+
### Context
|
|
385
|
+
|
|
386
|
+
- tracking plan
|
|
387
|
+
- data governance
|
|
388
|
+
- event schema
|
|
389
|
+
|
|
390
|
+
### Identity Resolution
|
|
391
|
+
|
|
392
|
+
Track anonymous users, then merge with identified users via identify().
|
|
393
|
+
Use alias() for identity merging between systems. Group users into
|
|
394
|
+
companies/organizations.
|
|
395
|
+
|
|
396
|
+
// Identity flow implementation
|
|
397
|
+
// lib/identity.ts
|
|
398
|
+
|
|
399
|
+
// Anonymous user tracking
|
|
400
|
+
export function trackAnonymousAction(event: string, properties?: object) {
|
|
401
|
+
// Analytics.js automatically generates anonymousId
|
|
402
|
+
analytics.track(event, properties);
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
// When user signs up or logs in
|
|
406
|
+
export async function identifyUser(user: {
|
|
407
|
+
id: string;
|
|
408
|
+
email: string;
|
|
409
|
+
name?: string;
|
|
410
|
+
plan?: string;
|
|
411
|
+
}) {
|
|
412
|
+
// This merges anonymous history with user profile
|
|
413
|
+
await analytics.identify(user.id, {
|
|
414
|
+
email: user.email,
|
|
415
|
+
name: user.name,
|
|
416
|
+
plan: user.plan,
|
|
417
|
+
created_at: new Date().toISOString(),
|
|
418
|
+
});
|
|
419
|
+
|
|
420
|
+
// Track the identification event
|
|
421
|
+
analytics.track('User Identified', {
|
|
422
|
+
method: 'signup',
|
|
423
|
+
});
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
// B2B: Associate user with company
|
|
427
|
+
export function associateWithCompany(company: {
|
|
428
|
+
id: string;
|
|
429
|
+
name: string;
|
|
430
|
+
plan?: string;
|
|
431
|
+
employees?: number;
|
|
432
|
+
industry?: string;
|
|
433
|
+
}) {
|
|
434
|
+
analytics.group(company.id, {
|
|
435
|
+
name: company.name,
|
|
436
|
+
plan: company.plan,
|
|
437
|
+
employees: company.employees,
|
|
438
|
+
industry: company.industry,
|
|
439
|
+
});
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
// Alias: Link identities (e.g., pre-signup email to user ID)
|
|
443
|
+
export function linkIdentities(previousId: string, newUserId: string) {
|
|
444
|
+
// Use when you identified someone with a temporary ID
|
|
445
|
+
// and now have their permanent user ID
|
|
446
|
+
analytics.alias(newUserId, previousId);
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
// Full signup flow
|
|
450
|
+
export async function handleSignup(
|
|
451
|
+
email: string,
|
|
452
|
+
password: string,
|
|
453
|
+
company?: { name: string; size: string }
|
|
454
|
+
) {
|
|
455
|
+
// 1. Create user in your system
|
|
456
|
+
const user = await createUser(email, password);
|
|
457
|
+
|
|
458
|
+
// 2. Identify with Segment (merges anonymous history)
|
|
459
|
+
await identifyUser({
|
|
460
|
+
id: user.id,
|
|
461
|
+
email: user.email,
|
|
462
|
+
name: user.name,
|
|
463
|
+
plan: 'free',
|
|
464
|
+
});
|
|
465
|
+
|
|
466
|
+
// 3. Track signup event
|
|
467
|
+
analytics.track('User Signed Up', {
|
|
468
|
+
signup_method: 'email',
|
|
469
|
+
plan: 'free',
|
|
470
|
+
});
|
|
471
|
+
|
|
472
|
+
// 4. If B2B, associate with company
|
|
473
|
+
if (company) {
|
|
474
|
+
const companyRecord = await createCompany(company, user.id);
|
|
475
|
+
|
|
476
|
+
associateWithCompany({
|
|
477
|
+
id: companyRecord.id,
|
|
478
|
+
name: company.name,
|
|
479
|
+
employees: parseInt(company.size),
|
|
480
|
+
});
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
### Context
|
|
485
|
+
|
|
486
|
+
- user identification
|
|
487
|
+
- anonymous tracking
|
|
488
|
+
- b2b tracking
|
|
489
|
+
|
|
490
|
+
### Destinations Configuration
|
|
491
|
+
|
|
492
|
+
Route data to analytics tools, data warehouses, and marketing platforms.
|
|
493
|
+
Use device-mode for client-side tools, cloud-mode for server processing.
|
|
494
|
+
|
|
495
|
+
// Segment destinations are configured in the Segment UI
|
|
496
|
+
// but here's how to optimize your implementation
|
|
497
|
+
|
|
498
|
+
// Conditional tracking based on destination needs
|
|
499
|
+
// lib/segment-destinations.ts
|
|
500
|
+
|
|
501
|
+
interface DestinationConfig {
|
|
502
|
+
mixpanel: boolean;
|
|
503
|
+
amplitude: boolean;
|
|
504
|
+
googleAnalytics: boolean;
|
|
505
|
+
warehouse: boolean;
|
|
506
|
+
hubspot: boolean;
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
// Only send events needed by specific destinations
|
|
510
|
+
export function trackWithDestinations(
|
|
511
|
+
event: string,
|
|
512
|
+
properties: Record<string, any>,
|
|
513
|
+
options?: {
|
|
514
|
+
integrations?: Partial<DestinationConfig>;
|
|
515
|
+
}
|
|
516
|
+
) {
|
|
517
|
+
analytics.track(event, properties, {
|
|
518
|
+
integrations: {
|
|
519
|
+
// Override specific destinations
|
|
520
|
+
All: true, // Send to all by default
|
|
521
|
+
...options?.integrations,
|
|
522
|
+
},
|
|
523
|
+
});
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
// Example: Track revenue event only to revenue-tracking destinations
|
|
527
|
+
export function trackRevenue(order: {
|
|
528
|
+
orderId: string;
|
|
529
|
+
total: number;
|
|
530
|
+
currency: string;
|
|
531
|
+
}) {
|
|
532
|
+
analytics.track('Order Completed', {
|
|
533
|
+
order_id: order.orderId,
|
|
534
|
+
revenue: order.total,
|
|
535
|
+
currency: order.currency,
|
|
536
|
+
}, {
|
|
537
|
+
integrations: {
|
|
538
|
+
// Explicitly enable revenue destinations
|
|
539
|
+
'Google Analytics 4': true,
|
|
540
|
+
'Mixpanel': true,
|
|
541
|
+
'Amplitude': true,
|
|
542
|
+
// Disable non-revenue destinations
|
|
543
|
+
'Intercom': false,
|
|
544
|
+
'Zendesk': false,
|
|
545
|
+
},
|
|
546
|
+
});
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
// Send PII only to secure destinations
|
|
550
|
+
export function identifyWithPII(userId: string, traits: {
|
|
551
|
+
email: string;
|
|
552
|
+
phone?: string;
|
|
553
|
+
address?: string;
|
|
554
|
+
}) {
|
|
555
|
+
analytics.identify(userId, traits, {
|
|
556
|
+
integrations: {
|
|
557
|
+
'All': false, // Disable all by default
|
|
558
|
+
// Only send PII to trusted destinations
|
|
559
|
+
'HubSpot': true,
|
|
560
|
+
'Salesforce': true,
|
|
561
|
+
'Warehouse': true, // Your data warehouse
|
|
562
|
+
// Don't send PII to analytics tools
|
|
563
|
+
'Mixpanel': false,
|
|
564
|
+
'Amplitude': false,
|
|
565
|
+
},
|
|
566
|
+
});
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
// Context enrichment for all events
|
|
570
|
+
export function enrichedTrack(
|
|
571
|
+
event: string,
|
|
572
|
+
properties: Record<string, any>
|
|
573
|
+
) {
|
|
574
|
+
analytics.track(event, {
|
|
575
|
+
...properties,
|
|
576
|
+
// Add common context
|
|
577
|
+
app_version: process.env.NEXT_PUBLIC_APP_VERSION,
|
|
578
|
+
environment: process.env.NODE_ENV,
|
|
579
|
+
timestamp: new Date().toISOString(),
|
|
580
|
+
}, {
|
|
581
|
+
context: {
|
|
582
|
+
app: {
|
|
583
|
+
name: 'MyApp',
|
|
584
|
+
version: process.env.NEXT_PUBLIC_APP_VERSION,
|
|
585
|
+
},
|
|
586
|
+
},
|
|
587
|
+
});
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
### Context
|
|
31
591
|
|
|
32
|
-
|
|
592
|
+
- data routing
|
|
593
|
+
- destination setup
|
|
594
|
+
- tool integration
|
|
33
595
|
|
|
34
|
-
###
|
|
596
|
+
### HTTP Tracking API
|
|
35
597
|
|
|
36
|
-
|
|
598
|
+
Direct HTTP API for any environment. Useful for edge functions,
|
|
599
|
+
workers, and non-Node.js backends. Batch up to 500KB per request.
|
|
37
600
|
|
|
38
|
-
|
|
601
|
+
// Edge/Serverless tracking via HTTP API
|
|
602
|
+
// lib/segment-http.ts
|
|
39
603
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
604
|
+
const SEGMENT_WRITE_KEY = process.env.SEGMENT_WRITE_KEY!;
|
|
605
|
+
const SEGMENT_API = 'https://api.segment.io/v1';
|
|
606
|
+
|
|
607
|
+
// Base64 encode write key for auth
|
|
608
|
+
const authHeader = `Basic ${btoa(SEGMENT_WRITE_KEY + ':')}`;
|
|
609
|
+
|
|
610
|
+
interface SegmentEvent {
|
|
611
|
+
userId?: string;
|
|
612
|
+
anonymousId?: string;
|
|
613
|
+
event?: string;
|
|
614
|
+
name?: string; // For page calls
|
|
615
|
+
properties?: Record<string, any>;
|
|
616
|
+
traits?: Record<string, any>;
|
|
617
|
+
context?: Record<string, any>;
|
|
618
|
+
timestamp?: string;
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
async function segmentRequest(
|
|
622
|
+
endpoint: string,
|
|
623
|
+
payload: SegmentEvent
|
|
624
|
+
): Promise<void> {
|
|
625
|
+
const response = await fetch(`${SEGMENT_API}${endpoint}`, {
|
|
626
|
+
method: 'POST',
|
|
627
|
+
headers: {
|
|
628
|
+
'Authorization': authHeader,
|
|
629
|
+
'Content-Type': 'application/json',
|
|
630
|
+
},
|
|
631
|
+
body: JSON.stringify({
|
|
632
|
+
...payload,
|
|
633
|
+
timestamp: payload.timestamp || new Date().toISOString(),
|
|
634
|
+
}),
|
|
635
|
+
});
|
|
636
|
+
|
|
637
|
+
if (!response.ok) {
|
|
638
|
+
console.error('Segment API error:', await response.text());
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
// HTTP API methods
|
|
643
|
+
export async function httpIdentify(
|
|
644
|
+
userId: string,
|
|
645
|
+
traits: Record<string, any>,
|
|
646
|
+
context?: Record<string, any>
|
|
647
|
+
) {
|
|
648
|
+
await segmentRequest('/identify', {
|
|
649
|
+
userId,
|
|
650
|
+
traits,
|
|
651
|
+
context,
|
|
652
|
+
});
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
export async function httpTrack(
|
|
656
|
+
userId: string,
|
|
657
|
+
event: string,
|
|
658
|
+
properties?: Record<string, any>,
|
|
659
|
+
context?: Record<string, any>
|
|
660
|
+
) {
|
|
661
|
+
await segmentRequest('/track', {
|
|
662
|
+
userId,
|
|
663
|
+
event,
|
|
664
|
+
properties,
|
|
665
|
+
context,
|
|
666
|
+
});
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
export async function httpPage(
|
|
670
|
+
userId: string,
|
|
671
|
+
name: string,
|
|
672
|
+
properties?: Record<string, any>
|
|
673
|
+
) {
|
|
674
|
+
await segmentRequest('/page', {
|
|
675
|
+
userId,
|
|
676
|
+
name,
|
|
677
|
+
properties,
|
|
678
|
+
});
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
// Batch API for high volume
|
|
682
|
+
export async function httpBatch(
|
|
683
|
+
events: Array<{
|
|
684
|
+
type: 'identify' | 'track' | 'page' | 'group';
|
|
685
|
+
userId?: string;
|
|
686
|
+
anonymousId?: string;
|
|
687
|
+
event?: string;
|
|
688
|
+
name?: string;
|
|
689
|
+
properties?: Record<string, any>;
|
|
690
|
+
traits?: Record<string, any>;
|
|
691
|
+
}>
|
|
692
|
+
) {
|
|
693
|
+
// Max 500KB per batch, 32KB per event
|
|
694
|
+
await segmentRequest('/batch', {
|
|
695
|
+
batch: events.map(e => ({
|
|
696
|
+
...e,
|
|
697
|
+
timestamp: new Date().toISOString(),
|
|
698
|
+
})),
|
|
699
|
+
} as any);
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
// Cloudflare Worker example
|
|
703
|
+
export default {
|
|
704
|
+
async fetch(request: Request): Promise<Response> {
|
|
705
|
+
const { userId, action, data } = await request.json();
|
|
706
|
+
|
|
707
|
+
// Track in edge function
|
|
708
|
+
await httpTrack(userId, action, data, {
|
|
709
|
+
ip: request.headers.get('cf-connecting-ip'),
|
|
710
|
+
userAgent: request.headers.get('user-agent'),
|
|
711
|
+
});
|
|
712
|
+
|
|
713
|
+
return new Response('ok');
|
|
714
|
+
},
|
|
715
|
+
};
|
|
716
|
+
|
|
717
|
+
### Context
|
|
718
|
+
|
|
719
|
+
- edge functions
|
|
720
|
+
- serverless
|
|
721
|
+
- http tracking
|
|
722
|
+
|
|
723
|
+
## Sharp Edges
|
|
724
|
+
|
|
725
|
+
### Anonymous ID Persists Until Explicit Reset
|
|
726
|
+
|
|
727
|
+
Severity: MEDIUM
|
|
728
|
+
|
|
729
|
+
### Device Mode Bypasses Protocols Blocking
|
|
730
|
+
|
|
731
|
+
Severity: HIGH
|
|
732
|
+
|
|
733
|
+
### HTTP API Has Strict Size Limits
|
|
734
|
+
|
|
735
|
+
Severity: MEDIUM
|
|
736
|
+
|
|
737
|
+
### Track Calls Without Identify Are Anonymous
|
|
738
|
+
|
|
739
|
+
Severity: HIGH
|
|
740
|
+
|
|
741
|
+
### Write Key in Client is Visible (But Intentional)
|
|
742
|
+
|
|
743
|
+
Severity: LOW
|
|
744
|
+
|
|
745
|
+
### Events May Be Lost on Page Navigation
|
|
746
|
+
|
|
747
|
+
Severity: MEDIUM
|
|
748
|
+
|
|
749
|
+
### Timestamps Without Timezone Cause Analytics Issues
|
|
750
|
+
|
|
751
|
+
Severity: MEDIUM
|
|
752
|
+
|
|
753
|
+
### Tracking Before Consent Violates GDPR
|
|
754
|
+
|
|
755
|
+
Severity: HIGH
|
|
756
|
+
|
|
757
|
+
## Validation Checks
|
|
758
|
+
|
|
759
|
+
### Dynamic Event Name
|
|
760
|
+
|
|
761
|
+
Severity: ERROR
|
|
762
|
+
|
|
763
|
+
Event names should be static, not include dynamic values
|
|
764
|
+
|
|
765
|
+
Message: Dynamic event name detected. Use static event names with dynamic properties.
|
|
766
|
+
|
|
767
|
+
### Inconsistent Event Name Casing
|
|
768
|
+
|
|
769
|
+
Severity: WARNING
|
|
770
|
+
|
|
771
|
+
Event names should follow consistent casing convention
|
|
772
|
+
|
|
773
|
+
Message: Mixed casing in event name. Use consistent convention (e.g., Title Case).
|
|
774
|
+
|
|
775
|
+
### Track Without Prior Identify
|
|
776
|
+
|
|
777
|
+
Severity: WARNING
|
|
778
|
+
|
|
779
|
+
Users should be identified before tracking critical events
|
|
780
|
+
|
|
781
|
+
Message: Revenue/conversion event without identify. Ensure user is identified.
|
|
782
|
+
|
|
783
|
+
### Missing Analytics Reset on Logout
|
|
784
|
+
|
|
785
|
+
Severity: WARNING
|
|
786
|
+
|
|
787
|
+
Analytics should be reset when user logs out
|
|
788
|
+
|
|
789
|
+
Message: Logout without analytics.reset(). Anonymous ID will persist to next user.
|
|
790
|
+
|
|
791
|
+
### Hardcoded Segment Write Key
|
|
792
|
+
|
|
793
|
+
Severity: ERROR
|
|
794
|
+
|
|
795
|
+
Write key should use environment variables
|
|
796
|
+
|
|
797
|
+
Message: Hardcoded Segment write key. Use environment variables.
|
|
798
|
+
|
|
799
|
+
### PII Sent to All Destinations
|
|
800
|
+
|
|
801
|
+
Severity: WARNING
|
|
802
|
+
|
|
803
|
+
PII should have destination controls
|
|
804
|
+
|
|
805
|
+
Message: PII in tracking without destination controls. Consider limiting destinations.
|
|
806
|
+
|
|
807
|
+
### Event Without Proper Timestamp
|
|
808
|
+
|
|
809
|
+
Severity: INFO
|
|
810
|
+
|
|
811
|
+
Explicit timestamps help with historical data
|
|
812
|
+
|
|
813
|
+
Message: Server track without explicit timestamp. Consider adding timestamp.
|
|
814
|
+
|
|
815
|
+
### Potentially Large Property Values
|
|
816
|
+
|
|
817
|
+
Severity: WARNING
|
|
818
|
+
|
|
819
|
+
Properties over 32KB will be rejected
|
|
820
|
+
|
|
821
|
+
Message: Potentially large property value. Segment has 32KB per event limit.
|
|
822
|
+
|
|
823
|
+
### Tracking Before Consent Check
|
|
824
|
+
|
|
825
|
+
Severity: ERROR
|
|
826
|
+
|
|
827
|
+
GDPR requires consent before tracking
|
|
828
|
+
|
|
829
|
+
Message: Tracking without consent check. Implement consent management for GDPR.
|
|
830
|
+
|
|
831
|
+
## Collaboration
|
|
832
|
+
|
|
833
|
+
### Delegation Triggers
|
|
834
|
+
|
|
835
|
+
- user needs A/B testing -> analytics-specialist (Segment + LaunchDarkly/Optimizely integration)
|
|
836
|
+
- user needs data warehouse -> data-engineer (Segment to BigQuery/Snowflake/Redshift)
|
|
837
|
+
- user needs customer support integration -> zendesk-integration (Identify calls syncing to support tools)
|
|
838
|
+
- user needs marketing automation -> hubspot-integration (Segment to HubSpot destination)
|
|
839
|
+
- user needs consent management -> privacy-specialist (GDPR/CCPA compliance with Segment)
|
|
50
840
|
|
|
51
841
|
## When to Use
|
|
52
|
-
|
|
842
|
+
|
|
843
|
+
- User mentions or implies: segment
|
|
844
|
+
- User mentions or implies: analytics.js
|
|
845
|
+
- User mentions or implies: customer data platform
|
|
846
|
+
- User mentions or implies: cdp
|
|
847
|
+
- User mentions or implies: tracking plan
|
|
848
|
+
- User mentions or implies: event tracking
|
|
849
|
+
- User mentions or implies: identify track page
|
|
850
|
+
- User mentions or implies: data routing
|