HarmonyOS
Latest version: v1.0.0
Update time: 2026-07-24
Downloads: Download
1. Overview
Starting from version 6.1, AE adds the experiment suite feature. You can create experiments in the AE backend, set the variable values for the control group and experiment groups, and pull them to your app through the client SDK to show different content to users in different groups.
This article describes how to integrate the HarmonyOS client SDK. On the app side, you only need to handle interactions with the ThinkingData SDK and don't need to care about task details in the AE backend.
Note that the experiment SDK must be used together with the Config Center SDK.
2. Integration
2.1 Integrate the SDK automatically
Run the following command in the application module directory:
ohpm install @thinkingdata/experiment
Or use the short form:
ohpm i @thinkingdata/experiment
ohpm automatically installs the transitive dependencies (@thinkingdata/remoteconfig, @thinkingdata/analytics, and so on).
You can also manually add the dependency to the oh-package.json5 file of the application module and then run ohpm install:
{
"dependencies": {
"@thinkingdata/experiment": "1.0.0"
}
}
2.2 Integrate the SDK manually
Config Center depends on the following ThinkingData SDKs:
| SDK name | Description | Version requirement |
|---|---|---|
| TDAnalytics | Collects and processes data | 1.9.0 |
| TDRemoteConfig | Fetches configuration from the AE backend | 1.0.0 |
- Click the download link at the top of this page to download the SDK and unzip it, and then put
TDExperiment.har,TDAnalytics.har, andTDRemoteConfig.harin thelibs/directory of the application module - Add the local HAR dependencies to the
oh-package.json5file of the application module:
{
"dependencies": {
"@thinkingdata/experiment": "file:./libs/TDExperiment.har",
"@thinkingdata/analytics": "file:./libs/TDAnalytics.har",
"@thinkingdata/remoteconfig": "file:./libs/TDRemoteConfig.har"
}
}
- Run
ohpm installto install the dependencies
Your code must directly
import { TDAnalytics }and initialize it. If the IDE or the build can't resolve the analytics module, also explicitly declare"@thinkingdata/analytics": "1.9.0"inoh-package.json5.
3. Initialization
The experiment SDK must be used together with the analytics SDK. Initialize TDAnalytics first, and then initialize the experiment SDK.
import { common } from '@kit.AbilityKit';
import { TDAnalytics } from '@thinkingdata/analytics';
import { TDExperiment, TDExperimentConfig } from '@thinkingdata/experiment';
const context = ... as common.UIAbilityContext;
const APP_ID = 'your_app_id';
const SERVER_URL = 'https://your-receiver-url';
// Initialize the data collection SDK
TDAnalytics.init(context, APP_ID, SERVER_URL);
TDAnalytics.login('user_distinct_id');
// Initialize the experiment SDK
const config = new TDExperimentConfig(APP_ID, SERVER_URL);
config.automaticExposureTracking = true;
config.customBucketId = { 'user_id': 'user_distinct_id' };
config.customFetchParams = { 'platform': 'harmonyos' };
config.initCallback = {
onFetchSuccess: () => {
// Experiment configuration fetched successfully
},
onError: (code: number, message: string) => {
// Failed to fetch the experiment configuration
}
};
TDExperiment.enableLog(true);
TDExperiment.init(context, config);
Initialization parameters:
- appId:APP ID
- serverUrl: the data receiving URL
- automaticExposureTracking: defaults to true. Exposure events are uploaded automatically when you call get to retrieve experiment parameters
- customBucketId: the custom bucket key
- customFetchParams: custom parameters on the client config channel
- enableLog: whether to enable debug logs (set through TDExperiment.enableLog)
- initCallback.onFetchSuccess: the experiment configuration is fetched successfully
- initCallback.onError: the experiment configuration fails to be fetched
4. Usage
4.1 Get experiment information
TDExperiment.getValueAsString(key, defaultValue);
TDExperiment.getValueAsNumber(key, defaultValue);
TDExperiment.getValueAsBoolean(key, defaultValue);
TDExperiment.getValueAsJson(key, defaultValue);
If automaticExposureTracking is true, exposure events are uploaded automatically when you call get.
4.2 Upload exposure events
You can call the exposure method to upload exposure events manually.
TDExperiment.exposure(key);
4.3 Proactively fetch all experiment information
TDExperiment.fetch();
TDExperiment.fetch().addOnSuccessListener({
onSuccess: () => {
// Success
}
}).addOnFailureListener({
onFailure: (code: number, msg: string) => {
// Failure
}
});
4.4 Custom bucket key
TDExperiment.setCustomBucketId({ 'bucket_id': 'account_id' });
4.5 Custom request parameters
TDExperiment.setCustomFetchParams({ 'key': 'value' });

