• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# OAID Service Component
2
3## Introduction
4
5The Open Anonymous Device Identifier (OAID) service facilitates personalized ad placement based on OAIDs, each of which is a non-permanent device identifier. The service provides personalized ads for users while protecting their personal data privacy. It can also interact with third-party tracking platforms to provide conversion attribution analysis for advertisers.
6
7## Directory Structure
8
9```
10/domains/cloud/oaid # Service code of the OAID service component
11├── interfaces                         # API code
12├── profile                            # Service configuration profile
13├── services                           # Service code
14├── test                               # Test cases
15├── LICENSE                            # License file
16└── bundle.json                        # Build file
17```
18
19## How to Use
20
21### Obtaining an OAID
22
23You can use the APIs provided in this repository to obtain OAIDs.
24
251. Request the ad tracking permission.
26
27   Configure the [ohos.permission.APP_TRACKING_CONSENT](https://docs.openharmony.cn/pages/v4.0/zh-cn/application-dev/security/permission-list.md/) permission in the **module.json5** file of the module.
28
29   ```javascript
30   {
31     "module": {
32       "requestPermissions": [
33         {
34           "name": "ohos.permission.APP_TRACKING_CONSENT" // Request the ad tracking permission.
35         }
36       ]
37     }
38   }
39   ```
40
412. Request authorization from the user by displaying a dialog box when the application is started.
42
43   ```javascript
44   import abilityAccessCtrl from '@ohos.abilityAccessCtrl';
45
46   private requestOAIDTrackingConsentPermissions(context: any): void {
47     // Display a dialog box when the page is displayed to request the user to grant the ad tracking permission.
48     const atManager = abilityAccessCtrl.createAtManager();
49     try {
50       atManager.requestPermissionsFromUser(context, ["ohos.permission.APP_TRACKING_CONSENT"]).then((data) => {
51         if (data.authResults[0] == 0) {
52           console.info(`request permission success`);
53         } else {
54           console.info(`user rejected`);
55         }
56       }).catch((err) => {
57         console.error(`request permission failed, error message: ${err.message}`);
58       })
59     } catch(err) {
60       console.error(`catch err->${JSON.stringify(err)}`);
61     }
62   }
63   ```
64
653. Obtain an OAID.
66
67- Obtain an OAID through the callback function.
68
69  ```javascript
70  import identifier from '@ohos.identifier.oaid';
71
72  private getOaidByCallback() {
73    try {
74      identifier.getOAID((err, data) => {
75        if (err.code) {
76          console.info(`getAdsIdentifierInfo failed, message: ${err.message}`);
77  	  } else {
78  		const oaid = data;
79  		console.error(`getOaidFromOaidSaAPi by callback success`);
80  	  }
81  	});
82    } catch (err) {
83      console.error(`catch err->${JSON.stringify(err)}`);
84    }
85  }
86  ```
87
88- Obtain an OAID through the promise.
89
90  ```javascript
91  import identifier from '@ohos.identifier.oaid';
92
93  private getOaidByPromise() {
94    try {
95      // Obtain an OAID.
96      identifier.getOAID().then((data) => {
97        const oaid = data;
98        console.info(`getAdsIdentifierInfo by promise success`);
99      }).catch((err) => {
100        console.error(`getAdsIdentifierInfo failed, message: ${err.message}`);
101      })
102    } catch (err) {
103      console.error(`catch err->${JSON.stringify(err)}`);
104    }
105  }
106  ```
107
108### Resetting the OAID
109
110You can use the API provided in this repository to reset OAIDs.  The API is a system API.
111
112```javascript
113import identifier from '@ohos.identifier.oaid';
114
115private resetOaid() {
116  try {
117    identifier.resetOAID();
118  } catch (err) {
119    console.error(`reset oaid catch error: ${err.code} ${err.message}`);
120  }
121}
122```
123
124## Repositories Involved
125