SDK Initialization¶
This document describes React Native SDK initialization and runtime capabilities.
Import the SDK¶
You can now use the following in your code:
import {
FTMobileReactNative,
FTReactNativeLog,
FTReactNativeTrace,
FTReactNativeRUM,
FTMobileConfig,
FTLogConfig,
FTTraceConfig,
FTRUMConfig,
ErrorMonitorType,
DeviceMetricsMonitorType,
DetectFrequency,
TraceType,
FTLogStatus,
EnvType,
} from '@cloudcare/react-native-mobile';
Basic Configuration¶
// Local environment deployment, DataKit deployment
let config: FTMobileConfig = {
datakitUrl: datakitUrl,
};
// Use public DataWay
let config: FTMobileConfig = {
datawayUrl: datawayUrl,
clientToken: clientToken,
};
await FTMobileReactNative.sdkConfig(config);
| Field | Type | Required | Description |
|---|---|---|---|
| datakitUrl | string | Yes | Reporting URL for local environment deployment (DataKit). Example: http://10.0.0.1:9529. The default port is 9529. The device on which the SDK is installed must be able to access this address. Note: Configure only one of datakitUrl and datawayUrl. Starting with SDK 0.4.3, you can configure it later after initialization through setDatakitURL(). No data is uploaded before it is configured. For details, see Dynamically Update the Reporting Endpoint. |
| datawayUrl | string | Yes | Public DataWay reporting URL. Obtain it from the [RUM] app. Example: https://open.dataway.url. The device on which the SDK is installed must be able to access this address. Note: Configure only one of datakitUrl and datawayUrl. Starting with SDK 0.4.3, you can configure it later after initialization through setDatawayURL(). No data is uploaded before it is configured. For details, see Dynamically Update the Reporting Endpoint. |
| clientToken | string | Yes | Authentication token. It must be used together with datawayUrl. Starting with SDK 0.4.3, you can configure it later together with the upload endpoint through setDatawayURL(datawayUrl, clientToken). For details, see Dynamically Update the Reporting Endpoint. |
| debug | boolean | No | Set whether log printing is allowed. Default: false |
| env | string | No | Environment configuration. Default: prod. Any character is allowed. A single word is recommended, such as test |
| envType | enum EnvType | No | Environment configuration. Default: EnvType.prod. Note: Configure only one of env and envType |
| service | string | No | Set the name of the business or service to which it belongs. This affects service field data in Log and RUM. Default: df_rum_ios, df_rum_android |
| autoSync | boolean | No | Whether to automatically synchronize data to the server after collection. Default: true. When set to false, use FTMobileReactNative.flushSyncData() to manage data synchronization yourself |
| syncPageSize | number | No | Set the number of entries per synchronization request. Range: [5,). Note: The larger the number of request entries, the more computing resources data synchronization consumes |
| syncSleepTime | number | No | Set the synchronization interval. Range: [0,5000]. Not set by default |
| enableDataIntegerCompatible | boolean | No | Recommended when data needs to coexist with web data. This configuration handles storage compatibility issues with web data types. Enabled by default in versions after 0.3.12 |
| globalContext | object | No | Add custom tags. For addition rules, see here |
| compressIntakeRequests | boolean | No | Enable deflate compression for uploaded synchronization data. Disabled by default |
| enableLimitWithDbSize | boolean | No | Enable using the DB to limit data size. Default: 100 MB, in bytes. The larger the database, the greater the disk pressure. Disabled by default. Note: After this is enabled, the Log configuration logCacheLimitCount and the RUM configuration rumCacheLimitCount become invalid. This parameter is supported in SDK 0.3.10 and later |
| dbCacheLimit | number | No | DB cache limit. Range: [30MB,). Default: 100 MB. Unit: bytes. This parameter is supported in SDK 0.3.10 and later |
| dbDiscardStrategy | string | No | Set the data discard rule in the database. Discard strategies: FTDBCacheDiscard.discard discards new data (default); FTDBCacheDiscard.discardOldest discards old data. This parameter is supported in SDK 0.3.10 and later |
| dataModifier | object | No | Modify a single field. Supported in SDK 0.3.14 and later. For usage examples, see Data Masking During Collection |
| lineDataModifier | object | No | Modify a single data record. Supported in SDK 0.3.14 and later. For usage examples, see Data Masking During Collection |
| enableDataFilter | boolean | No | Whether to enable SDK-side blacklist filtering. Default: true. Supports filtering Log and RUM data. Supported in SDK 0.4.2 and later. For usage examples, see Blacklist Filtering |
| dataFilters | Record |
No | Configure in-app blacklist rules. Supports two data categories: logging and rum. Supported in SDK 0.4.2 and later. For rule syntax, see Rule Syntax |
| allowWebViewHost | Array |
No | Configure the host allowlist shared by WebView RUM and Logs. null allows all hosts, and [] blocks all hosts. If not set, the previous RUM configuration is used; otherwise, this configuration takes precedence over the previous configuration. Remote configuration takes precedence. For details, see WebView Host Configuration. Supported starting with SDK 0.4.3. |
| remoteConfiguration | boolean | No | Whether to enable the remote configuration feature for data collection. Disabled by default. After it is enabled, SDK initialization or app hot start triggers a data update. Supported in SDK 0.3.16 and later. Configurable Parameters |
| remoteConfigMiniUpdateInterval | number | No | Set the minimum update interval for remote dynamic configuration, in seconds. Default: 12 hours. Supported in SDK 0.3.16 and later |
| remoteConfigOverrideRules | Array |
No | Set remote configuration override rules to make custom adjustments before applying the remote configuration. Supported in SDK 0.3.16 and later. For usage examples, see here |
Starting with SDK 0.4.3, you can omit the upload endpoint during initialization:
await FTMobileReactNative.sdkConfig({});
// Continue initializing the required collection modules such as RUM and Log.
// Set it after obtaining the upload endpoint:
await FTMobileReactNative.setDatawayURL('https://open.dataway.url', 'client_token');
When no endpoint is configured, enabled collection modules can still collect and cache data but will not upload it. When setting a DataWay endpoint, you must also provide clientToken; when using DataKit, call setDatakitURL(). The cache is still subject to capacity limits. For details, see Dynamically Update the Reporting Endpoint.
Blacklist Filtering¶
React Native SDK 0.4.2 and later supports filtering RUM and Log data before data is written to the local cache. This capability is enabled by default. You can disable SDK-side filtering with enableDataFilter: false.
Blacklist rules can be configured centrally in the blacklist of the Guance Workspace and automatically pulled by the SDK from DataKit or DataWay; they can also be configured in the app through FTMobileConfig.dataFilters. Both methods use the same blacklist filtering capability and can be used together. If any rule matches, the corresponding data record is discarded.
Blacklist filtering runs after lineDataModifier and before data is written to the local cache. If both lineDataModifier and blacklist filtering are configured, filtering rules evaluate the modified data.
enableDataFilteronly controls rule pulling and filtering on the SDK side. After it is set tofalse, the SDK no longer appliesdataFiltersor pulls Workspace rules; when reporting through DataKit, the Workspace blacklist may still be executed on the DataKit side.Data Filter applies to the SDK data write path. If there are too many rules or the regular expressions are too complex, data write performance may be affected. Configure only necessary rules.
let config: FTMobileConfig = {
datawayUrl: datawayUrl,
clientToken: clientToken,
enableDataFilter: true,
dataFilters: {
logging: [
"{ `source` in [ 'df_rum_ios_log' , 'df_rum_android_log' ] and `message` match [ 'timeout' ] }",
],
rum: [
"{ `resource_status` match [ '5..' ] }",
],
},
};
await FTMobileReactNative.sdkConfig(config);
When the SDK is initialized, it immediately pulls Workspace blacklist rules. Subsequent pull intervals are based on the pull_interval returned by the server. If the server does not return a valid value, the SDK uses 10 seconds as the fallback interval. pull_interval supports seconds or strings with units, such as 10, 30s, 2m, and 1h.
Rule Syntax¶
The Data Filter rule syntax is basically the same as the blacklist filtering rule syntax. For the complete syntax, see Blacklist Filtering Rules.
The key in dataFilters indicates the data category. The SDK currently supports:
| Category | Description |
|---|---|
logging |
Log data |
rum |
RUM data |
Each rule is expressed as { condition }. If any rule matches, data in that category is filtered. Rules can use the data's tag and field fields, as well as the source and measurement data type identifier fields.
For field value formats and operator semantics, see Field Value Format Description and Operator Description in the blacklist filtering rules.
For field values in SDK rule strings, use the array format. Reverse operators support not in and not match, and are also compatible with notin, notmatch, and not_in used by server-delivered rules.
User Information Binding and Unbinding¶
Usage¶
/**
* Bind a user.
* @param userId User ID.
* @param userName User name.
* @param userEmail User email.
* @param extra Additional user information.
*/
bindRUMUserData(userId: string, userName?: string, userEmail?: string, extra?: object): Promise<void>;
/**
* Unbind the user.
*/
unbindRUMUserData(): Promise<void>;
Example¶
import { FTMobileReactNative } from '@cloudcare/react-native-mobile';
FTMobileReactNative.bindRUMUserData('react-native-user', 'user_name');
FTMobileReactNative.unbindRUMUserData();
Runtime Capabilities¶
Shut Down the SDK¶
Use FTMobileReactNative to shut down the SDK.
Clear SDK Cached Data¶
Use FTMobileReactNative to clear cached data that has not been reported.
/**
* Clear all data that has not yet been uploaded to the server.
*/
clearAllData(): Promise<void>;
Actively Synchronize Data¶
When FTMobileConfig.autoSync is set to true, no additional action is required; the SDK synchronizes automatically.
When FTMobileConfig.autoSync is set to false, you need to actively trigger data synchronization.
/**
* Actively synchronize data. This must be actively triggered when `FTMobileConfig.autoSync = false` is configured.
*/
flushSyncData(): Promise<void>;
Initialization Order¶
Complete SDK initialization in the top-level index.js file before registering the App to ensure that the SDK is fully ready before other SDK methods are called.
After completing the basic configuration, configure RUM, Log, and Trace.
import App from './App';
async function sdkInit() {
await FTMobileReactNative.sdkConfig(config);
await FTReactNativeRUM.setConfig(rumConfig);
// ...
}
sdkInit();
AppRegistry.registerComponent('main', () => App);
Dynamic Configuration¶
Dynamic configuration-related capabilities have been split out into Dynamic Configuration.