How to Request Ads Using Location Data
Tags: java, tutorial, programming, hmscore
Published: 2022-08-09 03:38:11

## Background

Have you ever had the following experience: When you are walking on a road and searching for car information in a social networking app, an ad pops up to you suddenly, telling you about discounts at a nearby car dealership. Given the advantages of the short distance, demand matching, and discount, you are more likely to go to the place for some car details, and this ad succeeds to attract you to the activity.

Nowadays, advertising has been one of the most effective ways for app developers to monetize traffic and achieve business success. By adding sponsored links or displaying ads in various formats, such as splash ads and banner ads, in their apps, app developers will be able to attract targeting audiences to view and tap the ads, or even purchase items. So how do apps always push ads for the right users at the right moment? Audience targeting methods may be the right thing they are looking for.

In the car selling situation, you may wonder how the ad can know what you want.

This benefits from the location-based ad requesting technology. Thanks to the increasing sophistication of ad technology, apps are now able to request user location-based ads, when being authorized, and audience targeting also makes it possible.

So the most important thing for an ad is to reach its target customers. Therefore, app marketing personnel should be giving a lot of thought to how to target audience, place ads online to advertise their items, and maximize ad performance.

That's why it is critical for apps to track audience information. Mobile location data can indicate the user's patterns of consumption. Office workers tend to order a lot of takeout on busy weekdays, trendsetters may prefer more stylish and fashion activities, and homeowners in high-end villas are more likely to purchase luxury items, to cite just some examples. All these mean that user attributes can be extracted from location information for ad matching purposes, and ad targeting should be as precise and multi-faceted as possible.

As an app developer, I am always looking for new tools to help me match and request ads with greater precision. Some of these tools have disappointed me greatly. Fortunately, I stumbled upon Ads Kit in HMS Core, which is capable of [requesting ads based on geographical locations](https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides/publisher-service-location-based-0000001163023235?ha_source=hms3). With this tool, I've been able to integrate ads in various formats into my app with greater ease, and provide targeted, audience specific marketing content, including native and roll ads for nearby restaurants, stores, courses, and more.

I've been able to achieve monetization success with improvement of user conversions and substantially boost my ad revenue as a result.

To display ads more efficiently and accurately, my app can carry users’ location information through the Ads SDK, when requesting ads, so long as my app has been authorized to obtain the users' location information.

The SDK is surprisingly easy to integrate. Here's how to do it:

## Integration Steps

First, request permissions for your app.

i. As the Android OS provides two location permissions: **ACCESS_COARSE_LOCATION** (approximate location permission) and **ACCESS_FINE_LOCATION** (precise location permission), configure the permissions in the **AndroidManifest.xml** file.

```
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
```
ii. (Optional) If your app needs to continuously locate the device of Android 10 or later when it runs in the background, configure the **ACCESS_BACKGROUND_LOCATION** permission in the **AndroidManifest.xml** file.

```
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
```
iii. Dynamically apply for related location permissions (according to requirements for dangerous permissions in Android 6.0 or later).

```
// Dynamically apply for required permissions if the API level is 28 or lower.
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) {
    Log.i(TAG, "android sdk <= 28 Q");
    if (ActivityCompat.checkSelfPermission(this,
        Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
        && ActivityCompat.checkSelfPermission(this,
            Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        String[] strings =
            {Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION};
        ActivityCompat.requestPermissions(this, strings, 1);
    }
} else {
    // Dynamically apply for required permissions if the API level is greater than 28. The android.permission.ACCESS_BACKGROUND_LOCATION permission is required.
    if (ActivityCompat.checkSelfPermission(this,
        Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
        && ActivityCompat.checkSelfPermission(this,
            Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED
        && ActivityCompat.checkSelfPermission(this,
            "android.permission.ACCESS_BACKGROUND_LOCATION") != PackageManager.PERMISSION_GRANTED) {
        String[] strings = {android.Manifest.permission.ACCESS_FINE_LOCATION,
            android.Manifest.permission.ACCESS_COARSE_LOCATION,
            "android.permission.ACCESS_BACKGROUND_LOCATION"};
        ActivityCompat.requestPermissions(this, strings, 2);
    }
}
```
If your app requests and obtains the location permission from a user, the SDK will carry the location information by default. If you do not want to carry the location information in an ad request from the app, you can call the [setRequestLocation()](https://developer.huawei.com/consumer/en/doc/development/HMSCore-References/requestoptions-builder-0000001050064896#section8952113613360?ha_source=hms3) API and set **requestLocation** to **false**.

```
// Here, a banner ad is used as an example. The location information is not carried.
AdParam adParam = new AdParam.Builder()
        // Indicates whether location information is carried in a request. The options are true (yes) and false (no). The default value is true.
        .setRequestLocation(false)
        .build();
bannerView.loadAd(adParam);
```

## Conclusion

All app developers are deeply concerned with how to boost conversions and revenue, by targeting ad audiences. The key is gaining insight into what users care most about. Real time location is a key piece of this puzzle.

If your apps are permitted to do so, you can add personalized ads in apps for these users. Displaying ads through ad networks may be the most popular way to help you monetize traffic and other content. A good advertising mechanism can help you a lot, and in this way, location-based ad requesting is very important in this process. Through users' locations, you will be able to give what they are looking for and show ads perfectly matching user intent. All these implementations may be a complicated process, and I have been also searching for good ways for better results.

As you can see from the coding above, this SDK is easy to implement, with just a few lines of code, and is highly useful for you to request location-based ads. I hope that it serves you as well as it has served me.