Skip to content

SDK Initialization

This document describes Android SDK initialization.

Application Configuration

The best place to initialize the SDK is in the onCreate method of Application. If your application has not yet created an Application, create one and declare it in AndroidManifest.xml. For an example, see here.

<application
       android:name="YourApplication">
</application>

Basic Configuration

public class DemoApplication extends Application {

    @Override
    public void onCreate() {
        // Local environment deployment, DataKit deployment
        FTSDKConfig config = FTSDKConfig.builder(datakitUrl);
        // Use public DataWay
        FTSDKConfig config = FTSDKConfig.builder(datawayUrl, clientToken);
        // ...
        // config.setDebug(true);              // debug mode
        FTSdk.install(config);
    }
}
class DemoApplication : Application() {
    override fun onCreate() {
        // Local environment deployment, DataKit deployment
        val config = FTSDKConfig.builder(datakitUrl)
        // Use public DataWay
        val config = FTSDKConfig.builder(datawayUrl, clientToken)
        // ...
        // config.setDebug(true)              // debug mode
        FTSdk.install(config)
    }
}
Method Name Type Required Description
datakitUrl String Yes URL for reporting to local environment deployment (DataKit), for 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 either datakitUrl or datawayUrl, not both.
datawayUrl String Yes Public Dataway reporting URL, obtained from the [Real User Monitoring (RUM)] application, for example: https://open.dataway.url. The device on which the SDK is installed must be able to access this address. Note: Configure either datakitUrl or datawayUrl, not both.
clientToken String Yes Authentication token, which must be configured together with datawayUrl
setDebug Boolean No Whether to enable debug mode. Default: false. SDK runtime logs are printed only after this is enabled
setEnv EnvType No Set the collection environment. Default: EnvType.PROD
setEnv String No Set the collection environment. Default: prod. Note: Configure only one of the String or EnvType types.
setOnlySupportMainProcess Boolean No Whether to support running only in the main process. Default: true. To run in other processes, set this field to false
setAllowWebViewHost Array No Uniformly set the hosts allowed to use the Bridge for WebView RUM and Log. null means all hosts are allowed; an empty array means the Bridge is not used automatically. Configured hosts also match their subdomains. Default: null. Supported in ft-sdk 1.7.5 and later. For details, see WebView Monitoring
setEnableAccessAndroidID Boolean No Enable obtaining Android ID. Default: true. If set to false, data for the device_uuid field is not collected. For market privacy review, see here
addGlobalContext Dictionary No Add SDK global properties. For adding rules, see here
setServiceName String No Set the service name. This affects the service field data in Log and RUM. Default: df_rum_android
setAutoSync Boolean No Whether to automatically synchronize data to the server after collection. Default: true. When set to false, use FTSdk.flushSyncData() to manage data synchronization yourself
setSyncPageSize Int No Set the number of entries per synchronization request. SyncPageSize.MINI: 5 entries; SyncPageSize.MEDIUM: 10 entries; SyncPageSize.LARGE: 50 entries. Default: SyncPageSize.MEDIUM
setCustomSyncPageSize Enum No Set the number of entries per synchronization request. Range: [5,). Note that a larger number of request entries means data synchronization consumes more computing resources. Default: 10. Note: Configure only one of setSyncPageSize and setCustomSyncPageSize.
setSyncSleepTime Int No Set the synchronization interval. Range: [0,5000], in ms. Default: 0
enableDataIntegerCompatible Void No When data needs to coexist with web data, it is recommended to enable this. This configuration handles web data type storage compatibility issues. Enabled by default in ft-sdk 1.6.9
setNeedTransformOldCache Boolean No Whether to be compatible with synchronizing old cache data from ft-sdk versions earlier than 1.6.0. Default: false
enableFileDataStore Void No Enable file cache for synchronization cache and RUM aggregate data. By default, SQLite cache is still used. Supported in ft-sdk 1.7.2 and later
setUseFileDataStore Boolean No Set whether to use file cache. Pass true to use file cache; pass false to use the default SQLite cache. Supported in ft-sdk 1.7.2 and later
setFileDataStoreShadow Boolean No Enable file cache shadow writing. After enabled, reads still use SQLite, while writes are mirrored to the file cache for validation before migration. Supported in ft-sdk 1.7.2 and later
setCompressIntakeRequests Boolean No Compress uploaded synchronization data using deflate. Enabled by default; set to false to disable. This method is supported in ft-sdk 1.6.3 and later
enableLimitWithCacheSize Void, Long No Enable the total cache size limit. Default: 100 MB, in bytes. When cacheSize is passed, the value range is [30 MB,). After enabled, FTLoggerConfig.setLogCacheLimitCount and FTRUMConfig.setRumCacheLimitCount no longer take effect. Supported in ft-sdk 1.7.2 and later
setCacheDiscard CacheDiscard No Set the discard policy after the cache reaches the size limit. Default: CacheDiscard.DISCARD. DISCARD discards additional data; DISCARD_OLDEST deletes the earliest cached data. Supported in ft-sdk 1.7.2 and later
enableLimitWithDbSize Void No Deprecated, retained for compatibility with earlier versions. Use enableLimitWithCacheSize instead
setEnableOkhttpRequestTag Boolean No Automatically add a unique ResourceID to OkHttp Requests for scenarios with high concurrency of identical requests. Supported in ft-sdk 1.6.10 and later and ft-plugin 1.3.5 and later
setProxy java.net.Proxy No Set a Proxy for data network synchronization requests. Only okhttp3 is supported. Supported in ft-sdk 1.6.10 and later
setProxyAuthenticator okhttp3.Authenticator No Set Proxy authentication for data synchronization network requests. Only okhttp3 is supported. Supported in ft-sdk 1.6.10 and later
setDns okhttp3.Dns No Custom DNS for data synchronization network requests, enabling custom processing of domain name resolution. Only okhttp3 is supported. Supported in ft-sdk 1.6.10 and later
setDataModifier DataModifier No Modify individual fields. Supported in ft-sdk 1.6.11 and later. For a usage example, see here
setLineDataModifier LineDataModifier No Modify individual data entries. Supported in ft-sdk 1.6.11 and later. For a usage example, see here
setEnableDataFilter Boolean No Whether to enable blacklist filtering. Default: true. Supports filtering Logging and RUM data. Supported in ft-sdk 1.7.2 and later
setDataFilters HashMap<String, String[]> No Configure in-app blacklist rules. Supports logging and rum data. For rule syntax, see Blacklist Filtering Rules. Supported in ft-sdk 1.7.2 and later
setRemoteConfiguration Boolean No Whether to enable remote configuration. Default: false. Must be set before SDK initialization, and a valid reporting address and RUM App ID must be configured. When RUM is initialized or the app returns to the foreground, updates are attempted at the minimum interval. Supported in ft-sdk 1.6.12 and later, with DataKit >= 1.60 or public Dataway. For details, see Remote Configuration
setRemoteConfigMiniUpdateInterval Int No Minimum remote configuration update interval, in seconds. Default: 12 hours. This value limits request frequency and is not a scheduled refresh period. Supported in ft-sdk 1.6.12 and later
setRemoteConfigurationCallBack FTRemoteConfigManager.FetchResult No Register a global callback before SDK initialization to receive update results and customize adjustments before the configuration is applied. For active updates, you can pass a callback for this update to override the global callback. See Initialization and Active Update Examples. Supported in ft-sdk 1.6.16 and later

