@adv-re/segment-wrapper

中文文档
5.1.4发布于 2026/04/27ISCTypeScriptESM + CJS近 30 天下载 5328

Modern TypeScript abstraction layer on top of the Segment library

原始使用文档
文档目录21 节
  1. Features
  2. Installation
  3. Quick Start
  4. Basic Usage (ESM/TypeScript)
  5. UMD Usage (Browser)
  6. Configuration
  7. API Reference
  8. Core Methods
  9. Utilities
  10. Advanced Features
  11. Middleware System
  12. TCF/GDPR Compliance
  13. Campaign Tracking
  14. Migration Guides
  15. Migrating from v4 to v5
  16. Migrating from @s-ui/segment-wrapper
  17. Browser Support
  18. Bundle Size
  19. Development
  20. Documentation
  21. License

@adv-re/segment-wrapper

Modern TypeScript abstraction layer on top of the Segment Analytics library with built-in support for Segment integrations, Adobe Analytics, TCF compliance, and consent management.

Features

  • ✨ TypeScript Support - Full type safety and IntelliSense
  • 🔒 TCF/GDPR Compliance - Built-in Transparency & Consent Framework support
  • 🎯 Adobe Analytics - Adobe Visitor API integration
  • 🔌 Middleware System - Extensible source and destination middlewares
  • 📊 Campaign Tracking - UTM and STC parameter parsing and forwarding
  • 🆔 Universal ID - User identification across domains
  • 📦 Multiple Formats - ESM, CommonJS, and UMD builds
  • 🧪 Well Tested - Comprehensive test coverage

Installation

npm install @adv-re/segment-wrapper

Quick Start

Basic Usage (ESM/TypeScript)

import analytics from '@adv-re/segment-wrapper'
import {initConfig} from '@adv-re/segment-wrapper'

// Initialize configuration (optional - for custom settings)
initConfig({
  userIdPrefix: 'fc-',
  adobeMarketingCloudOrgId: 'YOUR_ORG_ID@AdobeOrg',
  sendXandrId: true,
  defaultProperties: {
    platform: 'web'
  }
})

// Track events
await analytics.track('Button Clicked', {
  buttonName: 'Sign Up',
  location: 'Homepage'
})

// Track page views
await analytics.page('Homepage', {
  title: 'Welcome to Our Site'
})

// Identify users
await analytics.identify('user-123', {
  email: '[email protected]',
  plan: 'premium'
})

UMD Usage (Browser)

<script src="https://cdn.segment.com/analytics.js/v1/YOUR_WRITE_KEY/analytics.min.js"></script>
<!-- Use specific version (recommended for production) -->
<script async src="https://unpkg.com/@adv-re/segment-wrapper@latest"></script>

<script>
  // Access via window.sui.analytics
  window.sui.analytics.track('Page Viewed', {
    page: 'home'
  })
</script>

Version Options:

<!-- Specific version (recommended for production) -->
<script async src="https://unpkg.com/@adv-re/[email protected]"></script>

<!-- Latest version (use only for development) -->
<script async src="https://unpkg.com/@adv-re/segment-wrapper"></script>

Configuration

import {initConfig} from '@adv-re/segment-wrapper'

initConfig({
  // Segment API Key
  key: 'xOoVeB57iXT3X1ehtxLk0Ipte3eIYTZw',

  // User ID Configuration
  userIdPrefix: 'fc-',

  // Adobe Analytics
  adobeMarketingCloudOrgId: 'YOUR_ORG_ID@AdobeOrg',

  // Xandr ID
  sendXandrId: true,

  // Default Properties (added to all track calls)
  defaultProperties: {
    platform: 'web',
    app: 'my-app'
  },

  // Middlewares
  experimentalPageDataMiddleware: false
})

See Configuration Guide for all available options.

API Reference

Core Methods

  • analytics.track(event, properties?, context?, callback?) - Track custom events
  • analytics.page(name?, properties?, context?, callback?) - Track page views
  • analytics.identify(userId, traits?, options?, callback?) - Identify users
  • analytics.reset() - Reset user identification

Utilities

  • getConfig(key) - Get configuration value
  • setConfig(key, value) - Set configuration value
  • clearConfig() - Clear all configuration
  • getUniversalId() - Get current Universal ID
  • getAdobeMCVisitorID() - Get Adobe Marketing Cloud Visitor ID
  • getAdobeVisitorData() - Get Adobe Visitor API data
  • getCampaignDetails() - Get UTM/STC campaign parameters from URL

