跳转到正文

快速入门

GitHub

你可以使用我们的 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-rudderstack
bunx 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 辅助工具。

初始化 RudderStack 客户端。

该方法保留了 Cordova 的签名,因此第二个参数可以是 config 对象或 Rudder options 对象。

import { nativePlugin } from '@capgo/capacitor-rudderstack';
await nativePlugin.initialize('write-key-123');

为提供的用户 ID 发送 identify 调用。

import { nativePlugin } from '@capgo/capacitor-rudderstack';
await nativePlugin.identify('user-id-123');

为提供的 group ID 发送 group 调用。

import { nativePlugin } from '@capgo/capacitor-rudderstack';
await nativePlugin.group('group-id-123');

为提供的事件名称发送 track 调用。

import { nativePlugin } from '@capgo/capacitor-rudderstack';
await nativePlugin.track('event');

为提供的屏幕名称发送 screen 调用。

import { nativePlugin } from '@capgo/capacitor-rudderstack';
await nativePlugin.screen('screen');

将当前用户关联到新的标识符。

import { nativePlugin } from '@capgo/capacitor-rudderstack';
await nativePlugin.alias('new-id-123');

重置当前的 RudderStack 身份状态。

import { nativePlugin } from '@capgo/capacitor-rudderstack';
await nativePlugin.reset();

立即刷新队列中的事件。

import { nativePlugin } from '@capgo/capacitor-rudderstack';
await nativePlugin.flush();

设置 RudderStack 转发至支持的目标平台的 push token。

import { nativePlugin } from '@capgo/capacitor-rudderstack';
await nativePlugin.putDeviceToken('device-token-123');

查看当前契约的源码定义。

import { nativePlugin } from '@capgo/capacitor-rudderstack';
await nativePlugin.setAdvertisingId('advertising-id-123');

设置自定义广告 ID 值。

import { nativePlugin } from '@capgo/capacitor-rudderstack';
await nativePlugin.putAdvertisingId('advertising-id-123');

查看当前契约的源码定义。

import { nativePlugin } from '@capgo/capacitor-rudderstack';
await nativePlugin.setAnonymousId('anonymous-id-123');

设置自定义匿名 ID 值。

import { nativePlugin } from '@capgo/capacitor-rudderstack';
await nativePlugin.putAnonymousId('anonymous-id-123');

切换 RudderStack 追踪退出状态。

import { nativePlugin } from '@capgo/capacitor-rudderstack';
await nativePlugin.optOut(true);

底层 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[];
}

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>;
}

支持的 Traits 负载数据: identify 以及 group.

export type RudderTraits = Record<string, any>;

支持的 Properties 负载数据: track 以及 screen.

export type RudderProperties = Record<string, any>;

插件版本 payload。

export interface PluginVersionResult {
/**
* Version identifier returned by the platform implementation.
*/
version: string;
}

为方便迁移而提供的 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 的实现细节。