File Cache

ft-sdk 1.7.2 and later supports writing synchronization cache and RUM aggregate data to file cache. To ensure smooth upgrade from earlier versions, the SDK still uses SQLite cache by default. If you want to enable file cache, explicitly enable it in FTSDKConfig.

FTSDKConfig config = FTSDKConfig.builder(datawayUrl, clientToken)
        .enableFileDataStore();

FTSdk.install(config);
val config = FTSDKConfig.builder(datawayUrl, clientToken)
    .enableFileDataStore()

FTSdk.install(config)

If you need to verify file cache writes first, you can enable shadow writing. After enabled, the SDK still reads data from SQLite while synchronously mirroring writes to the file cache; after validation passes, switch to enableFileDataStore.

FTSDKConfig config = FTSDKConfig.builder(datawayUrl, clientToken)
        .setFileDataStoreShadow(true);
val config = FTSDKConfig.builder(datawayUrl, clientToken)
    .setFileDataStoreShadow(true)

Cache Size Limit

In ft-sdk 1.7.2 and later, it is recommended to use enableLimitWithCacheSize to configure the SDK total cache size limit. After enabled, the separate log entry limit FTLoggerConfig.setLogCacheLimitCount and RUM entry limit FTRUMConfig.setRumCacheLimitCount no longer take effect.

FTSDKConfig config = FTSDKConfig.builder(datawayUrl, clientToken)
        // Enable the total cache size limit; this example uses 100 MB
        .enableLimitWithCacheSize(100 * 1024 * 1024L)
        // Delete the earliest cached data after the cache reaches the limit
        .setCacheDiscard(CacheDiscard.DISCARD_OLDEST);

FTSdk.install(config);
val config = FTSDKConfig.builder(datawayUrl, clientToken)
    // Enable the total cache size limit; this example uses 100 MB
    .enableLimitWithCacheSize(100 * 1024 * 1024L)
    // Delete the earliest cached data after the cache reaches the limit
    .setCacheDiscard(CacheDiscard.DISCARD_OLDEST)

