@aws/clickstream-react-native

中文文档
0.3.3发布于 2025/04/16Apache-2.0TypeScriptESM + CJS零依赖近 30 天下载 2355

Clickstream Analytics React Native SDK

原始使用文档
文档目录11 节
  1. Introduction
  2. Platform Support
  3. Integrate SDK
  4. Include SDK
  5. Initialize the SDK
  6. Start using
  7. How to integrate and test locally
  8. Test
  9. Collection of operational metrics
  10. Security
  11. License

AWS Solution Clickstream Analytics SDK for React Native

Introduction

Clickstream React Native SDK can help you easily collect and report events from React Native apps to AWS. This SDK is part of an AWS solution - Clickstream Analytics on AWS, which provisions data pipeline to ingest and process event data into AWS services such as Amazon S3, Redshift.

The SDK relies on the Clickstream Android SDK and Clickstream Swift SDK. Therefore, Clickstream React Native SDK also supports automatically collect common preset events and attributes (e.g., session start, first open). In addition, we've added easy-to-use APIs to simplify data collection in React Native apps.

Visit our Documentation site to learn more about Clickstream React Native SDK.

Platform Support

Android: Supports Android 4.1 (API level 16) and later.

iOS: Supports iOS 13 and later.

Integrate SDK

Include SDK

$ npm install @aws/clickstream-react-native

After complete, you need to install the pod dependencies for iOS:

$ cd ios && pod install

Initialize the SDK

Copy your configuration code from your clickstream solution web console, we recommended you add the code to your app's entry point like index.js, the configuration code should look like as follows. You can also manually add this code snippet and replace the values of appId and endpoint after you registered app to a data pipeline in the Clickstream Analytics solution web console.

// index.js

import { ClickstreamAnalytics } from '@aws/clickstream-react-native';

ClickstreamAnalytics.init({
  appId: 'your appId',
  endpoint: 'https://example.com/collect',
});

Your appId and endpoint are already set up in it.

Make sure you call ClickstreamAnalytics.init as early as possible in your application’s life-cycle. And make sure the SDK is initialized when calling other APIs.

Start using

Record event

Add the following code where you need to record events.

import { ClickstreamAnalytics } from '@aws/clickstream-react-native';

// record event with attributes
ClickstreamAnalytics.record({
  name: 'button_click',
  attributes: {
    event_category: 'shoes',
    currency: 'CNY',
    value: 279.9,
  },
});

// record event with name
ClickstreamAnalytics.record({ name: 'button_click' });

Login and logout

import { ClickstreamAnalytics } from '@aws/clickstream-react-native';

// when user login success.
ClickstreamAnalytics.setUserId("userId");

// when user logout
ClickstreamAnalytics.setUserId(null);

Add user attribute

import { ClickstreamAnalytics } from '@aws/clickstream-react-native';

ClickstreamAnalytics.setUserAttributes({
  userName: "carl",
  userAge: 22
});

When opening the Apps for the first time after integrating the SDK, you need to manually set the user attributes once, and current login user's attributes will be cached in disk, so the next time app open you don't need to set all user attributes again, of course you can use the same API ClickstreamAnalytics.setUserAttributes() to update the current user's attribute when it changes.

