[Huawei Developers] Mike-mei:
Conventional pop-up ads and roll ads in apps not only frustrate users, but are a headache for advertisers. This is because on the one hand, advertising is expensive, but on the other hand, these ads do not necessarily reach their target audience. The emergence of personalized ads has proved a game changer.

To ensure ads are actually sent to their intended audience, publishers usually need to collect the personal data of users to determine their characteristics, hobbies, recent requirements, and more, and then push targeted ads in apps. Some users are unwilling to share privacy data to receive personalized ads. Therefore, if an app needs to collect, use, and share users' personal data for the purpose of personalized ads, valid consent from users must be obtained first.

HUAWEI Ads provides the capability of obtaining user consent. In countries/regions with strict privacy requirements, it is recommended that publishers access the personalized ad service through the HUAWEI Ads SDK and share personal data that has been collected and processed with HUAWEI Ads. HUAWEI Ads reserves the right to monitor the privacy and data compliance of publishers. By default, personalized ads are returned for ad requests to HUAWEI Ads, and the ads are filtered based on the user's previously collected data. HUAWEI Ads also supports ad request settings for non-personalized ads. For details, please refer to "Personalized Ads and Non-personalized Ads" in the HUAWEI Ads Privacy and Data Security Policies.

To obtain user consent, you can use the Consent SDK provided by HUAWEI Ads or the CMP that complies with IAB TCF v2.0. For details, see Integration with IAB TCF v2.0.

Let's see how the Consent SDK can be used to request user consent and how to request ads accordingly.

Development Procedure​

To begin with, you will need to integrate the HMS Core SDK and HUAWEI Ads SDK. For details, see the development guide.

Using the Consent SDK​

1. Integrate the Consent SDK.

a. Configure the Maven repository address.

The code library configuration of Android Studio is different in versions earlier than Gradle 7.0, Gradle 7.0, and Gradle 7.1 and later versions. Select the corresponding configuration procedure based on your Gradle plugin version.

b. Add build dependencies to the app-level build.gradle file.

View attachment 5754499

Replace {version} with the actual version number. For details about the version number, please refer to the version updates. The sample code is as follows:

dependencies {
implementation 'com.huawei.hms:ads-consent:3.4.54.300'
}

After completing all the preceding configurations, click View attachment 5754503 on the toolbar to synchronize the build.gradle file and download the dependencies.

View attachment 5754505

2. Update the user consent status.

When using the Consent SDK, ensure that the Consent SDK obtains the latest information about the ad technology providers of HUAWEI Ads. If the list of ad technology providers changes after the user consent is obtained, the Consent SDK will automatically set the user consent status to UNKNOWN. This means that every time the app is launched, you should call the requestConsentUpdate() method to determine the user consent status. The sample code is as follows:

...
import com.huawei.hms.ads.consent.*;
...
public class ConsentActivity extends BaseActivity {
...
@Override
protected void onCreate(Bundle savedInstanceState) {
...
// Check the user consent status.
checkConsentStatus();
...
}
...
private void checkConsentStatus() {
...
Consent consentInfo = Consent.getInstance(this);
...
consentInfo.requestConsentUpdate(new ConsentUpdateListener() {
@Override
public void onSuccess(ConsentStatus consentStatus, boolean isNeedConsent, List<AdProvider> adProviders) {
// User consent status successfully updated.
...
}
@Override
public void onFail(String errorDescription) {
// Failed to update user consent status.
...
}
});
...
}
...
}

If the user consent status is successfully updated, the onSuccess() method of ConsentUpdateListener provides the updated ConsentStatus (specifies the consent status), isNeedConsent (specifies whether consent is required), and adProviders (specifies the list of ad technology providers).

3. Obtain user consent.

You need to obtain the consent (for example, in a dialog box) of a user and display a complete list of ad technology providers. The following example shows how to obtain user consent in a dialog box:

a. Collect consent in a dialog box.

The sample code is as follows:

