快速入门
复制包含安装步骤和该插件完整 Markdown 指南的配置 prompt。
Set up this Capacitor plugin in the project.
Use the package manager already used by the project.
Install these package(s): `@capgo/capacitor-rudderstack`
Run the required Capacitor sync/update step after installation.
Read this markdown guide for the full setup steps: https://raw.githubusercontent.com/Cap-go/website/refs/heads/main/apps/docs/src/content/docs/docs/plugins/rudderstack/getting-started.mdx
Use that guide for platform-specific steps, native file edits, permissions, config changes, imports, and usage setup.
If that guide references other docs pages, read them too.
安装
“安装”章节你可以使用我们的 AI 辅助设置来安装插件。使用以下命令将 Capgo skills 添加到你的 AI 工具中:
npx skills add https://github.com/Cap-go/capgo-skills --skill capacitor-plugins然后使用以下 prompt:
Use the `capacitor-plugins` skill from `Cap-go/capgo-skills` to install the `@capgo/capacitor-rudderstack` plugin in my project.如果你更想手动设置,请运行以下命令安装插件,并按照下方各平台的说明操作:
bun add @capgo/capacitor-rudderstackbunx cap sync导入
“导入”章节import { nativePlugin } from '@capgo/capacitor-rudderstack';快速入门
章节标题为“快速入门”import { nativePlugin } from '@capgo/capacitor-rudderstack';
await nativePlugin.initialize({ writeKey: 'YOUR_WRITE_KEY', dataPlaneUrl: 'https://your.dataplane.rudderstack.com',});await nativePlugin.identify({ userId: 'user_123', traits: { plan: 'pro' } });await nativePlugin.track({ event: 'Signed Up', properties: { source: 'mobile' } });示例:屏幕追踪
标题为“示例:屏幕追踪”的部分await nativePlugin.screen({ screen: 'Checkout', properties: { step: 'payment' },});常见坑点
章节标题为“常见陷阱”- 初始化一次:重复
initialize()多次调用可能会导致 SDK 状态重复,请在应用的 bootstrap 阶段进行拦截。 - 进入后台前刷新: 调用
flush()因为 iOS 或 Android 可能会在 upload 过程中挂起你的进程。 - 广告 ID:仅当你的隐私政策涵盖 IDFA 或 GAID 收集时,才使用 opt in 辅助工具。
API 概览
章节标题:“API 概览”initialize
“initialize” 章节初始化 RudderStack 客户端。
该方法保留了 Cordova 的签名,因此第二个参数可以是 config 对象或 Rudder options 对象。
import { nativePlugin } from '@capgo/capacitor-rudderstack';
await nativePlugin.initialize('write-key-123');identify
标题为 “identify” 的章节为提供的用户 ID 发送 identify 调用。
import { nativePlugin } from '@capgo/capacitor-rudderstack';
await nativePlugin.identify('user-id-123');group
标题为 “group” 的章节为提供的 group ID 发送 group 调用。
import { nativePlugin } from '@capgo/capacitor-rudderstack';
await nativePlugin.group('group-id-123');track
标题为 “track” 的章节为提供的事件名称发送 track 调用。
import { nativePlugin } from '@capgo/capacitor-rudderstack';
await nativePlugin.track('event');screen
标题为 “screen” 的章节为提供的屏幕名称发送 screen 调用。
import { nativePlugin } from '@capgo/capacitor-rudderstack';
await nativePlugin.screen('screen');alias
“alias” 章节将当前用户关联到新的标识符。
import { nativePlugin } from '@capgo/capacitor-rudderstack';
await nativePlugin.alias('new-id-123');reset
“reset” 章节重置当前的 RudderStack 身份状态。
import { nativePlugin } from '@capgo/capacitor-rudderstack';
await nativePlugin.reset();flush
“flush” 章节立即刷新队列中的事件。
import { nativePlugin } from '@capgo/capacitor-rudderstack';
await nativePlugin.flush();putDeviceToken
“putDeviceToken” 章节设置 RudderStack 转发至支持的目标平台的 push token。
import { nativePlugin } from '@capgo/capacitor-rudderstack';
await nativePlugin.putDeviceToken('device-token-123');setAdvertisingId
标题为 “setAdvertisingId” 的章节查看当前契约的源码定义。
import { nativePlugin } from '@capgo/capacitor-rudderstack';
await nativePlugin.setAdvertisingId('advertising-id-123');putAdvertisingId
标题为 “putAdvertisingId” 的章节设置自定义广告 ID 值。
import { nativePlugin } from '@capgo/capacitor-rudderstack';
await nativePlugin.putAdvertisingId('advertising-id-123');setAnonymousId
标题为 “setAnonymousId” 的章节查看当前契约的源码定义。
import { nativePlugin } from '@capgo/capacitor-rudderstack';
await nativePlugin.setAnonymousId('anonymous-id-123');putAnonymousId
标题为 “putAnonymousId” 的章节设置自定义匿名 ID 值。
import { nativePlugin } from '@capgo/capacitor-rudderstack';
await nativePlugin.putAnonymousId('anonymous-id-123');optOut
“optOut” 章节切换 RudderStack 追踪退出状态。
import { nativePlugin } from '@capgo/capacitor-rudderstack';
await nativePlugin.optOut(true);类型参考
“类型参考” 章节RudderConfiguration
“RudderConfiguration” 章节底层 RudderStack native SDK 支持的配置键。
export interface RudderConfiguration { /** * RudderStack data plane URL. */ dataPlaneUrl?: string;
/** * Number of events to batch before a flush. */ flushQueueSize?: number;
/** * Database row threshold that triggers pruning on Android and iOS. */ dbCountThreshold?: number;
/** * Server config refresh interval in hours. */ configRefreshInterval?: number;
/** * RudderStack log verbosity. */ logLevel?: RudderLogLevelValue;
/** * Sleep timeout / sleep count used by the native SDK. */ sleepTimeOut?: number;
/** * Android only. Lets the native SDK collect the advertising identifier automatically. */ autoCollectAdvertId?: boolean;
/** * Tracks `Application Installed`, `Application Updated`, and `Application Opened` automatically. */ trackLifecycleEvents?: boolean;
/** * RudderStack control plane URL. */ controlPlaneUrl?: string;
/** * Enables automatic screen tracking where supported by the native SDK. */ recordScreenViews?: boolean;
/** * Ignored in this Capacitor port. * * The Cordova SDK uses this field to bootstrap native destination factories from companion plugins. * Those extension packages are not implemented in this first Capacitor release. */ factories?: any[];}RudderOptions
“RudderOptions” 章节RudderStack 单次调用选项。
export interface RudderOptions { /** * External identifiers forwarded with the event. */ externalIds?: Record<string, string>;
/** * Destination enablement flags keyed by integration name. */ integrations?: Record<string, boolean>;}RudderTraits
“RudderTraits” 章节支持的 Traits 负载数据: identify 以及 group.
export type RudderTraits = Record<string, any>;RudderProperties
“RudderProperties” 章节支持的 Properties 负载数据: track 以及 screen.
export type RudderProperties = Record<string, any>;PluginVersionResult
标题为“PluginVersionResult”的章节插件版本 payload。
export interface PluginVersionResult { /** * Version identifier returned by the platform implementation. */ version: string;}RudderLogLevelValue
标题为 “RudderLogLevelValue” 的章节为方便迁移而提供的 RudderStack 日志级别值。
export type RudderLogLevelValue = 0 | 1 | 2 | 3 | 4 | 5;单一事实来源
标题为“单一事实来源”的部分本页面由插件的 src/definitions.ts生成。当上游的公共 API 发生变化时,请重新运行 sync。
从入门指南继续
“从入门指南继续”章节如果你正在使用 快速入门 要规划 dashboard 和 API 操作,请将其连接到 使用 @capgo/capacitor-rudderstack 用于“使用 @capgo/capacitor-rudderstack”中的原生能力, API 概览 关于 API Overview 中的实现细节, 简介 关于 Introduction 中的实现细节, API 密钥 有关 API Keys 的实现细节,以及 设备 有关 Devices 的实现细节。