An Open Anonymous Device Identifier (OAID) is a non-permanent device identifier. You can use OAIDs to provide personalized ads for users while protecting their privacy. In addition, third-party tracking platforms can provide conversion attribution for advertisers based on OAIDs.
Whether you are developing an app, an ad platform, or a third-party tracking platform, you can obtain OAIDs from devices. The OAIDs can be used to recommend personalized ads to users and attribute ad conversions.
An OAID is a 32-bit UUID-like identifier generated using a proprietary Huawei algorithm. The format is xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx. UUID is the short form of Universally Unique Identifier.
To be specific, an OAID has the following features:
As a device-level identifier, OAIDs obtained by different apps on the same device are the same.
Whether an app can obtain a valid OAID depends on the status of the Activity tracking for this app switch: When the switch is enabled, the app can obtain a valid OAID that is not composed of only zeros. When the switch is disabled, the app can obtain an OAID that contains only zeros.
An OAID is generated when the Activity tracking for this app switch is enabled for the first time by any app on the device.
On HarmonyOS NEXT Developer Beta5 and earlier versions, the Activity tracking for this app permission was named App tracking access.
The OAID changes in the following scenarios:
A user restores the device to factory settings.
A user resets the OAID.
The OAID service supports phones, tablets, and PCs/2-in-1 devices. Starting from API version 20, TVs are also supported.
For a PC/2-in-1 device, ensure that the Intelligent Promotion Service version on the device is 8.4.80.300 or later. You can check the version by going to Settings > Apps & services > More apps.
Before obtaining the OAID, ensure that you have fully informed users of related information and obtained user authorization. The privacy agreement pop-up must be displayed to obtain users' explicit consent before the OAID is obtained. Obtaining the OAID without user consent violates data collection regulations and may result in privacy compliance risks.
| API | Description |
|---|---|
| getOAID(): Promise<string> | Obtains an OAID. This API uses a promise to return the result. |
| getOAID(callback: AsyncCallback<string>): void | Obtains an OAID. This API uses an asynchronous callback to return the result. |
To call getOAID, request the ohos.permission.APP_TRACKING_CONSENT permission and keep Require consent to track disabled. There are three situations:
If the app has configured the ohos.permission.APP_TRACKING_CONSENT permission and Activity tracking for this app is set to Allow, the OAID is returned.
If the app has configured the ohos.permission.APP_TRACKING_CONSENT permission and Activity tracking for this app is set to Deny, 00000000-0000-0000-0000-000000000000 is returned.
If the app has not configured the ohos.permission.APP_TRACKING_CONSENT permission, 00000000-0000-0000-0000-000000000000 is returned.
In the module.json5 file of the module, request the activity tracking permission ohos.permission.APP_TRACKING_CONSENT, which is a user_grant permission. For a user_grant permission, reason and abilities are mandatory. For details about the configuration method, please refer to the requestPermissions description. The sample code is as follows:
{
"module": {
"requestPermissions": [
{
"name": "ohos.permission.APP_TRACKING_CONSENT",
"reason": "$string:reason",
"usedScene": {
"abilities": [
"EntryFormAbility"
],
"when": "inuse"
}
}
]
}
} To obtain the OAID, an app needs to call requestPermissionsFromUser to obtain the permission.
For details about how to obtain the context, please refer to Acquisition of Context. The sample code is as follows:
import { abilityAccessCtrl, PermissionRequestResult } from '@kit.AbilityKit';
import { identifier } from '@kit.AdsKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
async function requestOAID(context: Context): Promise<string | undefined> {
// Request user permission for Activity tracking for this app.
let isPermissionGranted: boolean = false;
try {
const atManager: abilityAccessCtrl.AtManager = abilityAccessCtrl.createAtManager();
const result: PermissionRequestResult =
await atManager.requestPermissionsFromUser(context, ['ohos.permission.APP_TRACKING_CONSENT']);
isPermissionGranted = result.authResults[0] === abilityAccessCtrl.GrantStatus.PERMISSION_GRANTED;
} catch (err) {
hilog.error(0x0000, 'testTag', `Failed to request permission. Code is ${err.code}, message is ${err.message}`);
}
if (isPermissionGranted) {
hilog.info(0x0000, 'testTag', 'Succeeded in requesting permission');
try {
const oaid = await identifier.getOAID();
hilog.info(0x0000, 'testTag', 'Succeeded in getting OAID');
return oaid;
} catch (err) {
hilog.error(0x0000, 'testTag', `Failed to get OAID. Code is ${err.code}, message is ${err.message}`);
}
} else {
hilog.error(0x0000, 'testTag', 'Failed to request permission. User rejected');
}
return undefined;
}