Implementation when displaying advertisement on WebView on Android

Sample is a here. In the following, we will explain the implementation method with reference to the sample.

Description in the sample

  • MainActivity.java
    • Class that displays WebView.
    • This sample reads the local sample.html.
  • AppData.java
    • It is a class which acquires advertisement ID, LAT, package name.
  • sample.html
    • This is the page to display in WebView.

Notes

The Google Play services SDK is required to obtain the advertisement ID used for RTB parameters from the terminal. Please add com.google.android.gms: play-services to build.gradle. (The latest version is recommended)

Editing the page displayed on WebView

We will place an ad tag in the page to display in WebView. We also set RTB parameters before that ad tag.

Please refer to the sample sample.html

<script>
    // Global variables used by ad tags
    var geparams = window.geparams || {};
    // Get RTB parameters from native processing and set them as global variables.
    geparams.idfa = appData.getAdvertisingId();
    geparams.lat = appData.getLat();
    geparams.bundle = appData.getPackageName();
</script>

<!-- Please set up the ad tag management tool -->
<script type="text/javascript" src="xxx.js"></script>
1
2
3
4
5
6
7
8
9
10
11

Acquisition of application information

The items to be set for the parameter used in RTB are advertisement ID, LAT, package name. Please refer to the sample AppData.java

Control of WebView

Refer to the sample of MainActivity.java for the setting below.

  • Cooperation between JavaScript and WebView
  • Processing when WebView ads are tapped

Cooperation with JavaScript

Set it so that native processing can be called from JavaScript.

webView.getSettings().setJavaScriptEnabled(true);
Context context = getApplicationContext();
webView.addJavascriptInterface(new AppData(context), "appData");
1
2
3

In the example above, when you execute appData.getAdvertisingId () on JavaScript, the getAdvertisingId () method of the native AppData object is called.

Processing when advertisement is tapped

There are cases where iFarme is used for the advertisement to be delivered. Tapping on these ads will cause the page to transition within the framework of the iframe where the ad is shown or will not behave as expected.

When the ad is tapped it is reasonable to activate the browser application and display the transition destination page. In order to realize such behavior, we do as follows.

  1. Edit the zone settings on the management tool and register any keywords to include in the ad URL
  2. When you catch the URL containing the registered keyword on onLoadResource of WebViewClient, judge that it is due to tap of advertisement
  3. Open the URL in the browser application
 webView.setWebViewClient(new WebViewClient() {

    @Override
    public void onLoadResource(WebView view, String url) {

        // Judge that the ad is clicked if the URL contains keywords
        if (url.contains("Keywords set with the management tool")) {
            // Since it opens in an external browser, reading in WebView is interrupted.
            webView.stopLoading();

            // Open in external browser.
            Uri uri = Uri.parse(url);
            Intent i = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(i);
            return;
        }

        super.onLoadResource(view, url);
    }
});

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

Confirmation method after implementation

There is a method to log output to confirm that the parameters are correct. Please set the following tag after parameter setting processing. Logs will be output to the console. You can view it on logcat on Android Stadio.

<script type='text/javascript' src='http://js.gsspcln.jp/l/jssdk_debug.js'></script>
1
  • Log when parameter setting is successful
idfa=xxx  (The value of xxx obtained from the application is displayed)
lat=xxx
bundle=xxx
1
2
3
  • Log when the parameter setting has failed
idfa=undifined
lat=undifined
bundle=undifined
1
2
3
Last Updated: 5/15/2019, 5:41:11 PM