@van-hung/nativescript-segment
by van-hung | v1.1.0
一个 NativeScript 插件,提供对原生 Segment SDK 的轻松访问。
npm i --save @van-hung/nativescript-segment

Nativescript-Segment

一个 NativeScript 插件,提供对原生 Segment SDK 的轻松访问。主要基于此 仓库

安装

tns plugin add nativescript-segment

若要访问原生 SDK 类型定义,请在您的 references.d.ts 中指定定义

/// <reference path="./node_modules/nativescript-segment/platforms/android/typings/android.d.ts" />
/// <reference path="./node_modules/nativescript-segment/platforms/ios/typings/ios.d.ts" />

警告:根据您的项目结构,上述路径可能不准确。

用法

示例

所有与库的交互都应该通过在 Segment 导入上调用静态函数来完成,因为 iOS 和 Android SDK 会在调用 configure 方法成功后,将 Segment 实例化为单例。

const config: SegmentConfig = {
trackLifeCycleEvents: true,
recordScreenViews: true
};
Segment.configure(SEGMENT_KEY, config); // SEGMENT_KEY being your secret key
import { Segment } from 'nativescript-segment';

public someInteraction(type: string) {
Segment.track(type);
}

对于更高级的使用,或者如果需要访问基础 SDK 方法,您可以访问 SDK 的共享实例

// iOS
Segment.ios.track('some event');

// Android
Segment.android.track('some event');

警告:直接访问 SDK 的方法可能需要转换为原生对象和集合类型

平台特定

Android

如果您的应用中尚未启用,则需要 网络 权限。

<uses-permission android:name="android.permission.INTERNET"/>

iOS 最佳实践

在您的应用程序的代理中

import { Segment } from 'nativescript-segment';

public applicationDidFinishLaunchingWithOptions(application, launchOptions): boolean {
const config = {
setDebug: true; // set to show full debug logging by the native APIs
}
Segment.configure(key, config);
return true;
}

API

configure

const config: SegmentConfig = {
trackLifeCycleEvents: true,
recordScreenViews: true
};
Segment.configure('your segment write key', config);

SegmentConfig 属性(全部为可选属性)

属性 默认值 描述
trackLifeCycleEvents true 启用或禁用自动跟踪生命周期事件
recordScreenViews true true
enable or disable auto tracking of screen views options
Default integration options, see SegmentOptions proxyUrl null
forward all Segment calls through a proxy false set base INFO logging in Android SDK and plugin itself
setDebug false Sets full debug logging in Android and iOS
middlewaresAndroid [] Android 的中间件列表。按数组顺序应用。有关更多信息,请参阅 此处
middlewaresIOS [] iOS 的中间件列表。按数组顺序应用。有关更多信息,请参阅 此处

SegmentOptions 属性(全部为可选属性)

属性 默认值 描述
useAll true enables all integrations (default for Segment SDKs)
excluded [] exclude Segment from integrating with the specified services
included [] include Segment integration with the specified services (note: this will only take affect if useAll is set to false)

identify

Identify the current user. Additional traits are supported, and custom traits are available.

const traits: SegmentTraits = {
firstName: 'Dave',
email: '[email protected]'
};
const customTraits: any {
favoriteColor: 'blue'
};
Segment.identify('userId', traits, customTraits);

SegmentTraits 属性(全部为可选属性)

有关所有可用特性和其描述,请参阅 Segment 的官方规范

track

Track an event.

Segment.track('Some event');

const properties = {
productName: 'Bread',
revenue: 4
};
Segment.track('Product Purchased', properties);

有关要添加到 track 调用的属性的详细信息,请参阅 Segment 的官方规范

screen

通过名称和可选类别手动记录屏幕视图。类别是 Android 的默认选项,但对于 iOS,它将 categoryname 连接到相同的 screen

Segment.screen('signup', 'verify password');

请参阅Segment官方规范,了解关于屏幕调用的详细信息。

分组

将当前用户与一个分组关联。一个用户可以属于多个分组。

Segment.group("0e8c78ea9d97a7b8185e8632", {
name: "Initech",
industry: "Technology",
employees: 329,
plan: "enterprise",
"total billed": 830
});

请参阅Segment官方规范,了解关于分组调用的详细信息。

别名

别名是您将一个身份与另一个身份关联的方式。

Segment.alias(newId);

请参阅Segment官方规范,了解关于别名调用的详细信息。

optout

禁用或启用所有分析,设置在应用重启期间保持不变。

Segment.optOut(true);

许可证

Apache许可证版本2.0,2004年1月