FTSdk.install(config)

Blacklist Filtering

ft-sdk 1.7.2 and later supports filtering Logging and RUM data before data is written to local cache. This capability is enabled by default and can be disabled with setEnableDataFilter(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-app via setDataFilters. Both methods provide the same blacklist filtering capability and can be used together. If any rule matches, that data entry is discarded.

setDataFilters supports two types of rules: logging and rum. Each rule is expressed as { condition } and can match data tags, fields, and data type identification fields such as source, measurement, and class. For the complete syntax, see Blacklist Filtering Rules.

Blacklist filtering is executed after LineDataModifier and before local cache writes. If both setLineDataModifier and blacklist filtering are configured, filtering rules are evaluated based on the modified data.

HashMap<String, String[]> filters = new HashMap<>();
filters.put("logging", new String[]{
        "{ source in ['custom_log'] and message match ['password'] }"
});
filters.put("rum", new String[]{
        "{ source in ['resource'] and status in [404, 503] }"
});

FTSDKConfig config = FTSDKConfig.builder(datawayUrl, clientToken)
        .setEnableDataFilter(true)
        .setDataFilters(filters);

FTSdk.install(config);
val filters = hashMapOf(
    "logging" to arrayOf(
        "{ source in ['custom_log'] and message match ['password'] }"
    ),
    "rum" to arrayOf(
        "{ source in ['resource'] and status in [404, 503] }"
    )
)

val config = FTSDKConfig.builder(datawayUrl, clientToken)
    .setEnableDataFilter(true)
    .setDataFilters(filters)

FTSdk.install(config)

To disable local and remote data filtering, explicitly set:

FTSDKConfig.builder(datawayUrl, clientToken)
        .setEnableDataFilter(false);

The pull interval for workspace blacklist rules is 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.

User Information Binding and Unbinding

Use FTSdk to bind and unbind users.

Usage

/**
 * Bind user information
 *
 * @param id User ID
 */
public static void bindRumUserData(@NonNull String id)

/**
 * Bind user information
 */
public static void bindRumUserData(@NonNull UserData data)

/**
 * Unbind user information
 */
public static void unbindRumUserData()
/**
 * Bind user information
 *
 * @param id User ID
 */
fun bindRumUserData(id: String)

/**
 * Bind user information
 *
 * @param data User information
 */
fun bindRumUserData(data: UserData)

/**
 * Unbind user information
 */
fun unbindRumUserData()

UserData

Method Name Description Required Note
setId Set user ID No
setName Set user name No
setEmail Set email No
setExts Set user extensions No For adding rules, see App Access

Code Example

// Call this method to bind user information after the user logs in successfully
FTSdk.bindRumUserData("001");

UserData userData = new UserData();
userData.setName("test.user");
userData.setId("test.id");
userData.setEmail("test@mail.com");
Map<String, String> extMap = new HashMap<>();
extMap.put("ft_key", "ft_value");
userData.setExts(extMap);
FTSdk.bindRumUserData(userData);

// Call this method to unbind user information after the user logs out
FTSdk.unbindRumUserData();
// Call this method to bind user information after the user logs in successfully
FTSdk.bindRumUserData("001")

// Bind more user data
val userData = UserData()
userData.name = "test.user"
userData.id = "test.id"
userData.email = "test@mail.com"
val extMap = HashMap<String, String>()
extMap["ft_key"] = "ft_value"
userData.setExts(extMap)
FTSdk.bindRumUserData(userData)

// Call this method to unbind user information after the user logs out
FTSdk.unbindRumUserData()

Runtime Capabilities

Shut Down the SDK

If you dynamically change SDK configurations, shut down the SDK first to avoid generating incorrect data.

FTSdk.shutDown();
FTSdk.shutDown()

Clear SDK Cache Data

Use FTSdk to clear cached data that has not been reported.

FTSdk.clearAllData();
FTSdk.clearAllData()

Set Automatic Data Synchronization

ft-sdk 1.7.3 and later supports dynamically enabling or disabling automatic synchronization of cached data after SDK initialization. After it is disabled, the SDK still writes collected data to the local cache but does not automatically trigger synchronization after data collection. You can use FTSdk.flushSyncData() to manage data synchronization yourself.

// Disable automatic synchronization
FTSdk.setAutoSync(false);

// Enable automatic synchronization
FTSdk.setAutoSync(true);
// Disable automatic synchronization
FTSdk.setAutoSync(false)

// Enable automatic synchronization
FTSdk.setAutoSync(true)

Actively Synchronize Data

Use FTSdk to actively synchronize data.

You need to synchronize data yourself only when FTSdk.setAutoSync(false) is set.

FTSdk.flushSyncData();
FTSdk.flushSyncData()

Feedback

Is this page helpful?