See API Reference for detailed documentation.

Advanced Features

Middleware System

The segment-wrapper includes a powerful middleware system that allows you to modify tracking data before it's sent:

// Middlewares are automatically registered on initialization
// They enrich events with:
// - User traits (anonymous ID, user ID)
// - Campaign context (UTM parameters, STC tracking)
// - Page referrer information
// - Screen dimensions
// - Default context properties

See Middleware Documentation for more details.

TCF/GDPR Compliance

Built-in support for Transparency & Consent Framework (TCF) v2:

import {getGdprPrivacyValue, checkAnalyticsGdprIsAccepted} from '@adv-re/segment-wrapper'

// Get current GDPR consent state
const gdprValue = await getGdprPrivacyValue()
// { analytics: 'accepted', advertising: 'declined' }

// Check if analytics tracking is allowed
const canTrack = checkAnalyticsGdprIsAccepted(gdprValue)

Events are automatically blocked or modified based on user consent.

See TCF Documentation for more details.

Campaign Tracking

Automatic UTM and STC campaign parameter parsing:

import {getCampaignDetails} from '@adv-re/segment-wrapper'

// Get campaign details from URL
const campaign = getCampaignDetails()
// Returns: { utm_source: 'google', utm_medium: 'cpc', ... }

// Campaign parameters are automatically added to all events
// via the campaignContext middleware

Features:

  • UTM parameter parsing (utm_source, utm_medium, utm_campaign, etc.)
  • STC format support (custom Adevinta format: ?stc=medium-source-campaign)
  • Automatic forwarding to Segment (and downstream destinations like GA4)

Migration Guides

Migrating from v4 to v5

See Upgrade to v5 Guide for step-by-step instructions on upgrading to version 5.

Key changes in v5:

  • GA4 now managed by Segment Device Mode (no more direct gtag.js initialization)
  • Removed googleAnalyticsMeasurementId, googleAnalyticsConfig, etc. configuration options
  • Consent Mode sent as event properties instead of direct gtag calls
  • Campaign tracking still supported (UTM/STC parameters)

Migrating from @s-ui/segment-wrapper

See Migration Guide for step-by-step instructions on migrating from the legacy @s-ui/segment-wrapper package.

Key changes:

  • Package name: @s-ui/segment-wrapper → @adv-re/segment-wrapper
  • Full TypeScript support with type definitions
  • Modern build system (tsup instead of sui-bundler)
  • Improved tree-shaking and bundle size

Browser Support

  • Chrome/Edge (last 2 versions)
  • Firefox (last 2 versions)
  • Safari (last 2 versions)
  • iOS Safari (last 2 versions)

Bundle Size

  • ESM: ~34KB (minified)
  • CommonJS: ~36KB (minified)
  • UMD: ~15KB (minified)

Development

# Install dependencies
npm install

# Run tests
npm test

# Build
npm run build

# Build UMD bundle
npm run build:umd

# Lint
npm run lint

Documentation

License

ISC

版本与发布记录

@adv-re/segment-wrapper 共发布 24 个版本,首次发布于 2026/01/12;过去 12 个月发布了 24 个版本。

版本发布日期距今
5.1.4latest2026/04/276 个月前
5.1.32026/04/236 个月前
5.1.22026/04/236 个月前
5.1.12026/04/136 个月前
5.1.02026/04/136 个月前
5.0.22026/03/247 个月前

其他发布通道:beta → 4.36.2-beta.3,beta-v5 → 5.0.0-beta.2。

关于 @adv-re/segment-wrapper 的常见问题

@adv-re/segment-wrapper 是什么?

Modern TypeScript abstraction layer on top of the Segment library

如何安装 @adv-re/segment-wrapper?

在项目目录运行 npm install @adv-re/segment-wrapper,或使用 pnpm add / yarn add / bun add。

@adv-re/segment-wrapper 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@adv-re/segment-wrapper 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@adv-re/segment-wrapper 可以商用吗?

@adv-re/segment-wrapper 使用 ISC 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@adv-re/segment-wrapper 的下载量有多少?

根据 npm 官方下载统计,@adv-re/segment-wrapper 在 2026/10/05 – 2026/11/03 这 30 天内被下载 5,328 次,日均约 178 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@adv-re/segment-wrapper 还在维护吗?

最新版本 5.1.4 发布于 2026/04/27(6 个月前);至今共发布 24 个版本,其中过去 12 个月发布了 24 个;首次发布于 2026/01/12。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。