...
import com.huawei.hms.ads.consent.*;
...
public class ConsentActivity extends BaseActivity {
...
@Override
protected void onCreate(Bundle savedInstanceState) {
...
// Check the user consent status.
checkConsentStatus();
...
}
...
private void checkConsentStatus() {
...
Consent consentInfo = Consent.getInstance(this);
...
consentInfo.requestConsentUpdate(new ConsentUpdateListener() {
@Override
public void onSuccess(ConsentStatus consentStatus, boolean isNeedConsent, List<AdProvider> adProviders) {
...
// The parameter indicating whether the consent is required is returned.
if (isNeedConsent) {
// If ConsentStatus is set to UNKNOWN, ask for user consent again.
if (consentStatus == ConsentStatus.UNKNOWN) {
...
showConsentDialog();
}
// If ConsentStatus is set to PERSONALIZED or NON_PERSONALIZED, no dialog box is displayed to ask for user consent.
else {
...
}
} else {
...
}
}
@Override
public void onFail(String errorDescription) {
...
}
});
...
}
...
private void showConsentDialog() {
// Start to process the consent dialog box.
ConsentDialog dialog = new ConsentDialog(this, mAdProviders);
dialog.setCallback(this);
dialog.setCanceledOnTouchOutside(false);
dialog.show();
}
}

Sample dialog box

View attachment 5754507

Note: This image is for reference only. Design the UI based on the privacy page.

More information will be displayed if users tap here.

View attachment 5754509

Note: This image is for reference only. Design the UI based on the privacy page.

b. Display the list of ad technology providers.

Display the names of ad technology providers to the user and allow the user to access the privacy policies of the ad technology providers.

After a user taps here on the information screen, the list of ad technology providers should appear in a dialog box, as shown in the following figure.

View attachment 5754511

Note: This image is for reference only. Design the UI based on the privacy page.

c. Set consent status.

After obtaining the user's consent, use the setConsentStatus() method to set their content status. The sample code is as follows:

Consent.getInstance(getApplicationContext()).setConsentStatus(ConsentStatus.PERSONALIZED);

d. Set the tag indicating whether a user is under the age of consent.

If you want to request ads for users under the age of consent, call setUnderAgeOfPromise to set the tag for such users before calling requestConsentUpdate().

// Set the tag indicating whether a user is under the age of consent.
Consent.getInstance(getApplicationContext()).setUnderAgeOfPromise(true);

If setUnderAgeOfPromise is set to true, the onFail (String errorDescription) method is called back each time requestConsentUpdate() is called, and the errorDescription parameter is provided. In this case, do not display the dialog box for obtaining consent. The value false indicates that a user has reached the age of consent.

4. Load ads according to user consent.

By default, the setNonPersonalizedAd method is not called for requesting ads. In this case, personalized and non-personalized ads are requested, so if a user has not selected a consent option, only non-personalized ads can be requested.

The parameter of the setNonPersonalizedAd method can be set to the following values:

ALLOW_ALL: personalized and non-personalized ads.

ALLOW_NON_PERSONALIZED: non-personalized ads.

The sample code is as follows:

// Set the parameter in setNonPersonalizedAd to ALLOW_NON_PERSONALIZED to request only non-personalized ads.
RequestOptions requestOptions = HwAds.getRequestOptions();
requestOptions = requestOptions.toBuilder().setNonPersonalizedAd(ALLOW_NON_PERSONALIZED).build();
HwAds.setRequestOptions(requestOptions);
AdParam adParam = new AdParam.Builder().build();
adView.loadAd(adParam);

Testing the Consent SDK​

To simplify app testing, the Consent SDK provides debug options that you can set.

1. Call getTestDeviceId() to obtain the ID of your device.

The sample code is as follows:

String testDeviceId = Consent.getInstance(getApplicationContext()).getTestDeviceId();

2. Use the obtained device ID to add your device as a test device to the trustlist.

The sample code is as follows:

Consent.getInstance(getApplicationContext()).addTestDeviceId(testDeviceId);

3. Call setDebugNeedConsent to set whether consent is required.

The sample code is as follows:

// Require consent for debugging. In this case, the value of isNeedConsent returned by the ConsentUpdateListener method is true.
Consent.getInstance(getApplicationContext()).setDebugNeedConsent(DebugNeedConsent.DEBUG_NEED_CONSENT);
// Not to require consent for debugging. In this case, the value of isNeedConsent returned by the ConsentUpdateListener method is false.
Consent.getInstance(getApplicationContext()).setDebugNeedConsent(DebugNeedConsent.DEBUG_NOT_NEED_CONSENT);

After these steps are complete, the value of isNeedConsent will be returned based on your debug status when calls are made to update the consent status.

For more information about the Consent SDK, please refer to the sample code.

References​

Ads Kit

Development Guide of Ads Kit