Add global attribute

  1. Add global attributes when initializing the SDK

    The following example code shows how to add traffic source fields as global attributes when initializing the SDK.

    import { ClickstreamAnalytics, Attr } from '@aws/clickstream-react-native';
    ClickstreamAnalytics.init({
       appId: 'your appId',
       endpoint: 'https://example.com/collect',
       globalAttributes:{
         [Attr.TRAFFIC_SOURCE_SOURCE]: 'amazon',
         [Attr.TRAFFIC_SOURCE_MEDIUM]: 'cpc',
         [Attr.TRAFFIC_SOURCE_CAMPAIGN]: 'summer_promotion',
         [Attr.TRAFFIC_SOURCE_CAMPAIGN_ID]: 'summer_promotion_01',
         [Attr.TRAFFIC_SOURCE_TERM]: 'running_shoes',
         [Attr.TRAFFIC_SOURCE_CONTENT]: 'banner_ad_1',
         [Attr.TRAFFIC_SOURCE_CLID]: 'amazon_ad_123',
         [Attr.TRAFFIC_SOURCE_CLID_PLATFORM]: 'amazon_ads',
         [Attr.APP_INSTALL_CHANNEL]: 'amazon_store',
       },
    });
    
  2. Add global attributes after initializing the SDK

    import { ClickstreamAnalytics, Attr } from '@aws/clickstream-react-native';
    
    ClickstreamAnalytics.setGlobalAttributes({
      [Attr.TRAFFIC_SOURCE_MEDIUM]: "Search engine",
      level: 10,
    });
    

It is recommended to set global attributes when initializing the SDK, global attributes will be included in all events that occur after it is set.

Delete global attribute

ClickstreamAnalytics.deleteGlobalAttributes(['level','_traffic_source_medium']);

Record event with items

You can add the following code to log an event with an item.

Note: Only pipelines from version 1.1+ can handle items with custom attribute.

import { ClickstreamAnalytics, Item, Attr } from '@aws/clickstream-react-native';

const itemBook: Item = {
  id: '123',
  name: 'Nature',
  category: 'book',
  price: 99,
  book_publisher: "Nature Research",
};
ClickstreamAnalytics.record({
  name: 'view_item',
  attributes: {
    [Attr.VALUE]: 99,
    [Attr.CURRENCY]: 'USD',
    event_category: 'recommended',
  },
  items: [itemBook],
});

Record Screen View events manually

By default, SDK will automatically track the preset _screen_view event when Android Activity triggers onResume or iOS ViewController triggers viewDidAppear.

You can also manually record screen view events whether automatic screen view tracking is enabled, add the following code to record a screen view event with two attributes.

  • SCREEN_NAME Required. Your screen's name.
  • SCREEN_UNIQUE_ID Optional. Set the id of your Component. If you do not set, the SDK will set a default value based on the hashcode of the current Activity or ViewController.
import { ClickstreamAnalytics, Attr, Event } from '@aws/clickstream-react-native';

ClickstreamAnalytics.record({
  name: Event.SCREEN_VIEW,
  attributes: {
    [Attr.SCREEN_NAME]: 'HomeComponet',
    [Attr.SCREEN_UNIQUE_ID]: '123adf',
  },
});

Record screen views when using React Navigation

Here's an example of globally logging React Native screen view events when using React Navigation 6.x

For other version of React Navigation, you can refer to official documentation: Screen tracking for analytics.

Other configurations

In addition to the required appId and endpoint, you can configure other information to get more customized usage when initialing the SDK:

import { ClickstreamAnalytics } from '@aws/clickstream-react-native';

ClickstreamAnalytics.init({
  appId: 'your appId',
  endpoint: 'https://example.com/collect',
  isLogEvents: true,
  isCompressEvents: true,
  isTrackScreenViewEvents: false,
  isTrackUserEngagementEvents: true,
  isTrackAppExceptionEvents: true,
  sendEventsInterval: 15000,
  sessionTimeoutDuration: 1800000,
  authCookie: 'your auth cookie',
  globalAttributes: {
    _traffic_source_medium: 'Search engine',
  },
});

Here is an explanation of each property:

  • appId (Required): the app id of your project in solution web control.
  • endpoint (Required): the endpoint path you will upload the event to ingestion server.
  • isLogEvents: whether to print out event json for debugging, default is false.
  • isCompressEvents: whether to compress event content when uploading events, default is true.
  • sendEventsInterval: event sending interval millisecond, works only bath send mode, the default value is 5000
  • isTrackScreenViewEvents: whether auto record native screen view events, default is true
  • isTrackUserEngagementEvents: whether auto record user engagement events, default is true
  • isTrackAppExceptionEvents: whether auto record exception events of native apps, default is false
  • sendEventsInterval: event sending interval millisecond, the default value is 10000 (10s)
  • sessionTimeoutDuration: the duration for session timeout millisecond, default is 1800000 (30 minutes)
  • authCookie: your auth cookie for AWS application load balancer auth cookie.
  • globalAttributes: the global attributes when initializing the SDK.

