Skip to content

SDK Initialization

This document covers the initialization and runtime fundamentals of the UniApp SDK.

Basic Configuration

<script>
var ftModule = uni.requireNativePlugin("GCUniPlugin-MobileAgent");

export default {
    onLaunch: function() {
        ftModule.sdkConfig({
            datakitUrl: 'your datakitUrl',
            debug: true,
            env: 'common',
            globalContext: {
                custom_key: 'custom value'
            }
        });
    }
}
</script>
Parameter Type Required Description
datakitUrl string No Upload URL for a local deployment (Datakit), e.g., http://10.0.0.1:9529. Used together with datawayUrl (choose one); for SDK 0.2.7+, you can omit this during initialization and set it dynamically later via setDatakitURL.
datawayUrl string No Public DataWay upload URL. Used together with datakitUrl (choose one); for SDK 0.2.7+, you can omit this during initialization and set it dynamically later via setDatawayURL.
clientToken string Yes when using datawayUrl Authentication token. Must be used together with datawayUrl.
debug boolean No Whether to print debug logs. Default: false.
env string No Environment name. Default: prod. A single word is recommended, e.g., test.
service string No The service or business name. Default: df_rum_ios, df_rum_android.
globalContext object No Global tags attached at initialization.
offlinePackage boolean No Android only. Whether to use an offline package or uni mini program. Default: false. See App Access FAQ for details.
autoSync boolean No Whether to automatically sync data to the server after collection. Default: YES. When set to NO, use flushSyncData to manage syncing manually.
syncPageSize number No Number of items per sync request. Range: [5,). Default: 10.
syncSleepTime number No Sync interval. Range: [0,5000]. Default: not set.
enableDataIntegerCompatible boolean No Recommended to enable when coexisting with Web data. Enabled by default since SDK 0.2.1.
compressIntakeRequests boolean No Whether to apply deflate compression to synced data. Default: disabled. Supported since SDK 0.2.0.
enableLimitWithDbSize boolean No Whether to enable DB size limit. Default: disabled. When enabled, logCacheLimitCount and rumCacheLimitCount become ineffective.
dbCacheLimit number No DB cache size limit. Range: [30MB,). Default: 100MB. Unit: bytes.
dbDiscardStrategy string No DB data discard strategy: discard (discard new data, default), discardOldest (discard old data).
dataModifier object No Field-level data masking. See Data Collection Masking.
lineDataModifier object No Record-level data masking. See Data Collection Masking.
remoteConfiguration boolean No Whether to enable remote configuration for data collection. Default: false. When enabled, the SDK will check for configuration updates on initialization or app warm start. Requires Datakit >= 1.60 or a public DataWay. Supported since SDK 0.2.7.
remoteConfigMiniUpdateInterval number No Minimum interval for remote configuration updates. Range: [0,). Unit: seconds. Default: 12 hours. Supported since SDK 0.2.7.
enableDataFilter boolean No Whether to enable data filtering compatible with DataKit. Default: true. Both local and remote rules are controlled by this switch. Supported since SDK 0.2.7.
dataFilters object No Local data filtering rules. Keys support logging and rum. Values are arrays of rule strings. Supported since SDK 0.2.7.

Remote Configuration and Data Filtering

var ftModule = uni.requireNativePlugin("GCUniPlugin-MobileAgent");

ftModule.sdkConfig({
    datakitUrl: 'http://10.0.0.1:9529',
    remoteConfiguration: true,
    remoteConfigMiniUpdateInterval: 600,
    enableDataFilter: true,
    dataFilters: {
        logging: [
            "{ message match [ 'password' ] }"
        ],
        rum: [
            "{ resource_status match [ '5..' ] }"
        ]
    }
});
  • remoteConfiguration enables remote updates of SDK configurations such as sampling rate. To trigger an update manually, use updateRemoteConfigWithMiniUpdateInterval.
  • dataFilters are local blacklist rules shipped with the app. Data matching any rule is discarded before being written to the local cache.
  • Local and remote rules are applied together. Rules are evaluated after lineDataModifier, so the filtering is based on the modified data.
  • Each rule is expressed as { condition }. For supported fields and value formats, see Blacklist Filtering Rules. Too many rules or overly complex regular expressions may affect data write performance.

Binding and Unbinding User Information

var ftModule = uni.requireNativePlugin("GCUniPlugin-MobileAgent");

ftModule.bindRUMUserData({
    userId: 'Test userId',
    userName: 'Test name',
    userEmail: 'test@123.com',
    extra: {
        age: '20'
    }
});

ftModule.unbindRUMUserData();

API - bindRUMUserData

Field Type Required Description
userId string Yes User ID
userName string No User name
userEmail string No User email
extra object No Additional user information

API - unbindRUMUserData

Unbinds the current user.

Runtime Capabilities

Dynamically Update the Upload URL

SDK 0.2.7+ supports dynamically setting the upload URL after initialization. Use setDatakitURL or setDatawayURL (choose one). When switching to DataWay, you must provide clientToken as well.

You can omit both datakitUrl and datawayUrl during initialization. The SDK will still initialize and cache collected data. Once a valid upload URL is set, the SDK will start uploading to that address.

var ftModule = uni.requireNativePlugin("GCUniPlugin-MobileAgent");

ftModule.setDatakitURL({
    datakitUrl: 'http://10.0.0.1:9529'
});

// When using DataWay, call the following method instead of setDatakitURL.
ftModule.setDatawayURL({
    datawayUrl: 'https://open.dataway.url',
    clientToken: 'client-token'
});

API - setDatakitURL

Field Type Required Description
datakitUrl string Yes New Datakit upload URL

API - setDatawayURL

Field Type Required Description
datawayUrl string Yes New DataWay upload URL
clientToken string Yes Authentication token matching the DataWay URL

Manually Update Remote Configuration

Before calling this method, you must set remoteConfiguration: true in sdkConfig. The miniUpdateInterval parameter in this call specifies the minimum update interval for this invocation; the initialization-time remoteConfigMiniUpdateInterval is not used.

var ftModule = uni.requireNativePlugin("GCUniPlugin-MobileAgent");

ftModule.updateRemoteConfigWithMiniUpdateInterval({
    miniUpdateInterval: 0
}, result => {
    if (result.success) {
        console.log('remote config: ' + result.rawJson);
    } else {
        console.log('remote config failed: ' + result.errorMessage);
    }
});

API - updateRemoteConfigWithMiniUpdateInterval

Request Field Type Required Description
miniUpdateInterval number No Minimum interval for this manual update. Range: [0,). Unit: seconds. Default: 0.

Callback parameters:

Return Field Type Description
success boolean Whether the update was successful.
platform string Current platform: ios or android.
rawJson string Raw remote configuration JSON string returned on success; may be absent if the server has no content.
errorCode number/string Error code on failure. iOS returns a number, Android returns a string.
errorMessage string Error message on failure.

Shut Down the SDK

var ftModule = uni.requireNativePlugin("GCUniPlugin-MobileAgent");
ftModule.shutDown();

API - shutDown

Shuts down the SDK.

Clear SDK Cached Data

var ftModule = uni.requireNativePlugin("GCUniPlugin-MobileAgent");
ftModule.clearAllData();

API - clearAllData

Clears all data that has not yet been uploaded to the server.

Manually Sync Data

var ftModule = uni.requireNativePlugin("GCUniPlugin-MobileAgent");
ftModule.flushSyncData();

API - flushSyncData

When sdkConfig.autoSync is set to true, no additional action is required; the SDK syncs automatically.

When sdkConfig.autoSync is set to false, you must call this method manually to trigger data synchronization.

Feedback

Is this page helpful?