How to Get Traffic Data for Better Ad Performance
Tags: java, programming, tutorial, performance
Published: 2022-07-28 09:10:21

## Background

Ads are everywhere and we can't escape them. They're on road billboards, shops, buses and buildings. If a world filled with ads sounds daunting and you want to hide indoors, just beware that ads will even jump out at you from your TV, computer, or phone.

It's likely that ads will be around with us for forever. Ads are often seen as annoying, but they can in fact be very helpful. If you are looking for a job, want to go somewhere fun, or are not sure what special thing you can get for your other half's birthday, ads may in fact provide some inspiration to you.

Nowadays, ads are transforming in appearance and purpose. Ads are becoming more intuitive as they are able to target specific audiences. For example, ads about quick fashion will target young adults, while car ads are typically pushed to automobile enthusiasts. This is an advanced advertising mechanism for brands, media, and users, where all can benefit. Someone gets traffic, someone makes money, and someone gets what they needed. Under ideal conditions, ads, under control and without harmful information spread, mean a lot for us.

As an app developer, I have been trying for a long time to request ads in my app to monetize the traffic and increase revenue. Recently, I encountered some obstacles with the ad traffic monetization data of my own app, and I have been searching for tools to obtain data and analyze ad performance. Collecting such data and then deciding how to analyze ad performance was no easy task, and this was complicated by the many tools and plugins needed to do so. I have used some of the tools found in the industry, but their performance rarely satisfied me.

But I eventually came across a very useful tool, the Publisher Service Reporting API of Ads Kit provided by HMS Core. The traffic monetization service helps me display various ad formats, such as banners, native ads, and rewarded ads, to my users. Routine data tracking is crucial for me to boost revenue. What's more, the Reporting API is key to obtaining ad monetization data, including ad requests, returned ads, impression rate, and click-through rate. I can analyze ad performance based on this data and adjust my monetization strategies accordingly.

But how did I acquire traffic monetization data? Here I'll tell you how I did it.

## Implementation Procedure

i. Obtain the client ID and key.

Before obtaining the monetization data, prepare an OAuth 2.0 client ID and key, which are needed for generating the access token passed for calling the Reporting API.

Note that the client ID and key are not those in **App information** (shown in the following red box) in AppGallery Connect, instead, the client ID and key of the server app created under **HMS API services** > **Credentials** on HUAWEI Developers.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rs8ts9ov8kv0x1wlcgx2.png)

a. [Register as a developer](https://developer.huawei.com/consumer/en/?ha_source=hms3) and complete identity verification.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h1k85yckkxdz7xqzsh5r.png)

b. Sign in to the console, choose **HMS API Services** > **Credentials**, and create a project. If you already have a project, directly select it.

- Click the drop-down arrow at the top, click **New project**, fill in the project name and alias, and click **OK**.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y4xb1871dx33tsw9utvb.png)

- Select the project you created and click **OK**.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xiggm7v5ru016txdg680.png)

c. Choose **OAuth 2.0 client ID**.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7eunvfjbotjal4mlgeh7.png)

If an OAuth 2.0 client ID and key already exist in your project, you can check whether the product type is server app by clicking **Edit** in the **Operation** column of the project. If so, skip step d.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tga35qvwema0lxfrxnsn.png)

If your product is not a server app, you should create an OAuth 2.0 client ID.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1lozijsy710c4ln9gpee.png)

d. Select **Server app** from **Product type**, set **Product name**, **App type**, and **Default language**, and click **Create**. In the **Note** dialog box that displays **Client ID** and **Key**, click **Confirm**.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/312ivmqtr7khxg0qd332.png)

e. Go to **HMS API services** > **My APIs**, ensure that the project is the one you created, and click **Add API from library**.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8w5u5cs9rq1pxfn0q1rw.png)

f. Click **Publisher Service Reporting API** under **App Services**.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mitujp4vo2so7kp2qcfb.png)

g. On the page displayed, click **Enable** to enable the API. Then you can call the API.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bo0in1h687iq8rrbkymv.png)

If you disable it and then enable it again, the setting does not take effect in real time due to page cache, for example. The setting will take about 6 to 10 minutes to take effect. If you have waited for longer time, try again.

ii. Call the Publisher Service Reporting API.

a. Obtain an access token.

Call this API:

POST https://oauth-login.cloud.huawei.com/oauth2/v3/token

Request example:

```
POST /oauth2/v3/token HTTP/1.1
Host: oauth-login.cloud.huawei.com
Content-Type: application/x-www-form-urlencoded

grant_type=client_credentials&
client_id=<Client ID>&
client_secret=<Client secret>
```

Screenshot:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0ve2n5pt6m0mmubjwjq4.png)

Note that again: the client ID and key are those of the server app but not those of the mobile app created.

b. Call the Reporting API.

POST https://ads.cloud.huawei.com/openapi/monetization/reports/v1/publisher

If you are in Russia, call this API:

POST https://ads-drru.cloud.huawei.ru/openapi/monetization/reports/v1/publisher

Request example:

```
POST /openapi/monetization/reports/v1/publisher HTTP/1.1
Content-Type: application/json
Authorization: Bearer ***
        Accept: application/json
Content-Length: 233

{
    "start_date": "2022-06-01",
    "end_date": "2022-06-28",
    "filtering": {
        "currency": "CNY"
    },
    "time_granularity": "STAT_TIME_GRANULARITY_DAILY",
    "page": 1,
    "page_size": 10
}
```
Screenshot:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ljiurio20rgynwr7jja0.png)

As shown in the preceding figure, the data is successfully returned. For more request parameters and response parameters, click [here](https://developer.huawei.com/consumer/en/doc/development/HMSCore-References/query-publisher-service-reports-0000001050933546#section20939514163519?ha_source=hms3).

In addition, I can customize data display formats to suit my specific app scenarios.

## Some Problems You May Encounter
**Why was message "access forbidden" returned during the API calling?**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o2ckn9q10fbuthy5fs80.png)

### Possible causes:

1) You have not enabled the Reporting API.

2) You have not been authorized to call this API, or your permission has not taken effect yet.

3) The client ID and key used for obtaining the access token are not those of your server app.

### My solutions:

i. Click **Add API from library** under **My APIs** and enable the Reporting API.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hbfgrqag4zxbc4lrhwyp.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rrrauha1ck9t076l4byg.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bapabeodvzya2yu6xfkt.png)

ii. If you disabled and enabled the API again, you may need to wait for 6-10 minutes before the setting takes effect.

iii. Check whether your client ID and key belong to a server app when you obtain the access token. To find out it, check the app type on the **Credentials** page. If it is not a server app, create one and use its client ID and key to request an access token.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1oerqp2sbj3w876jqo48.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gg6w2n01m33giufeqqqc.png)

## Conclusion

Daily advertising data tracking matters very much for app developers, and how to easily get these data is also very important. These data helps me learn about app operations effects in real time, and I can adjust my strategies accordingly. Choosing the right tool or method to do this will make us win more with less work. If you also think obtaining ad monetization data and dealing with ad issues are annoying as me, you can try this tool to help you in subsequent operations. Hope that my procedure and experience can benefit more guys and you can also find out more about useful methods.