Configuration update

You can update the default configuration after initializing the SDK, below are the additional configuration options you can customize.

import { ClickstreamAnalytics } from '@aws/clickstream-react-native';

ClickstreamAnalytics.updateConfigure({
  appId: 'your appId',
  endpoint: 'https://example.com/collect',
  isLogEvents: true,
  authCookie: 'your auth cookie',
  isCompressEvents: true,
  isTrackPageViewEvents: false,
  isTrackUserEngagementEvents: false,
  isTrackAppExceptionEvents: false,
});

Logging events json in debug mode

import { ClickstreamAnalytics } from '@aws/clickstream-react-native';

ClickstreamAnalytics.init({
  appId: 'your appId',
  endpoint: 'https://example.com/collect',
  isLogEvents: true,
});

After configuring isLogEvents:true when initializing the SDK, when you record an event, you can see the event raw json in AndroidStudio Logcat or Xcode debug console by filter EventRecorder.

Disable SDK

You can disable the SDK in the scenario you need. After disabling the SDK, the SDK will not handle the logging and sending of any events. Of course, you can enable the SDK when you need to continue logging events.

import { ClickstreamAnalytics } from '@aws/clickstream-react-native';

// disable SDK
ClickstreamAnalytics.disable();

// enable SDK
ClickstreamAnalytics.enable();

How to integrate and test locally

Clone this repository locally and execute the following script to generate aws-clickstream-react-native-0.3.3.tgz zip package, which will be located in the project root folder.

$ cd clickstream-react-native && yarn && yarn run pack

Copy the aws-clickstream-react-native-0.3.3.tgz into your project, then execute the script in your project root folder to install the SDK.

$ yarn add ./aws-clickstream-react-native-0.3.3.tgz

Note: Please correct the SDK version and change the path to where the aws-clickstream-react-native-0.3.3.tgz file is located.

You can also find the aws-clickstream-react-native-0.3.3.tgz file in the Release page.

Test

$ yarn run test

# with lint
$ sh ./deployment/run-unit-tests.sh

Collection of operational metrics

This solution collects anonymized operational metrics to help AWS improve the quality of features of the solution. For more information, including how to disable this capability, please see the implementation guide.

Security

See CONTRIBUTING for more information.

License

This library is licensed under the Apache 2.0 License.

Although this repository is released under the Apache 2.0 License, it includes some third party dependencies with BlueOak-1.0.0 license.

版本与发布记录

@aws/clickstream-react-native 共发布 8 个版本,首次发布于 2024/04/03;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.3.3latest2025/04/161.6 年前
0.3.22025/02/281.7 年前
0.3.12024/07/252 年前
0.3.02024/06/282 年前
0.2.12024/06/142 年前
0.2.02024/04/122 年前

关于 @aws/clickstream-react-native 的常见问题

@aws/clickstream-react-native 是什么?

Clickstream Analytics React Native SDK

如何安装 @aws/clickstream-react-native?

在项目目录运行 npm install @aws/clickstream-react-native,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react-native(*)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@aws/clickstream-react-native 支持 TypeScript 吗?

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

@aws/clickstream-react-native 支持 ESM 还是 CommonJS?

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

@aws/clickstream-react-native 可以商用吗?

@aws/clickstream-react-native 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@aws/clickstream-react-native 的下载量有多少?

根据 npm 官方下载统计,@aws/clickstream-react-native 在 2026/10/08 – 2026/11/06 这 30 天内被下载 2,355 次,日均约 79 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@aws/clickstream-react-native 还在维护吗?

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