目录

OAID Service

Obtaining the OAID

[h2]Use Cases

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:

The OAID changes in the following scenarios:

[h2]Constraints

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.

[h2]API Description

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:

  1. 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.

  2. 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.

  3. If the app has not configured the ohos.permission.APP_TRACKING_CONSENT permission, 00000000-0000-0000-0000-000000000000 is returned.

[h2]Development Procedure

  1. 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"
            }
          }
        ]
      }
    }
  2. 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;
}